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 );
}
}
运行结果