Spring annotation2

Spring2

1 spring的IOC container(2)

1.3 spring Annotations

前提在一个applicatcontext中打开注解扫描器配置:

<context:annotation-config/>
Note
<context:annotation-config/> only looks for annotations on beans in the same application
context in which it is defined. This means that, if you put <context:annotation-config/>in a WebApplicationContext for a DispatcherServlet, it only checks for @Autowired
beans in your controllers, and not your services. See Section 17.2, “The DispatcherServlet” for
more information.
在哪个容器中打开就扫描相应的注解!

@Required :The @Required annotation applies to bean property setter methods, as in the following

@Autowired

单个方法参数注入
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
    this.movieCatalogs = movieCatalogs;
}
多个方法参数注入
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;

}

如果你在一个类的多个构造器使用这个注解,spring只能搜寻参数最多的一个使用,故官方做法只标注一个构造去就好

为一个接口类注入实例
@Autowired
private ApplicationContext context;

@Autowired, @Inject, @Resource, and @Value annotations are handled by a Spring BeanPostProcessor implementations which in turn means that you cannot apply these annotations within your own BeanPostProcessor or BeanFactoryPostProcessor types (if any). These types must be wired up explicitly via XML or using a Spring @Bean method.


@Qualifier : 当你用@Autowired去注入一个实例的时候往往会筛选出好多的备选对象,这时候spring提供舒适的服务,配置这个注解,做更精确的筛选(从备选中筛选出id为制定的对象)!

public class MovieRecommender {
    @Autowired
    @Qualifier("main"),
    private MovieCatalog movieCatalog;
// ...
}
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;
    }
// ...
}

当你用bean 定义的name进行注入的时候及不能用@Autowired了,改换@Resource,还有注入Map Collection类型的时候也不能用@Autowired

@Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level

@Resource is supported only for fields and bean property setter methods with a single argument.

自定义

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {
    String value();
}


public class MovieRecommender {
    @Autowired
    @Genre("Action")
    private MovieCatalog actionCatalog;
    private MovieCatalog comedyCatalog;
    @Autowired
    public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
        this.comedyCatalog = comedyCatalog;
    }
// ...
}

@Resource : 通过bean注册的name查找引用实例

public class SimpleMovieLister {
    private MovieFinder movieFinder;
    @Resource(name="myMovieFinder")
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}

不指定name就是给根据set方法后面去取

public class SimpleMovieLister {
    private MovieFinder movieFinder;
    @Resource
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}

这个等价于 @Resource(name="movieFinder")

The name provided with the annotation is resolved as a bean name by the ApplicationContext of which the CommonAnnotationBeanPostProcessor is aware.

不显示指定名称处理工程跟@Autowired一样了,按照类型找出匹配的对象

@Configuration, @Bean, @Import, and @DependsOn 这些java的注解也可以使用了!


Spring stereotype annotations

@Component, @Service, and @Controller  @Repository


<context:component-scan>开启后。没必要使用<context:annotation-config>这个了 ,他包括了后者的注解

AutowiredAnnotationBeanPostProcessor
and
CommonAnnotationBeanPostProcessor are both included implicitly when you use the component-
scan element. That means that the two components are autodetected and wired together - all without
any bean configuration metadata provided in XML.


Using filters to customize scanning

@Configuration
@ComponentScan(basePackages = "org.example",
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
excludeFilters = @Filter(Repository.class))
public class AppConfig {
...
}

或者
<beans>
<context:component-scan base-package="org.example">
<context:include-filter type="regex"
expression=".*Stub.*Repository"/>
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>

Defining bean metadata within components

@Component
public class FactoryMethodComponent {
@Bean
@Qualifier("public")
public TestBean publicInstance() {
    return new TestBean("publicInstance");
}
public void doWork() {
// Component method implementation omitted
}
}

Using JSR 330 Standard Annotations

标准注解

Dependency Injection with @Inject and @Named Instead of @Autowired, @javax.inject.Inject may be used as follows:

import javax.inject.Inject;
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
}
// ...
}

As with @Autowired, it is possible to use @Inject at the class-level, field-level, method-level and
constructor-argument level. If you would like to use a qualified name for the dependency that should be
injected, you should use the @Named annotation as follows:

import javax.inject.Inject;
import javax.inject.Named;
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(@Named("main") MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
}
// ...
}


@Named: a standard equivalent to the @Component annotation


instead of @Component, @javax.inject.Named may be used as follows:

import javax.inject.Inject;
import javax.inject.Named;
@Named("movieListener")
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
}
// ...
}



你可能感兴趣的:(spring,Annotations)