Spring高级--容器实现-ApplicationContext实现(一)
一、ClassPathXmlApplicationContext:从类路径查找 XML 配置文件,创建容器(旧)
1、代码
/** * 较为经典的容器,基于classpath 下xml格式配置文件来创建 */ private static void testClassPathXmlApplicationContext(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("b01.xml"); Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); Bean1 bean1 = context.getBean(Bean2.class).getBean1(); System.out.println(bean1); }
2、Bean
static class Bean1{ } static class Bean2{ private Bean1 bean1; public void setBean1(Bean1 bean1){ this.bean1=bean1; } public Bean1 getBean1(){ return bean1; } }
3、用xml方式注入bean
<?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 id="bean1" class="com.mangoubiubiu.show.AplicontextInterface.Bean1"/> <bean id="bean2" class="com.mangoubiubiu.show.AplicontextInterface.Bean2"> <property name="bean1" ref="bean1"/> </bean> </beans>
结果都注入到了ioc容器,并进行了依赖注入
二、FileSystemXmlApplicationContext:从磁盘路径查找 XML 配置文件,创建容器(旧)
/** * 基于磁盘下的xml格式配置文件来创建 */ private static void testFileSystemXmlApplicationContext(){ FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("E:\\idea_workspace_study\\show\\src\\main\\resources\\b01.xml"); Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); Bean1 bean1 = context.getBean(Bean2.class).getBean1(); System.out.println(bean1); }
结果都注入到了ioc容器,并进行了依赖注入
小总结:
ClassPathXmlApplicationContext和FileSystemXmlApplicationContext是如何将xml里的bean注入进去的
1、ClassPathXmlApplicationContext
public static void main(String[] args) { DefaultListableBeanFactory factory=new DefaultListableBeanFactory(); Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); System.out.println("----------------------------------------------"); XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(new ClassPathResource("b01.xml")); Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); }
2、FileSystemXmlApplicationContext
public static void main(String[] args) { DefaultListableBeanFactory factory=new DefaultListableBeanFactory(); Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); System.out.println("----------------------------------------------"); XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(new FileSystemResource("E:\\idea_workspace_study\\show\\src\\main\\resources\\b01.xml")); Arrays.stream(factory.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); }
三、AnnotationConfigApplicationContext:加入一些有用的处理器
1、代码
private static void testAnnotationConfigApplicationContext(){ AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(Config.class); Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); }
2、用配置类注入Bean
@Configuration static class Config{ @Bean public Bean1 bean1(){ return new Bean1(); } @Bean public Bean2 bean2(){ return new Bean2(); } }
3、相当于<context:annotation-driven/>
没加之前
private static void testClassPathXmlApplicationContext(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("b01.xml"); Arrays.stream(context.getBeanDefinitionNames()).forEach((name)->{ System.out.println(name); }); Bean1 bean1 = context.getBean(Bean2.class).getBean1(); System.out.println(bean1); }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="bean1" class="com.mangoubiubiu.show.AplicontextInterface.Bean1"/> <bean id="bean2" class="com.mangoubiubiu.show.AplicontextInterface.Bean2"> <property name="bean1" ref="bean1"/> </bean> </beans>
加了之后
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="bean1" class="com.mangoubiubiu.show.AplicontextInterface.Bean1"/> <bean id="bean2" class="com.mangoubiubiu.show.AplicontextInterface.Bean2"> <property name="bean1" ref="bean1"/> </bean> <context:annotation-config/> </beans>
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16089617.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
Spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步