美文网首页SpringBoot云课堂我爱编程
Spring的依赖注入及三种配置方式(Spring的测试框架和配

Spring的依赖注入及三种配置方式(Spring的测试框架和配

作者: Tommmmm | 来源:发表于2018-01-30 15:38 被阅读84次

Spring的测试框架和ApplicationContext配置文件

一、ApplicationContext的实现类

1.1 使用FileSystemXmlApplicationContext获取spring配置文件

Application.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-3.0.xsd">
    <description>Spring Quick Start</description>

    <bean id="TheAction1" class="com.example.service.LowerAction">
        <property name="prefix">
            <value>Hi</value>
        </property>
        <property name="message">
            <value>Good Morning</value>
        </property>
    </bean>
    <bean id="TheAction2" class="com.example.service.LowerAction">
        <constructor-arg index="0">
            <value>Hi</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value>Good Afternoon</value>
        </constructor-arg>
    </bean>
</beans>

使用FileSystemXmlApplicationContext来获取spring配置文件

    @Test
    public void test1() {
        String XML = "file:src/applicationContext.xml";
        ApplicationContext ctx = new FileSystemXmlApplicationContext(XML);
        Action action = (Action) ctx.getBean("TheAction1");
        action.execute("Rod Johnson4");
    }

实验结果如下:


实验结果

想到的问题 FileSystemXmlApplicationContext 的路径
没有盘符的是项目工作路径,即项目的根目录;
有盘符表示的是文件绝对路径。
一般用file:作为前缀,也可以省略

使用绝对路径:

    @Test
    public void test1() {
        String XML="file:/Users/wushuohan/IdeaProjects/hello/src/applicationContext.xml";
        ApplicationContext ctx = new FileSystemXmlApplicationContext(XML);
        Action action = (Action) ctx.getBean("TheAction1");
        action.execute("Rod Johnson4");
    }

实验中碰到的问题
在实验中一开始只注释掉了@ContextConfiguration,而忘记注释掉@RunWith。
错误代码如下:

@RunWith(SpringRunner.class)
//@ContextConfiguration(locations={"file:src/applicationContext.xml"})
public class Test2_InJunit {
    @Test
    public void test1() {
        String XML = "file:src/applicationContext.xml";
        ApplicationContext ctx = new FileSystemXmlApplicationContext(XML);
        Action action = (Action) ctx.getBean("TheAction1");
        action.execute("Rod Johnson4");
    }

程序报错如下:


报错

错误原因
@RunWith是一个运行器,而@ContextConfiguration指定 Spring 配置文件所在的位置。
@RunWith(SpringRunner.class)让测试运行于Spring测试环境,Spring框架在org.springframework.test.annotation 包中提供。

因为Action1是我自己在XML里定义的bean,而我这里只使用了@RunWith导致了程序抛出空指针异常,因为并没有被初始化的相应Bean对象。

从这个问题中我们可以近一步考虑Spring容器与Bean之间的关系:

ApplicationContext ctx=new ClassPathXmlApplicationContext("file:src/ApplicationContext.xml");
Action action = (Action)ctx.getBean("TheAction1");

在创建ApplicationContext实例对象过程中会创建一个spring容器,该容器会读取配置文件" file:src/ApplicationContext.xml ",并统一管理由该文件中定义好的所有bean实例对象。
如果要获取某个bean实例,使用getBean方法就行了。
因此只需要将Action提前配置在xml文件中,之后可以不需使用new Action()的方式创建实例,而是通过容器来获取实例,这就相当于将Action的控制权交由spring容器了。


1.2使用ClassPathXmlApplicationContext获取Spring配置文件

测试函数如下:

    @Test
    public void test2() {
        String XML = "file:src/applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext("src/applicationContext.xml");
        Action action1 = (Action) ctx.getBean("TheAction1");
        action1.execute("Rod Johnson5");
    }

实验结果如下所示:


实验结果

想到的问题:ClassPathXmlApplicationContext的路径有哪些写法?
默认的是读取classes文件夹下的文件,可以在前面加上classpath,不加也可以。
用file作前缀加上绝对路径也可以。

实验中碰到的问题1:classpath在哪里?
一开始直接把XML文件放在TARGET的CLASSES下面,报错如下:

报错

打印classpath,在控制台里查看具体路径:

    @Test
    public void testt(){
        String s[] = System.getProperty("java.class.path").split(";");
        for (String string : s) {
            System.out.println(string);
        }

    }

打印结果如下:


打印结果

还是不知道在哪里……

最后我把XML文件直接放在Resource文件夹下:


放在Resource文件夹下

测试成功:


测试成功

1.3使用 @ContextConfiguration(locations={……})获取Spring配置文件

测试代码如下:

@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"file:src/applicationContext.xml"})
public class Test2_InJunit {
    @Test
    public void test3() {
        action1.execute("Rod Johnson5");
    }

    @Autowired
    @Qualifier("TheAction1")
    Action action1;
}

实验结果如下:


实验结果

实验中想到的问题:为什么不再需要像之前那样getBean()?

因为有@Autowired
Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。

实验中碰到的问题:
XML文件里的Bean太多,报错如下

报错
解决方法,在@AutoWired下加上@Qualifier
    @Test
    public void test3() {
        action1.execute("Rod Johnson5");
        action2.execute("Rod Johnson5");

    }
    @Autowired
    @Qualifier("TheAction1")
    Action action1;

    @Autowired
    @Qualifier("TheAction2")
    Action action2;

本章的总结

BeanFactory和ApplicationContext是Spring的两大核心接口,其中ApplicationContext是BeanFactory的子接口。
它们都可以当作Spring的容器,Spring容器是生成Bean实例的工厂,并管理容器中的Bean。在基于Spring的Java EE应用中,所有的组件都被当成Bean处理,包括数据源,Hibernate的SessionFactory、事务管理器等。

BeanFactory负责配置、创建、管理Bean,还管理着Bean和Bean之间的依赖关系。

ApplicationContext常用的实现类是:
FileSystemXmlApplicationContext
ClassPathXmlApplicationContext
AnnotationConfigApplicationContext
除了提供BeanFactory所支持的所有功能外,ApplicationContext还有额外的功能,如资源访问,比如访问URL和文件、同时加载多个配置文件、以声明方式启动并创建Spring容器等


相关文章

网友评论

    本文标题:Spring的依赖注入及三种配置方式(Spring的测试框架和配

    本文链接:https://www.haomeiwen.com/subject/colczxtx.html