控制反转(Inversion of Control)是一种是面向对象编程中的一种设计原则,用来减低计算机代码之间的耦合度。其基本思想是:借助于“第三方”实现具有依赖关系的对象之间的解耦。
第一个helloWord程序
程序所需jar包
commons-logging-1.2.jar
junit-4.12.jar
spring-beans-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
spring-test-4.0.4.RELEASE.jar
1.用于IOC容器管理的类
public class HelloWorld {
public HelloWorld(){
System.out.println("init....");
}
public void sayHello(){
System.out.println("Hello Word !");
}
}
2.spring 配置文件
<?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="helloWorld" class="com.plei.HelloWorld" scope="prototype" />
</beans>
@Test
public void sayHello() throws Exception {
//初始化IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//从容器中获取对象
//方法一 : HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//方法二 :HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
System.out.println("----------------");
HelloWorld helloWorld = ctx.getBean("helloWorld",HelloWorld.class);
helloWorld.sayHello();
}
----------------
init....
Hello Word !
上面的例子可以看到我们并没有手动去初始化HelloWorld类而是交由IOC容器去初始化
二.在spring容器中运行Junit
上面的例子可以看到我们是在Junit中加载的Spring容器,而在测试时我们应该在Spring容器中运行JUnit,下面将修改测试类,以使JUnit在Spring容器中运行
需添加jar包:spring-aop-4.0.4.RELEASE.jar
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml") //不加参数时默认加载当前目录 测试类名-context.xml
public class HelloWorldSpringTest {
@Autowired
ApplicationContext ctx;
@Test
public void sayHello() throws Exception{
System.out.println("-------");
HelloWorld helloWorld=ctx.getBean("helloWorld",HelloWorld.class);
helloWorld.sayHello();
}
运行结果
init....
-------
Hello Word !
3.容器初始化对象的几种方式
1.构造器初始化:上面的例子即为使用无参构造器进行初始化,如果没有无参构造器,容器在启动时会报错
2.静态工厂方法初始化:
工厂方法类
public class HelloWordFactory {
private HelloWordFactory(){}
public static HelloWorld getHelloWord(){
return new HelloWorld();
}
}
spring配置文件
<?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="helloWorld" class="com.plei.HelloWordFactory" factory-method="getHelloWord"/>
</beans>
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml") //不加参数时默认加载当前目录 测试类名-context.xml
public class HelloWorldSpringTest {
@Autowired
ApplicationContext ctx;
@Test
public void sayHello() throws Exception{
System.out.println("-------");
HelloWorld helloWorld=ctx.getBean("helloWorld",HelloWorld.class);
helloWorld.sayHello();
}
}
运行结果
init....
-------
Hello Word !
3.实例工厂方法初始化
-------
Hello Word !
3.实例工厂方法初始化
工厂类
public class HelloWordFactory {
public HelloWordFactory(){
System.out.println("factory init....");
}
public HelloWorld getHelloWorld(){
return new HelloWorld();
}
}
spring 配置文件
<?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="helloWorldFactory" class="com.plei.HelloWordFactory"/>
<bean id="helloWorld" factory-bean="helloWorldFactory" factory-method="getHelloWorld"/>
</beans>
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml") //不加参数时默认加载当前目录 测试类名-context.xml
public class HelloWorldSpringTest {
@Autowired
ApplicationContext ctx;
@Test
public void sayHello() throws Exception{
System.out.println("-------");
HelloWorld helloWorld=ctx.getBean("helloWorld",HelloWorld.class);
helloWorld.sayHello();
}
}
运行结果
factory init....
helloWorld init....
-------
Hello Word !
helloWorld init....
-------
Hello Word !