标签:spring注解 null 容器 epo pack rate val person 注释
<bean class="test.bean.Person" id="person"> <property name="age" value="18"></property> <property name="name" value="zhangsan"></property> </bean>
ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("beans.xml"); Person bean = cac.getBean("person", Person.class); System.out.println(bean); cac.close();
@Configuration public class MainConfig { // 给容器中注册一个Bean; 类型为返回值的类型, id默认是方法名 @Bean(value = "person01") public Person person() { return new Person("lisi", 20); } }
public class MainTest { public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig.class); Person bean2 = ac.getBean(Person.class); Person bean3 = ac.getBean(Person.class); System.out.println(bean2 == bean3); ac.close(); } }
@Configuration @ComponentScan(value = "test") public class MainConfig { // 给容器中注册一个Bean; 类型为返回值的类型, id默认是方法名 @Bean(value = "person01") @Lazy @Scope("singleton") public Person person() { return new Person("lisi", 20); } }
<!-- 包扫描: 只要标注了@Component, @Service, @Repository, @Controller, 就会被自动扫描加进容器 --> <context:component-scan base-package="test"></context:component-scan>
@ComponentScan(value = "test", excludeFilters = { @Filter(type=FilterType.ANNOTATION, classes = {Controller.class, Service.class}) }) @ComponentScan(value = "test", includeFilters = { @Filter(type=FilterType.ANNOTATION, classes = {Repository.class}) })
@Filter(type = FilterType.ASSIGNABLE_TYPE, classes= {BookService.class})
public class MyTypeFilter implements TypeFilter { /** * metadataReader: 读取到当前正在扫描的类的信息 * metadataReaderFactory: 可以获取到其他任何类信息的. * * 返回false是一个也不匹配. */ @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //获取当前类注解的信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //获取当前正在扫描的类的类信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); String className = classMetadata.getClassName(); System.out.println(className); //获取当前类资源(类的路径等信息) Resource resource = metadataReader.getResource(); //类名中有"er"就会被扫描进容器. if(className.contains("er")) { return true; } return false; } }
@Filter(type=FilterType.CUSTOM, classes = {MyTypeFilter.class})
public class MaleCondition implements Condition { /** * ConditionContext: 判断条件能使用的上下文(环境) * AnnotatedTypeMetadata: 注释信息 * 返回true则允许注册. */ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //是否为男 //1.能获取到ioc使用的beanfactory ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); //2.获取类加载器 ClassLoader classLoader = context.getClassLoader(); //3.获取当前环境信息 Environment environment = context.getEnvironment(); //获取到bean定义的注册类 BeanDefinitionRegistry registry = context.getRegistry(); String property = environment.getProperty("os.name"); //当前系统是否为Windows if(property.contains("Windows")) { return true; } return false; } }
public class FemaleCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); String property = environment.getProperty("os.name"); if(property.contains("Linux")) { return true; } return false; } }
@Conditional({ MaleCondition.class }) @Bean("ming") public Person person001() { return new Person("xiaoming", 16); } @Conditional({ FemaleCondition.class }) @Bean("hong") public Person person002() { return new Person("xiaohong", 15); }
@Configuration @ComponentScan(value = "test") @Import({Color.class, Red.class}) //导入组件, id默认是组件的全类名 public class MainConfig { //... }
//自定义逻辑, 返回需要导入的组件 public class MyImportSelector implements ImportSelector { /** * 返回值就是导入到容器中的组件的全类名 * AnnotationMetadata: 当前标注@Import的注解类的所有注解信息. */ @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { // 方法不能返回null值, 可以返回一个空数组 return new String[] {"test.bean.Blue", "test.bean.Yellow"}; } }
@Configuration @ComponentScan(value = "test") @Import({Color.class, Red.class, MyImportSelector.class}) //导入组件, id默认是组件的全类名 public class MainConfig { //... }
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * BeanDefinitionRegistry: BeanDefinition注册类, 把所有需要添加到容器中的bean, * 调用BeanDefinitionRegistry.registerBeanDefinition手工注册进来 */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { //是否包含"red"这个组件 boolean definition = registry.containsBeanDefinition("red"); boolean definition2 = registry.containsBeanDefinition("blue"); if(definition && definition2) { //指定Bean信息: (Bean的类型, Scope等) RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class); //指定bean名 registry.registerBeanDefinition("rainBow", beanDefinition); } } }
@Configuration @ComponentScan(value = "test") @Import({MyImportSelector.class, MyImportBeanDefinitionRegistrar.class}) //导入组件, id默认是组件的全类名 public class MainConfig { //... }
//创建一个Spring定义的FactoryBean public class ColorFactoryBean implements FactoryBean<Color> { // 返回一个Color对象, 这个对象会添加到容器中 @Override public Color getObject() throws Exception { // TODO Auto-generated method stub return new Color(); } @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return Color.class; } // 是否单例 @Override public boolean isSingleton() { return false; } }
@Bean public ColorFactoryBean colorFactoryBean() { return new ColorFactoryBean(); }
@Test public void test() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig.class); Object bean = ac.getBean("colorFactoryBean"); System.out.println(bean); Object bean2 = ac.getBean("&colorFactoryBean"); System.out.println(bean2); }
标签:spring注解 null 容器 epo pack rate val person 注释
原文地址:https://www.cnblogs.com/binwenhome/p/13064849.html