1. Spring IOC 注解

心已赠人 2023-06-27 05:37 118阅读 0赞

1. Spring IOC 注解

1.1 装配Bean

  1. BeanFactory是所有IOC容器的父类,ApplicationContext也继承自BeanFactory

    public interface BeanFactory {

    1. //前缀
    2. String FACTORY_BEAN_PREFIX = "&";
    3. //以下多个获取bean的方法
    4. Object getBean(String var1) throws BeansException;
    5. <T> T getBean(String var1, Class<T> var2) throws BeansException;
    6. Object getBean(String var1, Object... var2) throws BeansException;
    7. <T> T getBean(Class<T> var1) throws BeansException;
    8. <T> T getBean(Class<T> var1, Object... var2) throws BeansException;
    9. <T> ObjectProvider<T> getBeanProvider(Class<T> var1);
    10. <T> ObjectProvider<T> getBeanProvider(ResolvableType var1);
    11. boolean containsBean(String var1);
    12. //是否单例,默认是
    13. boolean isSingleton(String var1) throws NoSuchBeanDefinitionException;
    14. //是否是原型
    15. boolean isPrototype(String var1) throws NoSuchBeanDefinitionException;
    16. boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException;
    17. boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException;
    18. @Nullable
    19. Class<?> getType(String var1) throws NoSuchBeanDefinitionException;
    20. //获取bean的类型
    21. @Nullable
    22. Class<?> getType(String var1, boolean var2) throws NoSuchBeanDefinitionException;
    23. //获取bean的别名
    24. String[] getAliases(String var1);

    }

  2. Spring会把ApplicationContext的类目录及所有子目录下的组件自动添加到容器中

  3. @Configuration注解的类代表是配置类

    @Configuration
    public class AppConfig{

    1. @Bean(value="admin")
    2. public User adminUser(){
    3. User user = new User();
    4. return user;
    5. }

    }

可以通过如下方法测试:

  1. public void test(){
  2. ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  3. //ApplicationContext可以通过类名或者Bean名称两种方式获取bean
  4. User user = context.getBean(User.class);
  5. log.info(user.getUserName());
  6. }
  1. @Bean注解一个方法,会把方法的返回对象添加到容器中,若没有用value=””指定名称,则会把方法的名称作为该组件的名称。
  2. Bean在容器中,默认是单例的
  3. 自动装配bean到容器(无需@Bean注解)
    使用**@Component标明哪个类被扫描进入IOC容器
    使用
    @ComponentScan**标明使用何种策略扫描装配Bean(默认是@ComponentScan注解的类当前目录及其子目录的所有@Component注解的类都自动添加到容器中)。
    @SpringBootApplication注解中就有@ComponentScan注解,所以ApplicationContext目录及子目录的组件会自动添加到容器。

    @Component(“user”)
    public class User{

    1. @Value(1)
    2. private int id;
    3. @Value("nike")
    4. private String username;
    5. @Value("note1")
    6. private String note;

    }

@ComponentScan的扫描策略可指定扫描的包基础路径、过滤条件等。如:

  1. @ComponentScan(basePackages = "xyz.mxlei.base.*",
  2. excludeFilters = {
  3. @Filter(classes={
  4. Service.class})})

1.2 依赖注入

在非常多的bean都装入容器中后,每个bean在容器中都行相互独立的,而在实际应用中需要不同的bean之间进行相互依赖使用。这时候就使用依赖注入的方式实现bean之间的依赖关系。

  1. @AutoWired
    根据数据类型type来获取bean,在IOC容器的顶级接口BeanFactory中有相应的getBean方法。
    如果@AutoWire注解的对象是接口类型的,当项目中只有一个该接口实现类时正常getBean,当有多个实现类时,会根据对象的名称,通过以bean名称的方式获取bean,若依旧不匹配,则抛出异常。

    public interface Animal{

  1. }
  2. public class Dog implements Animal{
  3. }
  4. public class Cat implements Animal{
  5. }
  6. @Componment
  7. public class Test{
  8. //由于Animal有两个实现类,以dog名称获取bean
  9. @AutoWired
  10. public Animal dog;
  11. }
  1. @AutoWired冲突处理
    当通过类型获取bean有多个时,可以用1中的处理方法,也可以用@Primary或者@Qualifier两个注解来解决。
  2. @Primary
    用来注解bean类,告诉IOC容器,当发现多个同类型bean时,请优先使用我进行注入。

    @Primary
    public class Dog implements Animal{

    }

但是多个类用@Primary注解时,还会出现不知道如何选择的问题,这时可用@Qualifier

  1. @Qualifier
    和@AutoWired结合使用,表示使用名称和类型结合一起来查找bean。

    @Componment
    public class Test{

    1. //名称和类型结合一起来查找bean
    2. @AutoWired
    3. @Qualifier("dog")
    4. public Animal animal;

    }

  2. 带参数的构造方法类的装配
    上面中队Test类装配了bean,是基于Test类的构造方法是无参的,假如Test类只有有参构造方法,可通过@AutoWired注解构造方法的参数进行注入。

    @Componment
    public class Test{

    1. public Animal animal;
    2. public Test( @AutoWired @Qualifier("dog") Animal animal){
    3. this.animal = animal;
    4. }

    }

1.3 Bean的生命周期

bean在@AutoWired注入时才实例化

ComponentScan所定义的包

bean定义保存到BeanDefinition实例中

IOC容器装在Bean定义

创建Bean的实例对象

资源定位

Bean定义

发布Bean定义

实例化

依赖注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fj3h1911-1578040728960)(https://i.loli.net/2020/01/03/kltDoTObW5IPeKz.png)\]

1.4 使用配置文件配置bean属性

可以用application.properties或application.yaml来配置bean属性。

@compoment注解的类会被扫描添加到IOC容器,这些bean的参数可以在类中赋予初始值也可在配置文件中配置。

  1. application.properties配置User的值

    admin.username=mxlei
    admin.password=1234

  2. @Value配置

    @Component
    @ConfigurationProperties(“admin”)
    public class User{

    1. @Value(${
    2. admin.username})
    3. private String username;
    4. @Value(${
    5. admin.password})
    6. private String password;

    }

  3. @ConfigurationProperties配置
    设置配置文件中的配置名的前缀,自动根据名称来初始化值

    @Component
    @ConfigurationProperties(“admin”)
    public class User{

    1. private String username;
    2. private String password;

    }

  4. @PropertySource指定配置文件

    @Component
    @ConfigurationProperties(“admin”)
    @PropertySouce(value={

    1. "classpath:account.properties"}, ignoreResourceNotFound=true)

    public class User{

    1. private String username;
    2. private String password;

    }

  5. @Conditional条件装配Bean

1.5 Bean的作用域

在IOC容器的顶级接口BeanFactory中,有isSingleton和isPrototype两个方法。分别表示bean在容器中以单例存在和每次获取bean的时候,IOC容器都创建一个新的Bean。在web容器中,存在多种所用域。









































作用域类型 使用范围 描述
singleton 所有Spring应用 默认值,IOC容器只存在单例
prototype 所有Spring应用 每当从IOC容器中取出一个bean,则拆改那就一个新的bean
session Spring Web应用 HTTP会话
application Spring Web应用 Web工程生命周期,完全可用singleton代替
request Spring Web应用 Web工程单次请求
globalSession Spring Web应用 在一个全局的HTTP Session中,一个Bean定义对应一个实例
  1. @Scope配置作用域

    @Componment
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class ScopeBean{

    1. }

在ConfigurableBeanFactory中只有singleton和prototype两种作用域,在SpringMVC中可以使用WebApplicationContext来配置其他作用域。

  1. @Componment
  2. @Scope(WebApplicationContext.SCOPE_REQUEST)
  3. public class ScopeBean{
  4. }

1.6 使用@Profile指定bean的配置文件

在实际开发中,有开发环境、测试环境、生产环境等,各个环境下的配置文件经常要求不相同。

UTOOLS1578039572205.png

  1. @Componment
  2. //使用上图中的application-dev.yml配置bean
  3. @Profile("dev")
  4. public class User{
  5. }

在使用@Profile指定应用环境时,需要配置spring.profiles.active或者spring.profiles.default来指定当前的运行环境。当运行环境符合@Profile配置的环境时才装配bean属性,否则不装配属性。

1.7 使用XML文件配置Bean

在Springboot中推荐使用配置文件properties或者yaml来配置bean,同样也兼容spring中的使用xml文件配置bean。可以使用@ImportResource注解来指定使用xml文件配置bean。

  1. //没有@component
  2. public class User{
  3. }

xml配置文件spring-other.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.s3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframeword.org/schema/beans/spring-beans.xsd">
  5. <bean id="user" class="xyz.mxlei.pojo.User"/>
  6. </beans>

在Java配置文件中载入xml配置文件

  1. @Configuration
  2. @ComponentScan(basePackages = "xyz.mxlei.server.*")
  3. @ImportResource(value = {
  4. "classpath:spring-other.xml"})
  5. public class AppConfig{
  6. }

1.8 使用Spring EL

  1. $表示读取上下文的属性值
  2. #表示启用Spring表达式
  3. T表示使用引入的类

    publiC class User{

    1. @Value(${
    2. admin.username})
    3. String username;
    4. @Value(#{
    5. T{
    6. System}.currentTimeMillis()})
    7. Long createTime;
    8. @Value(#{
    9. 3.14})
    10. private float pi;

    }

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring-ioc-1

    1.IOC和ID的区别:   控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。