本文问题:
1. @Scope是做什么的?常见的用法有几种?
2. @DependsOn是做什么的?常见的用法有几种?
3. @ImportResource干什么的?通常用在什么地方?
4. @Lazy做什么的,通常用在哪些地方?常见的用法有几种?
1,@Scope:指定bean的作用域
@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;
}
用法:
1,和@Compontent一起使用在类上
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ServiceA {
}
2,和@Bean一起标注在方法上
@Configurable
public class Main {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public ServiceA serviceA() {
return new ServiceA();
}
}