@Bean
@Configuration
@ConponentScans
使用自己的类型扫描过滤器,来实现对固定的类进行扫描 案例实现
扫描com.learning包中的注解,只有符合过滤条件MyTypeFilter,才可以加入到容器中 因为spring默认扫描指定包下的所有内容,如果做过滤条件,必须将默认的扫描过滤关掉,即useDefaultFilters=false
@Configuration
@ComponentScans ( {
@ComponentScan ( value = "com.learning" , includeFilters = {
@ComponentScan . Filter ( type = FilterType. CUSTOM, classes = { MyTypeFilter. class } )
} , useDefaultFilters = false )
} )
public class MyConfig {
@Bean
public Person person ( ) {
return new Person ( "小明" , 11 ) ;
}
@Bean ( name = "xiaoming" )
public Person person1 ( ) {
return new Person ( "xiaoming" , 23 ) ;
}
}
public class MyTypeFilter implements TypeFilter {
@Override
public boolean match ( MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
AnnotationMetadata annotationMetadata = metadataReader. getAnnotationMetadata ( ) ;
System. out. println ( "++++++" + annotationMetadata. getClass ( ) ) ;
ClassMetadata classMetadata = metadataReader. getClassMetadata ( ) ;
System. out. println ( "-------" + classMetadata. getClassName ( ) ) ;
Resource resource = metadataReader. getResource ( ) ;
System. out. println ( "------" + resource) ;
if ( classMetadata. getClassName ( ) . contains ( "ser" ) ) {
return true ;
}
return false ;
}
}
@Scope
用来声明单实例、多实例 创建时机
singleton:单实例,IOC容器启动会调用方法创建对象放到IOC容器中,以后每次获取就是直接从容器中获得 prototype:多实例,IOC启动不会调用方法创建对象放到容器,而是在每次获取的时候才会调用方法创建对象放到IOC容器中。
@Lazy
@Conditional
spring4.0之后提供的 作用:按照一定的条件进行判断,满足条件给容器中注册bean 使用@Conditional(Condition) 事例
实现功能,在windons环境下,注入bill gate的Person,在Linux环境下,注入linus的Person
@Configuration
@Conditional ( LinuxCondition. class )
public class MyConfig_Conditional {
@Conditional ( WindowsCondition. class )
@Bean ( "bill" )
public Person person ( ) {
return new Person ( "Bill Gates" , 11 ) ;
}
@Conditional ( LinuxCondition. class )
@Bean ( name = "linus" )
public Person person1 ( ) {
return new Person ( "Linus" , 23 ) ;
}
}
public class Person {
private String name;
private int age;
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
@Override
public String toString ( ) {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
}
public class WindowsCondition implements Condition {
@Override
public boolean matches ( ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
ConfigurableListableBeanFactory beanFactory = conditionContext. getBeanFactory ( ) ;
ClassLoader classLoader = conditionContext. getClassLoader ( ) ;
BeanDefinitionRegistry registry = conditionContext. getRegistry ( ) ;
boolean person = registry. containsBeanDefinition ( "person" ) ;
Environment environment = conditionContext. getEnvironment ( ) ;
String property = environment. getProperty ( "os.name" ) ;
return property. toLowerCase ( ) . contains ( "window" ) ;
}
}
public class LinuxCondition implements Condition {
@Override
public boolean matches ( ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Environment environment = conditionContext. getEnvironment ( ) ;
String property = environment. getProperty ( "os.name" ) ;
return property. toLowerCase ( ) . contains ( "linux" ) ;
}
}
public class Test {
static AnnotationConfigApplicationContext applicationContext = null;
static {
applicationContext =
new AnnotationConfigApplicationContext ( MyConfig_Conditional. class ) ;
}
public static void main ( String[ ] args) {
test_Conditional ( ) ;
}
public static void test_Conditional ( ) {
Map< String, Person> beansOfType = applicationContext. getBeansOfType ( Person. class ) ;
beansOfType. forEach ( ( str, p) - > System. out. println ( p) ) ;
}
}