Spring框架:启动IOC容器的三种方式

来源:互联网 发布:杭州知实食品有限公司 编辑:程序博客网 时间:2024/06/05 05:43

  • 前期准备工作
    • 创建项目时需要用到的框架有Java和Web
    • 创建项目完成之后的目录
  • 通过BeanFactory启动IOC容器
    • bean文件入在类路径classpath下
    • bean文件放在文件系统FileSystem目录下
  • 通过ApplicationContext类启动IOC容器
  • 通过WebApplicationContext类启动IOC容器
  • 总结

前期准备工作

使用IDE:IDEA

创建项目时需要用到的框架有Java和Web

就像下图的进行勾选。

这里写图片描述

创建项目完成之后的目录

如下图所示。

这里写图片描述

创建项目时,gradle中用到的依赖:

// Java EE 通过本地Jar包导入compile fileTree(dir:'lib',include:['*.jar'])// Spring 框架def springVersion = '4.3.10.RELEASE'compile("org.springframework:spring-aop:$springVersion",            "org.springframework:spring-orm:$springVersion",            "org.springframework:spring-jdbc:$springVersion",            "org.springframework:spring-instrument-tomcat:$springVersion",            "org.springframework:spring-instrument:$springVersion",            "org.springframework:spring-framework-bom:$springVersion",            "org.springframework:spring-expression:$springVersion",            "org.springframework:spring-core:$springVersion",            "org.springframework:spring-context-support:$springVersion",            "org.springframework:spring-context:$springVersion",            "org.springframework:spring-beans:$springVersion",            "org.springframework:spring-aspects:$springVersion",            "org.springframework:spring-test:$springVersion",            "org.springframework:spring-tx:$springVersion",            "org.springframework:spring-web:$springVersion",            "org.springframework:spring-webmvc:$springVersion",            "org.springframework:spring-webmvc-portlet:$springVersion")// 模拟测试框架testCompile "org.mockito:mockito-core:2.+"// 集成测试框架testCompile 'org.testng:testng:6.8.17'// 单元测试框架testCompile group: 'junit', name: 'junit', version: '4.12'

还需要的框架有:Java EE

通过BeanFactory启动IOC容器

BeanFactory类是Spring框架下最核心的接口,它提供了高级的IOC配置机制。我们可以使用下面的XmlBeanDefinitionReader和DefaultListableBeanFactory类启动IOC容器。

在com.czuaphe.test包下,创建Car类

这里写图片描述

bean文件入在类路径(classpath)下

在Resource目录编写beans.xml文件生成实例Bean

这里写图片描述

然后,在test/java目录下,使用TestNG框架测试一个BeanFactoryTest测试类
通过加载Resource下的beans.xml文件启动IOC容器,得到实例Bean。

这里写图片描述

上图我们是通过类路径得到XML文件的,在gradle下,类路径就是resources目录下

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();// 通过类路径得到资源,以classpath:开头Resource resource = resolver.getResource("classpath:beans.xml");

运行结果如下图所示:

这里写图片描述

bean文件放在文件系统(FileSystem)目录下

如果我们将beans.xml文件放在与Car类相同的路径下:

这里写图片描述

在test/java目录下,BeanFactoryTest类中的resouce变量只需要改变一下,就可以达到和上面一样的结果。

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();Resource resource = resolver.getResource("file:src/main/java/com/czuaphe/test/beans.xml");

通过ApplicationContext类启动IOC容器

ApplicationContext类继承了BeanFactory类,实现了更多面向应用的功能。

我们就在上例的基础上,在test/java目录下,使用TestNG框架再编写一个ApplicationContextTest类。同样可以实现像上面两种加载XML文件启动IOC容器的方法,此外,ApplicatioinContext类还可以通过类注解的方式加载Bean启动IOC容器。

我们实现通过类注解的方式,实现启动IOC容器。首先,在com.czuaphe.test包下写Bean.java文件,编写实例化Bean的配置。

这里写图片描述

在ApplicationContextTest类下,实现通过加载类注解配置文件启动IOC容器。

这里写图片描述

另外两种通过ApplicationContext类启动IOC容器,也在注释中写出了。

通过类注解启动IOC容器,运行测试的结果如下图所示:

这里写图片描述

通过WebApplicationContext类启动IOC容器

WebApplicationContext类继承了ApplicationContext类,专门用于Web应用。启动方式也是通过Web容器启动,实现IOC容器的启动。不能简单通过Java代码实现启动。

在webapp/WEB-INF下的web.xml中定义ContextLoaderListener监听Web容器的启动,然后定义contextConfigLocation获得Spring框架的Bean配置文件。

然后在web.xml文件中,再定义一个Servlet,定义它的属性load-on-startup为1,表示在Web容器和Spring框架的IOC容器启动之后自启动,得到WebApplicationContext类的实例,就可以得到Bean实例了。

web.xml文件如下图所示

这里写图片描述

在com.czuaphe.webtest包下,编写SpringBeanServlet类,重写Servlet的init()方法,得到WebApplicationContext类的实例。

这里写图片描述

最后,对此项目配置Tomcat服务器,在启动Web容器时,就会启动Spring的IOC容器,而且会运行自定义的SpringBeanServlet,得到Bean实例。运行结果如下图所示:

这里写图片描述

总结

编写了三种IOC容器的启动方式,在非Web应用下,使用ApplicationContext启动IOC容器就已经足够使用,在Web应用下,就必须在web.xml文件中配置WebApplicationContext的启动。让Web容器启动时IOC容器也启动。BeanFactory类启动IOC容器一般不常用。

原创粉丝点击