Spring_HelloWord
环境:IntelliJ 14 ; jdk1.8
Spring操作步骤
1.新建项目---Spring Batch 2.IntelliJ会自动加载jar包 3.现在就可以在src目录下写Java类文件了 4.将相应的类部署在XML配置文件spring-config.xml中 (Eclipse需要手动创建,貌似名为bean.xml) 5.写主程序main (右键-->Run)
HelloWord小程序
此处直接从第3步开始
3.首先定义两个接口,Person和Axe
public interface Person {
public void useAxe();
}
public interface Axe {
public String chop();
}
然后定义接口的实现类,Chinese和StoneAxe
public class Chinese implements Person {
private Axe axe;
//设值注入
public void setAxe(Axe axe) {
this.axe = axe;
}
@Override
public void useAxe() {
System.out.println(axe.chop());
}
}
public class StoneAxe implements Axe {
@Override
public String chop() {
return "石斧砍柴好慢";
}
}
4.修改spring-config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="chinese" class="org.spring.service.impl.Chinese">
<property name="axe" ref="stoneAxe"/>
</bean>
<bean id="stoneAxe" class="org.spring.service.impl.StoneAxe"/>
</beans>
5.写主程序main
public class SpringTest {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
Person p = (Person) ctx.getBean("chinese", Person.class);
p.useAxe();
}
}
程序到此结束,运行结果为:
石斧砍柴好慢
Spring的核心机制:依赖注入(Dependency Inhection)(控制反转IoC—Inversion of Control)
依赖注入通常包括以下两种:
设值注入:使用属性的setter方法来注入被依赖的实例
构造注入:使用构造器来注入被依赖的实例
请看下面的例子
public class Chinese implements Person {
private Axe axe;
//设值注入
public void setAxe(Axe axe) {
this.axe = axe;
}
//构造注入
public Chinese(Axe axe) {
this.axe = axe;
}
@Override
public void useAxe() {
System.out.println(axe.chop());
}
}
不同的注入方式在配置文件中的配置也不一样
<!--设值注入-->
<bean id="chinese" class="org.spring.service.impl.Chinese">
<property name="axe" ref="steelAxe"/>
</bean>
<!--构造注入-->
<bean id="chinese" class="org.spring.service.impl.Chinese">
<constructor-arg ref="stoneAxe"/>
</bean>
建议采用以设值注入为主,构造注入为辅的注入策略。对于依赖关系无需变化的注入,尽量采用构造注入;而其他的依赖关系的注入,则考虑使用设值注入。
Spring的两个核心接口Spring容器最基本的接口是BeanFactory。它有一个子接口:ApplicationContext。
ApplicationContext有两个常用的实现类:
FileSystemXmlApplicationContext:以基于文件系统的XML配置文件创建实例
ClassPathXmlApplicationContext:以类加载路径下的XML配置文件创建实例
Spring_HelloWord的更多相关文章
- 05_ssm基础(三)之Spring基础
11.spring入门引导 12.spring_HelloWord程序 实现步骤: 0.找到spring压缩包,并解压 1.拷贝jar包 2.添加主配置文件(官方文档约28页) 3.在测试中使用 13 ...
- java之spring之helloword
这篇文章主要讲 spring的基础的使用案例 项目整体目录结构: 1.新建一个java项目:spring_helloworld 2.在项目下创建一个lib文件夹,并把一些必须的jar包复制过去 新建l ...
随机推荐
- java开发目前技术选型
目前系统采用 1.后端 服务框架:Dubbo.zookeeper 缓存:Redis.ehcache 消息中间件:ActiveMQ,kafka 负载均衡:Nginx 分布式文件:FastDFS 数据库连 ...
- HTML5实现端访问时禁止放大和缩小网页
<title>html5禁止和移动.缩放网页</title><meta name="viewport" content="width=dev ...
- java中Statement详细用法。
1.创建 Statement 对象 建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句.Statement 对象用 Connection 的方法createStatement 创建,如下列代 ...
- tomcat 测试页面显示
首先下载匹配jdk版本的tomcat 解压即可使用 将完成的html文件直接放置到webapps目录下的子目录中是无法使用的 原因是tomcat默认加载的是jsp文件,且需要文件配置 所以,除去在we ...
- 从头认识java-13.9 隐式和显示的创建类型实例
对于上一章节擦除引起的问题与解决的方法有读者提出过于简单.这里解释一下:由于笔者本身也遇不到对应的问题.仅仅是凭空想像一些有可能的问题,基于水平有限,因此上一章节写的比較简单,欢迎广大读者踊跃提意见, ...
- OpenCV学习笔记二:OpenCV模块一览
注:本系列博客基于OpenCV 2.9.0.0 一,一览图: 二,模块: /* 基础库 */ 1,opencv_core(链接) ,opencv最基础的库.包含exception,point,rect ...
- POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)
题目链接: PKU:http://poj.org/problem?id=1861 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 谈抽象1——无脑copy等于自杀
近期被外派帮助国内某公司做政府某部门OA系统.听说他们那有个成熟的java框架,使用了非常长时间,抱着学习的态度,我进入这个公司.当我熟悉了一周后,留下了非常多疑问,而这些疑问,也诱发了这次关于&qu ...
- ios应用,64位问题,Missing 64-bit support
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFyb25fYmxvZ3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- coreData笔记
1. CDVehicle *vehicle = (CDVehicle *)[[NSManagedObject alloc] initWithEntity:entity insertIntoMan ...