将Bean交给Spring容器管理有几种方式?

Spring核心

Spring核心是 IOC 和 AOP

所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系。

至于更详细的说明,或者去深入理解Spring这两大核心,不是此篇文章的目的,暂不细说了。

我们在Spring项目中,我们需要将Bean交给Spring容器,也就是IOC管理,这样你才可以使用注解来进行依赖注入。

包扫描+组件注解

针对类是我们自己编写的情况

这种方式是我们日常开发中最常用到的spring将扫描路径下带有@Component@Controller@Service@Repository注解的类添加到spring IOC容器中。

如果你使用过MybatisPlus,那这个就和他的包扫描注入一样。

那我们这个ComponentScan注解,又有三个配置。

配置项一

basePackages用于定义扫描的包的路径。

@ComponentScan(basePackages = "com.timemail.bootmp")
复制代码

比如这样,就是扫描com.timemail.bootmp整个包下的,带有以上指定注解的类,并放入IOC

我在别的文章上找了一个完整的示例:

@Component
public class Person {
    private String name;
 
    public String getName() {
 
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + ''' +
                '}';
    }
}
 
@ComponentScan(basePackages = "com.springboot.initbean.*")
public class Demo1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
复制代码
//结果
Person{name='null'}
复制代码

这就说明上面代码的Person类已经被IOC容器所管理了。

配置项二

includeFilters包含规则

Filter注解 用 FilterType.CUSTOM 可以自定义扫描规则 需要实现TypeFilter接口实现match方法 其中参数 MetadataReader 当前类的信息(注解,类路径,类原信息…) MetadataReaderFactory MetadataReader的工厂类。

配置项三

excludeFilters移除规则

同包含规则。

这后两个配置项我没怎么用过,也不太熟悉,所以详细使用请自行查阅相关资料。

@Configuration + @Bean

@Configuration + @Bean也是我们常用的一种放入容器的方式。

@Configuration用于声明配置类

@Bean用于声明一个Bean

@Configuration
public class Demo {
    @Bean
    public Person person() {
        Person person = new Person();
        person.setAge(10);
        return person;
    }
}
复制代码

就像这样。

那我们指知道,在SSM里面,通常我们会在xml里面去配置bean

@Configuration
public class ConfigBean {
​
}
复制代码

那我们这个@Configuration注解,就相当于一个Beanxml配置。

<?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-4.3.xsd">
​
</beans>
复制代码

Bean注解中的属性

我们@Bean注解还有许多属性可以配置。

我们可以查看其源码:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) //@1
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
​
    @AliasFor("name")
    String[] value() default {};
​
    @AliasFor("value")
    String[] name() default {};
​
    @Deprecated
    Autowire autowire() default Autowire.NO;
​
    boolean autowireCandidate() default true;
​
    String initMethod() default "";
​
    String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
}
复制代码
  1. value和name是一样的,设置的时候,这2个参数只能选一个,原因是@AliasFor导致的
  2. value:字符串数组,第一个值作为bean的名称,其他值作为bean的别名
  3. autowire:这个参数上面标注了@Deprecated,表示已经过期了,不建议使用了
  4. autowireCandidate:是否作为其他对象注入时候的候选bean。
  5. initMethod:bean初始化的方法,这个和生命周期有关,以后详解
  6. destroyMethod:bean销毁的方法,也是和生命周期相关的,以后详解

扩展

@Configuration修饰的类,spring容器中会通过cglib给这个类创建一个代理,代理会拦截所有被@Bean修饰的方法,默认情况(bean为单例)下确保这些方法只被调用一次,从而确保这些bean是同一个bean,即单例的。

@Import注解导入

先看源码:

​
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
 
    /**
   * 用于导入一个class文件
     * {@link Configuration @Configuration}, {@link ImportSelector},
     * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
     */
    Class<?>[] value();
 
}
复制代码

@Import只能用于类注解。

这里我直接搬运公众号:小哈学Java的内容吧。

他的讲解非常详细。

前两种方式,大家用的可能比较多,也是平时开发中必须要知道的,@Import注解用的可能不是特别多了,但是也是非常重要的,在进行Spring扩展时经常会用到,它经常搭配自定义注解进行使用,然后往容器中导入一个配置文件。

关于@Import注解,我会多介绍一点,它有四种使用方式。这是@Import注解的源码,表示只能放置在类上。

@Import直接导入类

public class Person {
    private String name;
 
    public String getName() {
 
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + ''' +
                '}';
    }
}
/**
* 直接使用@Import导入person类,然后尝试从applicationContext中取,成功拿到
**/
@Import(Person.class)
public class Demo1 {
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
复制代码

上述代码直接使用@Import导入了一个类,然后自动的就被放置在IOC容器中了。

注意:我们的Person类上 就不需要任何的注解了,直接导入即可。

@Import + ImportSelector

其实在@Import注解的源码中,说的已经很清楚了,感兴趣的可以看下,我们实现一个ImportSelector的接口,然后实现其中的方法,进行导入。

@Import(MyImportSelector.class)
public class Demo1 {
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
 
class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.springboot.pojo.Person"};
    }
}
复制代码

我自定义了一个MyImportSelector实现了ImportSelector接口,重写selectImports方法,然后将我们要导入的类的全限定名写在里面即可,实现起来也是非常简单。

@Import + ImportBeanDefinitionRegistrar

这种方式也需要我们实现ImportBeanDefinitionRegistrar接口中的方法,具体代码如下:

@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo1 {
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
 
class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
 
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 构建一个beanDefinition, 关于beanDefinition我后续会介绍,可以简单理解为bean的定义.
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
        // 将beanDefinition注册到Ioc容器中.
        registry.registerBeanDefinition("person", beanDefinition);
    }
}
复制代码

上述实现其实和Import的第二种方式差不多,都需要去实现接口,然后进行导入。接触到了一个新的概念,BeanDefinition,可以简单理解为bean的定义(bean的元数据),也是需要放在IOC容器中进行管理的,先有bean的元数据,applicationContext再根据bean的元数据去创建Bean

@Import + DeferredImportSelector

这种方式也需要我们进行实现接口,其实它和@Import的第二种方式差不多,DeferredImportSelector 它是 ImportSelector 的子接口,所以实现的方法和第二种无异。只是Spring的处理方式不同,它和Spring Boot中的自动导入配置文件 延迟导入有关,非常重要。使用方式如下:

@Import(MyDeferredImportSelector.class)
public class Demo1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
class MyDeferredImportSelector implements DeferredImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        // 也是直接将Person的全限定名放进去
        return new String[]{Person.class.getName()};
    }
}
复制代码

关于@Import注解的使用方式,大概就以上三种,当然它还可以搭配@Configuration注解使用,用于导入一个配置类。

FactoryBean接口

说到FactoryBean,我们很多入门的开发者很容易将他与BeanFactory搞混。

BeanFactory他是所有Spring Bean的容器根接口,给Spring 的容器定义一套规范,给IOC容器提供了一套完整的规范,比如我们常用到的getBean方法等。

也就是我们常说的Bean的工厂。

而我们的FactoryBean,它实际上就是一个BeanFactory是他的名字,顾名思义嘛。

@Configuration
public class Demo1 {
    @Bean
    public PersonFactoryBean personFactoryBean() {
        return new PersonFactoryBean();
    }
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
 
class PersonFactoryBean implements FactoryBean<Person> {
 
​
    @Override
    public Person getObject() throws Exception {
        return new Person();
    }
​
    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }
}
复制代码

这里,我们可以先看看FactoryBean中的方法:

他是一个接口类。

那我们就需要有一个类来继承这个接口,并且重写方法。

这里,我们将需要注册的Bean的类,放到FactoryBean的泛型中。

getObject方法用于直接返回创建的对象。

getObjectType直接返回类的class

然后实际上,还是要使用@Bean注解,将继承接口的类对象返回。

然后Configuration注解,将此类改为springboot的配置类,相当于springmvc中的xml文件。

我们可以通过AnnotationConfigApplicationContextgetBean方法,来看看是否被IOC管理。

运行后,可以看到,对象地址被输出了。

说明成功了。

使用 BeanDefinitionRegistryPostProcessor

写这篇文章时,我也是查阅了网上很多大佬的资料。

有一种我不熟悉的方法。

于是......


开始原文照搬....

其实这种方式也是利用到了 BeanDefinitionRegistry,在Spring容器启动的时候会执行 BeanDefinitionRegistryPostProcessorpostProcessBeanDefinitionRegistry ()方法,大概意思就是等beanDefinition加载完毕之后,对beanDefinition进行后置处理,可以在此进行调整IOC容器中的beanDefinition,从而干扰到后面进行初始化bean。

public class Demo1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        MyBeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new MyBeanDefinitionRegistryPostProcessor();
        applicationContext.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor);
        applicationContext.refresh();
        Person bean = applicationContext.getBean(Person.class);
        System.out.println(bean);
    }
}
 
class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
 
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
        registry.registerBeanDefinition("person", beanDefinition);
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
 
    }
}
复制代码

上述代码中,我们手动向beanDefinitionRegistry中注册了person的BeanDefinition,最终成功将person加入到applicationContext中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值