1.@Autowired注释用法
1.@Autowired 应用于构造函数(多个构造方法时必须有一个构造方法被@Autowired注释)
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
}
2.@autowired 应用于Setter方法
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
3.@Autowired注释应用于具有任意名称和/或多个参数的方法
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
}
4.@Autowired注释运用于字段,甚至与构造函数混合:
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
}
5.将@Autowired注释添加到需要该类型数组的字段或方法中,还可以从applicationContext中提供特定类型的所有bean。
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
}
6.@Autowired注释运用集合
public class MovieRecommender {
private Set<MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
}
2.@Qualifier注释用法
1.当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下你可以使用
@Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱
public class MovieRecommender {
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;
}
2.@Qualifier注解也可以在单独的构造函数参数或方法参数上指定
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
}
3.您可以创建自己的自定义限定符注释。只需定义一个注释并在定义中提供@Qualifier注释
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {
String value();
}
3.@Required注释用法
1.@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配 置文件中,
否则容器就会抛出一个 BeanInitializationException 异常.
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Required
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
4.@Resource注释用法
1.@Resource 注释可以在字段中或者 setter 方法中使用.@Resource 注释使用一个 ‘name’ 属性,该属性以
一个 bean 名称的形式被注入。你可以说,它遵循 by-name 自动连接语义
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
2.@Resource 注释可以在字段中或者 setter 方法中使用.如果不使用name属性,将采用默认的字段名,所以下面的示例将
把名为“moviefinder”的bean注入其setter方法中
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
5.@ComponentScan和@Configuration注释用法
1.要自动检测这些类并注册相应的bean,需要将@componentscan添加到@configuration类中,其中basepackages属性是
这两个类的通用父包。(或者,可以指定包含每个类的父包的逗号/分号/空格分隔列表。)
带@Bean注释的方法可以有任意数量的参数来描述构建该bean所需的依赖关系。例如,如果我们的TransferService需要
一个AccountRepository,我们可以通过一个方法参数来实现依赖关系:
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}
2.@Bean注释支持指定任意初始化和销毁回调方法,很像Spring XML在bean元素上的init-method和destroy-method属性
public class Foo {
public void init() {
// initialization logic
}
}
public class Bar {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")
public Bar bar() {
return new Bar();
}
}
3.您可以指定使用@Bean注释定义的bean应该具有特定的范围。您可以使用Bean作用域部分中指定的任何标准作用域。
默认范围是单例的,但是您可以使用@Scope注释覆盖它
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}
6.@Pointcut
1.切入点表达式可以使用“&&”、“||”和“!”组合
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}
@Pointcut("within(com.xyz.someapp.trading..*)")
private void inTrading() {}
@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}