基于classpath下的xml格式的配置文件来创建bean对象
- java代码
-
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class TestApplicationContext { private static final Logger log = LoggerFactory.getLogger(TestApplicationContext.class); public static void main(String[] args) { testClassPathXmlApplicationContext(); } // todo 较为经典的容器、基于classpath下的xml格式的配置文件来创建 private static void testClassPathXmlApplicationContext() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("b01.xml"); for (String name : context.getBeanDefinitionNames()) { System.out.println(name); } System.out.println(context.getBean(Bean2.class).getBean1()); } static class Bean1 { } static class Bean2 { private Bean1 bean1; public void setBean1(Bean1 bean1) { this.bean1 = bean1; } public Bean1 getBean1() { return bean1; } } }
-
- 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 id="bean1" class="com.example.TestApplicationContext.Bean1"></bean> <bean id="bean2" class="com.example.TestApplicationContext.Bean2"> <property name="bean1" ref="bean1"/> </bean> </beans>
-
-
在前面也讲过,ApplicationContext接口的实现需要BeanFactory接口的支持,具体见文章:BeanFactory与ApplicationContext_熵240的博客-CSDN博客
-
具体如下关键实现代码如下
-
// todo 先创建一个beanFactory对象 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // todo 设置将读取目标存在哪一个beanFactory中 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); // todo 设置读取目标 reader.loadBeanDefinitions(new ClassPathResource("b01.xml")); for (String name : beanFactory.getBeanDefinitionNames()) { System.out.println(name); }
-
-
- 运行结果为
基于磁盘路径下的xml格式的配置文件来创建bean对象
- java代码
-
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class TestApplicationContext { private static final Logger log = LoggerFactory.getLogger(TestApplicationContext.class); public static void main(String[] args) { // testClassPathXmlApplicationContext(); testFileSystemXmlApplication(); } // todo 基于磁盘路径下的xml格式的配置文件来创建bean对象 private static void testFileSystemXmlApplication() { FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\JavaProject\\Spring\\Spring\\src\\main\\resources\\b01.xml"); for (String name : context.getBeanDefinitionNames()) { System.out.println(name); } } static class Bean1 { } static class Bean2 { private Bean1 bean1; public void setBean1(Bean1 bean1) { this.bean1 = bean1; } public Bean1 getBean1() { return bean1; } } }
-
-
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 id="bean1" class="com.example.TestApplicationContext.Bean1"></bean> <bean id="bean2" class="com.example.TestApplicationContext.Bean2"> <property name="bean1" ref="bean1"/> </bean> </beans>
-
-
同样的这种方法也是通过Beanfactory接口来具体实现的
-
具体关键代码如下
-
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new PathResource("D:\\JavaProject\\Spring\\Spring\\src\\main\\resources\\b01.xml")); for (String name : beanFactory.getBeanDefinitionNames()) { System.out.println(name); }
-
-
-
运行结果如下
基于java配置类来创建bean对象
- java代码(配置类代码也写在一起了,一般讲是创建一个配置类)
-
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; public class TestApplicationContext { private static final Logger log = LoggerFactory.getLogger(TestApplicationContext.class); public static void main(String[] args) { // testClassPathXmlApplicationContext(); // testFileSystemXmlApplication(); testAnnotationConfigApplicationText(); } // todo 基于java配置类来创建bean对象 private static void testAnnotationConfigApplicationText() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); for (String name : context.getBeanDefinitionNames()) { System.out.println(name); } } // todo 创建一个配置类 @Configuration static class Config { @Bean public Bean1 bean1() { return new Bean1(); } @Bean public Bean2 bean2(Bean1 bean1) { Bean2 bean2 = new Bean2(); bean2.setBean1(bean1); return bean2; } } static class Bean1 { } static class Bean2 { private Bean1 bean1; public void setBean1(Bean1 bean1) { this.bean1 = bean1; } public Bean1 getBean1() { return bean1; } } }
-