spring加载ApplicationContext.xml的四种方式

spring加载ApplicationContext.xml的四种方式

项目结构

在这里插入图片描述
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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"

       default-lazy-init="false" default-autowire="byName">
    <bean id="testService" class="com.service.TestService">
    </bean>
</beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.liumin</groupId>
    <artifactId>test-redis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>

    </dependencies>


</project>

TestService.java

package com.service;

/**
 * @Author:lm
 * @Despriction:
 * @Date:2019/3/4 11:27
 */
public class TestService {

    public void hello(){
        System.out.println("hello world");
    }
}

Test.java

import com.service.TestService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @Author:lm
 * @Despriction: spring加载ApplicationContext.xml的四种方式
 * @Date:2019/3/4 11:29
 */
public class Test {
    public static void main(String[] args) {

        doResource();
        doFileSystemXmlApplicationContext();
        doClassPathXmlApplicationContext();

    }

    /**
     * XmlBeanFactory 引用资源
     */
    public static void doResource() {
        Resource resource = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        TestService testService = (TestService) factory.getBean("testService");
        testService.hello();
    }

    /**
     * 用文件系统的路径
     */
    public static void doFileSystemXmlApplicationContext() {
        // src目录下的
        //ApplicationContext ac = new FileSystemXmlApplicationContext("src/appcontext.xml");
        //ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\aoshi\\test-redis\\src\\main\\resources\\applicationContext.xml");
        ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
        TestService testService = (TestService) ac.getBean("testService");
        testService.hello();
    }

    /**
     * ClassPathXmlApplicationContext  编译路径
     */
    public static void doClassPathXmlApplicationContext() {
        //ApplicationContext ac = new ClassPathXmlApplicationContext("file:D:\\aoshi\\test-redis\\src\\main\\resources\\applicationContext.xml");
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        TestService testService = (TestService) ac.getBean("testService");
        testService.hello();
    }

    /**
     * XmlWebApplicationContext 是专为Web工程定制的。
     */
    public static void doXmlWebApplicationContext(){
        //ServletContext servletContext = request.getSession().getServletContext();
        //ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
    }
}

运行结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值