feat(youlai-laboratory-spring): 添加两种条件注入bean

添加两种条件注入bean
This commit is contained in:
zc 2022-03-04 18:06:19 +08:00
parent 6129c77e38
commit 6b2d7616ed
4 changed files with 76 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package com.youlai.laboratory.spring;
import java.util.Objects;
/**
* 说明描述
*
@ -42,4 +44,17 @@ public class Bean {
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bean bean = (Bean) o;
return age == bean.age && Objects.equals(name, bean.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}

View File

@ -2,7 +2,7 @@ package com.youlai.laboratory.spring.beanDefinition;
/**
*
*
* 通过import注入的bean
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/2/18 0018 20:50
*/

View File

@ -81,6 +81,10 @@ public class Property {
}
/**
* 条件注入bean
* @return
*/
@org.springframework.context.annotation.Bean(name="conditional")
@Conditional(DevConditional.class)
public Bean bean(){
@ -90,6 +94,17 @@ public class Property {
/**
* 条件注入bean
* @return
*/
@org.springframework.context.annotation.Bean(name="profile")
@Profile("dev")
public Bean profile(){
return new Bean();
}
}

View File

@ -1,10 +1,13 @@
package com.youlai.laboratory.spring;
import com.google.common.base.Objects;
import com.youlai.laboratory.spring.beanDefinition.BeanA;
import com.youlai.laboratory.spring.beanDefinition.BeanB;
import com.youlai.laboratory.spring.beanDefinition.ImportBean;
import com.youlai.laboratory.spring.beanDefinition.Property;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -22,6 +25,10 @@ import java.util.stream.Collectors;
@SpringBootTest
public class BeanDefinition {
@Autowired
@Qualifier("scope")
private Bean bean;
@Test
void scope() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Property.class);
@ -30,6 +37,9 @@ public class BeanDefinition {
System.out.println(scope1.equals(scope2)); //获取两个相同的bean
}
/**
* 原型模式的bean
*/
@Test
void prototype() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Property.class);
@ -38,6 +48,9 @@ public class BeanDefinition {
System.out.println(prototype1.equals(prototype2)); //获取两个不同的bean
}
/**
* 懒加载的bean
*/
@Test
void lazy() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Property.class);
@ -45,6 +58,9 @@ public class BeanDefinition {
context.getBean("lazy"); //获取bean的时候才创建bean
}
/**
* 多个相同类型的bean,声明默认的bean
*/
@Test
void primary() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Property.class);
@ -52,6 +68,9 @@ public class BeanDefinition {
System.out.println(bean);
}
/**
* 依赖bean
*/
@Test
void dependsOn() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Property.class);
@ -109,5 +128,31 @@ public class BeanDefinition {
System.out.println(context.getBean(ImportBean.class));
}
/**
* 注入指定名称的bean
*/
@Test
void qualifier(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Property.class);
Bean scope = context.getBean("scope", Bean.class);
System.out.println(Objects.equal(scope,bean));
}
/**
* 根据环境注入的bean
*/
@Test
void profile(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod");
//context.getEnvironment().setActiveProfiles("dev");
context.register(Property.class);
context.refresh();
Bean profile = context.getBean("profile", Bean.class);
}
}