- 给容器注册组件@Configuration、@Bean
- 首先我们先生成一个maven项目
- 在pom文件中引入spring的核心组件context依赖:
咱们也引入一下junit测试依赖,方便测试使用<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.20.RELEASE</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <scope>test</scope> </dependency>
- 首先创建一个实体类Person
如代码所示:public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
- 首先我们先使用xml配置的方式来配置bean,并从容器中获取实体
在resource文件夹下创建beans.xml的spring配置文件:
然后在测试类中获取实体:<?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="person" class="entity.Person"> <property name="name" value="张三"></property> <property name="age" value="10"></property> </bean> </beans>
运行结果为:@Test public void testOne() { System.out.println("读取xml配置方法"); //通过ClassPathXmlApplicationContext类读取对应的配置文件,得到spring容器ApplicationContext ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml"); //从spring容器中获取装配的bean组件 Person person = (Person) app.getBean("person"); System.out.println(person.toString()); }
读取xml配置方法 十一月 24, 2018 10:03:05 org.springframework.context.support.ClassPathXmlApplicationContextprepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7506e922: startup date [Sat Nov 24 22:03:05 CST 2018]; root of context hierarchy 十一月 24, 2018 10:03:05 下 org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] Person [name=张三, age=10]
- 接下在咱们通过spring的@Configuration和@Bean注解来实现以上内容:
创建BeanConfig类:
测试类进行测试://让spring识别此类为配置类 @Configuration public class BeanConfig { //此注解为配置组件,默认id为方法名 @Bean public Person person() { return new Person("李四",20); } }
执行结果:@Test public void testTwo() { System.out.println("通过注解配置方法"); //通过AnnotationConfigApplicationContext类获取配置类 ApplicationContext app = new AnnotationConfigApplicationContext(BeanConfig.class); //从容器中获取组件 Person person = (Person) app.getBean("person"); System.out.println(person.toString()); }
通过注解配置方法 十一月 24, 2018 10:03:05 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@20322d26: startup date [Sat Nov 24 22:03:05 CST 2018]; root of context hierarchy Person [name=李四, age=20]
- 通过这两种方式可以发现,注解方式原理跟xml配置一致,都是讲组件加载到spring容器中让其识别。
- 按照条件注册bean组件-@Conditional
- spring底层给我们提供可以根据条件来装配组件。大致原理是通过实现springframework.context.annotation.Condition接口来自己编写装配条件类。然后通过spring注解@Conditional({xxxxxx.class})来实现按照条件注册组件。
- 下面通过装配person来体验一下:通过验证服务器系统来作为条件来进行装配bean
先定义一个linux的条件类:
再定义一个window的条件类:import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; public class LinuxCondition implements Condition{ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment env = context.getEnvironment(); String name = env.getProperty("os.name"); if(name.contains("Linux")) { return true; } return false; } }
然后配置bean:public class WindowCondition implements Condition{ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //获取环境信息 Environment env = context.getEnvironment(); String name = env.getProperty("os.name"); //获取容器中装配的bean信息 BeanDefinitionRegistry beans = context.getRegistry(); //判断是否为windows系统以及是否装配了person组件类 if(name.contains("Windows")&&beans.containsBeanDefinition("person")) { return true; } return false; } }
测试类:@Configuration public class BeanConfig { @Bean("person") public Person person() { return new Person("李四",20); } //只有在windows系统下以及容器中有person组件的情况下才会装配 @Conditional({WindowCondition.class}) @Bean("window") public Person personWin() { return new Person("window",100); } //只有在linux系统下才会被装配 @Conditional({LinuxCondition.class}) @Bean("linux") public Person personLin() { return new Person("linux",200); } }
测试结果为:@Test public void testThree() { ApplicationContext app = new AnnotationConfigApplicationContext(BeanConfig.class); //获取容器中所有类型为Person的组件的名称 String[] names = app.getBeanNamesForType(Person.class); for (String string : names) { System.out.println(string); } }
十一月 25, 2018 11:52:25 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@512ddf17: startup date [Sun Nov 25 11:52:25 CST 2018]; root of context hierarchy person window
- 条件装配在springboot底层实现自动装配时大量使用。先了解一下这个注解,以后方便了解springboot自动装配方式。
spring注解开发——容器部分
猜你喜欢
转载自blog.csdn.net/qq_35340980/article/details/84454496
今日推荐
周排行