spring-IOC

控制反转(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>


3.测试类

    @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();
    }


4.输出结果

----------------
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.实例工厂方法初始化
工厂类
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 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值