@Scope、@DependsOn、@ImportResource、@Lazy 详解

@Scope用来配置bean的作用域,等效于bean xml中的bean元素中的scope属性

查看源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {

    @AliasFor("scopeName")
    String value() default "";

    @AliasFor("value")
    String scopeName() default "";

    ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;

}

常见2种用法分别在实现对象的注册中
1、和@Compontent一起使用在类上
2、和@Bean一起标注在方法上
与@Component一起使用作用在类上

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)//@1
public class ServiceA {
}

不同的枚举值于分别代表作用域

上面定义了一个bean,作用域为单例的。
@1:ConfigurableBeanFactory接口中定义了几个作用域相关的常量,可以直接拿来使用,如:
String SCOPE_SINGLETON = "singleton";
String SCOPE_PROTOTYPE = "prototype";

@Bean一起标注在方法

@Configurable
public class MainConfig2 {
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public ServiceA serviceA() {
        return new ServiceA();
    }
}

@DependsOn等效于bean xml中的bean元素中的depend-on属性

作用与bean之间相互依赖
@DependsOn可以指定当前bean依赖的bean,通过这个可以确保@DependsOn指定的bean在当前bean创建之前先创建好
源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DependsOn {

    String[] value() default {};

}

常见2种用法

1、和@Compontent一起使用在类上
2、和@Bean一起标注在方法上
和@Compontent一起使用在类上
定义3个bean:service1、service2、service3;service1需要依赖于其他2个service,需要确保容器在创建service1之前需要先将其他2个bean先创建好

import org.springframework.stereotype.Component;

@Component
public class Service2 {
    public Service2() {
        System.out.println("create Service2");
    }
}
import org.springframework.stereotype.Component;

@Component
public class Service3 {
    public Service3() {
        System.out.println("create Service3");
    }
}
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

@DependsOn({"service2", "service3"}) //@1
@Component
public class Service1 {
    public Service1() {
        System.out.println("create Service1");
    }
}

和@Bean一起标注在方法上,与之前作用域的方法很相似

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;

@Configurable
public class MainConfig4 {

    @Bean
    @DependsOn({"service2", "service3"})//@1
    public Service1 service1() {
        return new Service1();
    }

    @Bean
    public Service2 service2() {
        return new Service2();
    }

    @Bean
    public Service3 service3() {
        return new Service3();
    }

}

@ImportResource:配置类中导入bean定义的配置文件

个人理解,是一些项目可能有老的配置文件,但是现在开发时使用注解的方式,会用到一些配置文件的东西,所以使用@ImportResource注解

@Configurable
@ImportResource("classpath:/com/demo/test5/beans*.xml")
public class MainConfig5 {
}

会将该目录下的配置文件中bean注册到容器中

@Lazy:延迟初始化

@Lazy等效于bean xml中bean元素的lazy-init属性,可以实现bean的延迟初始化。

所谓延迟初始化:就是使用到的时候才会去进行初始化

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {

    boolean value() default true;

}

通过源码可以

可以用在任意类型、方法、构造器、参数、字段上面
参数:
value:boolean类型,用来配置是否应发生延迟初始化,默认为true

常用3种方式

1、和@Compontent一起标注在类上,可以是这个类延迟初始化
2、和@Configuration一起标注在配置类中,可以让当前配置类中通过@Bean注册的bean延迟初始化
3、和@Bean一起使用,可以使当前bean延迟初始化

和@Compontent一起


@Component
@Lazy //@1
public class Service1 {
    public Service1() {
        System.out.println("创建Service1");
    }
}

@Lazy //@1
@Configurable
public class MainConfig7 {

    @Bean
    public String name() {
        System.out.println("create bean:name");
        return "路人甲Java";
    }

    @Bean
    public String address() {
        System.out.println("create bean:address");
        return "上海市";
    }

    @Bean
    @Lazy(false) //@2
    public Integer age() {
        System.out.println("create bean:age");
        return 30;
    }
}
@1:配置类上使用了@Lazy,此时会对当前类中所有@Bean标注的方法生效
@2:这个方法上面使用到了@Lazy(false),此时age这个bean不会被延迟初始化。其他2个bean会被延迟初始化。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值