Spring框架基础(1)----Ioc容器xml使用

启动 IoC 容器的⽅式
在这里插入图片描述

Java环境下启动IoC容器
ClassPathXmlApplicationContext:从类的根路径下加载配置⽂件(推荐使⽤)
FileSystemXmlApplicationContext:从磁盘路径上加载配置⽂件
AnnotationConfigApplicationContext:纯注解模式下启动Spring容器

        <!--引入spring Ioc容器依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
       <!--引入springWeb功能-->
        <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() {
//        通过读取classpath下面的xml文件来启动容器(xml模式SE应用推荐使用)
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        不推荐使用
//        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("文件绝对路径");
        AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
        System.out.println(accountDao);
    }
}

Web环境下启动IoC容器
从xml启动容器:
在ContextLoaderListener的父类ContextLoader中,我们可以指定配置参数contextConfigLocation的值来读取对应位置的配置文件。如果不配置,默认读取/WEB-INF/applicationContext.xml。

	/**
	 * Name of servlet context parameter (i.e., {@value}) that can specify the
	 * config location for the root context, fallingA back to the implementation's
	 * default otherwise.
	 * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
	 */
	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>
  <!--使⽤监听器启动Spring的IOC容器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

spring给我们提供了一个工具类WebApplicationContextUtils,我们可以通过这个工具类来获取容器。在servlet中,把servlet上下文传入即可。

public class TransferServlet extends HttpServlet {
    //从BeanFactory获取ProxyFactory代理工厂对象,然后通过proxyFactory获取代理对象
//    private ProxyFactory proxyFactory = (ProxyFactory) BeanFactory.getBean("proxyFactory");
//    TransferService transferService = (TransferService)proxyFactory.getJdkProxy(BeanFactory.getBean("transferService"));
    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>
    <!--告诉ContextloaderListener知道我们使⽤注解的⽅式启动ioc容器-->
    <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>
    <!--使⽤监听器启动Spring的IOC容器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值