启动 IoC 容器的?式
Java环境下启动IoC容器: ClassPathXmlApplicationContext:从类的根路径下加载配置?件(推荐使?) FileSystemXmlApplicationContext:从磁盘路径上加载配置?件 AnnotationConfigApplicationContext:纯注解模式下启动Spring容器
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
对于bean的配置文件,一般都是以applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao" class="com.nanmao.dao.impl.AccountDaoImpl">
<property name="ConnectionUtils" ref="connectionUtils"></property>
</bean>
<beans>
可以写一个测试用例来实现JavaSE应用下的Ioc容器启
public class IocXmlTest {
@Test
public void TestIocContainer() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
System.out.println(accountDao);
}
}
Web环境下启动IoC容器 从xml启动容器: 在ContextLoaderListener的父类ContextLoader中,我们可以指定配置参数contextConfigLocation的值来读取对应位置的配置文件。如果不配置,默认读取/WEB-INF/applicationContext.xml。
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
在src\main\webapp\WEB-INF\web.xml中配置监听器,当tomcat容器启动时,会读取web.xml文件,发现里面有listener标签,就会根据全类名执行对应的类。
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
spring给我们提供了一个工具类WebApplicationContextUtils,我们可以通过这个工具类来获取容器。在servlet中,把servlet上下文传入即可。
public class TransferServlet extends HttpServlet {
private TransferService transferService = null;
@Override
public void init() throws ServletException {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ProxyFactory proxyFactory = (ProxyFactory) webApplicationContext.getBean("proxyFactory");
transferService = (TransferService)proxyFactory.getJdkProxy(webApplicationContext.getBean("transferService"));
}
从配置类启动容器:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.lagou.edu.SpringConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
|