spring IOC annotation

来源:互联网 发布:unity3d ngui 实例 编辑:程序博客网 时间:2024/06/06 18:11
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Component {/** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */String value() default "";}
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Repository {/** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */String value() default "";}


@Component("userDao")注解在类上,表示定义了一个bean,id为userDao,和此注解类似的还有:@Repository,@Service,@Controller用来注解dao层,业务逻辑层和控制层,他们都会被解析成bean,只是后面几个spring会赋予一些特殊功能,所以写的时候根据类的作用写不同的注解

要在定义bean的时候,给bean定义scope那么就再添加一个注解@Scope

@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Scope {/** * Specifies the scope to use for the annotated component/bean. * @return the specified scope */String value() default BeanDefinition.SCOPE_SINGLETON;/** * Specifies whether a component should be configured as a scoped proxy * and if so, whether the proxy should be interface-based or subclass-based. * <p>Defaults to {@link ScopedProxyMode#NO}, indicating that no scoped * proxy should be created. * <p>Analogous to {@literal <aop:scoped-proxy/>} support in Spring XML. */ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;}

要实现bean的装配,那么使用@Autowired,@Qualifier,@Resource

@Autowired 默认按类型进行装配,如果找不到就出异常,required属性定义为false就不会出异常了

@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})public @interface Autowired {/** * Declares whether the annotated dependency is required. * <p>Defaults to <code>true</code>. */boolean required() default true;}

如果容器中有一个以上的bean匹配,那么使用@Qualifier限定Bean的名称,和@Autowired配合使用

@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})@Inherited@Documentedpublic @interface Qualifier {String value() default "";}

 

@Resource和@Autowired类似,不过@Resource是javax.annotation包下的

@Target({TYPE, FIELD, METHOD})@Retention(RUNTIME)public @interface Resource {

@Resource("car") 找id为car的bean注入,这个注解要求提供一个bean名称的属性,如果没有指定,那么默认采用标注处的变量名或方法名作为bean的名称。与@Resource雷同的还有@Inject,很少使用,建议用@Autowired

@Resource装配顺序
如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;

 

在类中定义了这些注解后,要在配置文件中使用context命名空间中的component-scan的base-package属性指定扫描的路径,那么该包以及下面所有的子包中的类都会被扫描

<context:component-scan base-package="com.baobaotao.anno"/>

这个元素里可以配置扫描哪些类,排除哪些类

<context:component-scan base-package="com.auscend"><!-- 安全性配置,如果启用安全性,请注释掉 --><context:exclude-filter type="regex" expression="com.auscend.secure.*"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
<context:component-scan base-package="com.baobaotao">       <context:include-filter type="regex" expression="com\.baobaotao\.anno.*Dao"/>       <context:include-filter type="regex" expression="com\.baobaotao\.anno.*Service"/>       <context:exclude-filter type="aspectj" expression="com.baobaotao..*Controller+"/></context:component-scan>
<context:component-scan base-package="com.baobaotao" resource-pattern="anno/*.class"/>

支持的过滤器类型:

Filter TypeExample ExpressionDescriptionannotationorg.example.SomeAnnotationAn annotation to be present at the type level in target components.assignableorg.example.SomeClassA class (or interface) that the target components are assignable to (extend/implement).aspectjorg.example..*Service+An AspectJ type expression to be matched by the target components.regexorg\.example\.Default.*A regex expression to be matched by the target components class names.customorg.example.MyTypeFilterA custom implementation of the org.springframework.core.type .TypeFilter interface.
练习时请使用ApplicationContext,因为有些BeanFacory不支持自动依赖注入。