ClassPathXmlApplicationContext
基于classpath下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.xsd"><bean class="com.xkj.org.ApplicationContextA.Bean1" id="bean1"></bean><bean class="com.xkj.org.ApplicationContextA.Bean2" id="bean2"><property name="bean1" ref="bean1"></property></bean>
</beans>
package com.xkj.org;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationContextA {public static void main(String[] args) {testClassPathXmlApplicationContext();}private static void testClassPathXmlApplicationContext() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");String[] beanDefinitionNames = context.getBeanDefinitionNames();for (String beanDefinitionName : beanDefinitionNames) {System.out.println(beanDefinitionName);}Bean2 bean2 = context.getBean(Bean2.class);System.out.println(bean2.getBean1());}static class Bean1 {}static class Bean2 {private Bean1 bean1;public Bean1 getBean1() {return bean1;}public void setBean1(Bean1 bean1) {this.bean1 = bean1;}}
}
FileSystemXmlApplicationContext
基于磁盘路径下xml格式的配置文件夹创建
private static void testFileSystemXmlApplicationContext() {FileSystemXmlApplicationContext context =new FileSystemXmlApplicationContext("D:\\projects\\reggie_take_out\\src\\main\\resources\\bean1.xml");String[] beanDefinitionNames = context.getBeanDefinitionNames();for (String beanDefinitionName: beanDefinitionNames) {System.out.println(beanDefinitionName);}Bean2 bean2 = context.getBean(Bean2.class);System.out.println(bean2.getBean1());}
基于相对路径也可以:
FileSystemXmlApplicationContext context =new FileSystemXmlApplicationContext("src\\main\\resources\\bean1.xml");
内部原理:
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();System.out.println("读取之前...");String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();for (String beanDefinitionName : beanDefinitionNames) {System.out.println(beanDefinitionName);}System.out.println("读取之后...");XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));for(String name: beanFactory.getBeanDefinitionNames()) {System.out.println(name);}
AnnotationConfigApplicationContext
基于java配置类来创建
private static void testAnnotationConfigApplicationContext() {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);for (String s : context.getBeanDefinitionNames()) {System.out.println(s);}Bean2 bean2 = context.getBean(Bean2.class);System.out.println(bean2.getBean1());}
private static void testAnnotationConfigApplicationContext() {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);for (String s : context.getBeanDefinitionNames()) {System.out.println(s);}Bean2 bean2 = context.getBean(Bean2.class);System.out.println(bean2.getBean1());}@Configurationstatic class Config {@Beanpublic Bean1 bean1() {return new Bean1();}@Beanpublic Bean2 bean2(Bean1 bean1) {Bean2 bean2 = new Bean2();bean2.setBean1(bean1);return bean2;}}static class Bean1 {}static class Bean2 {private Bean1 bean1;public Bean1 getBean1() {return bean1;}public void setBean1(Bean1 bean1) {this.bean1 = bean1;}}
AnnotationConfigServletWebServerApplicationContext
基于java配置类来创建,用于web环境。
private static void testAnnotationConfigServletWebServerApplicationContext() {AnnotationConfigServletWebServerApplicationContext context =new AnnotationConfigServletWebServerApplicationContext(WebConfig.class);for (String s : context.getBeanDefinitionNames()) {System.out.println(s);}}
@Configurationstatic class WebConfig{//servlet服务器@Beanpublic ServletWebServerFactory servletWebServerFactory() {return new TomcatServletWebServerFactory();}@Beanpublic DispatcherServlet dispatcherServlet() {return new DispatcherServlet();}//将dispatcherServlet给一个路径注册到servlet服务器@Beanpublic DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet) {return new DispatcherServletRegistrationBean(dispatcherServlet, "/");}//控制器@Bean("/hello")public Controller controller1() {return (request ,response) -> {response.getWriter().print("hello");return null;};}}