spring IOC annotation

  1. @Target(ElementType.TYPE)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. public @interface Component {  
  5.   
  6.     /** 
  7.      * The value may indicate a suggestion for a logical component name, 
  8.      * to be turned into a Spring bean in case of an autodetected component. 
  9.      * @return the suggested component name, if any 
  10.      */  
  11.     String value() default "";  
  12.   
  13. }  
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @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 "";

}
  1. @Target({ElementType.TYPE})  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. @Component  
  5. public @interface Repository {  
  6.   
  7.     /** 
  8.      * The value may indicate a suggestion for a logical component name, 
  9.      * to be turned into a Spring bean in case of an autodetected component. 
  10.      * @return the suggested component name, if any 
  11.      */  
  12.     String value() default "";  
  13.   
  14. }  
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @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

  1. @Target({ElementType.TYPE, ElementType.METHOD})  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. public @interface Scope {  
  5.   
  6.     /** 
  7.      * Specifies the scope to use for the annotated component/bean. 
  8.      * @return the specified scope 
  9.      */  
  10.     String value() default BeanDefinition.SCOPE_SINGLETON;  
  11.   
  12.     /** 
  13.      * Specifies whether a component should be configured as a scoped proxy 
  14.      * and if so, whether the proxy should be interface-based or subclass-based. 
  15.      * <p>Defaults to {@link ScopedProxyMode#NO}, indicating that no scoped 
  16.      * proxy should be created. 
  17.      * <p>Analogous to {@literal <aop:scoped-proxy/>} support in Spring XML. 
  18.      */  
  19.     ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;  
  20.   
  21. }  
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @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就不会出异常了

  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})  
  3. public @interface Autowired {  
  4.   
  5.     /** 
  6.      * Declares whether the annotated dependency is required. 
  7.      * <p>Defaults to <code>true</code>. 
  8.      */  
  9.     boolean required() default true;  
  10.   
  11. }  
@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配合使用

  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})  
  3. @Inherited  
  4. @Documented  
  5. public @interface Qualifier {  
  6.   
  7.     String value() default "";  
  8.   
  9. }  
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Inherited
@Documented
public @interface Qualifier {

	String value() default "";

}

 

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

  1. @Target({TYPE, FIELD, METHOD})  
  2. @Retention(RUNTIME)  
  3. public @interface Resource {  
@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属性指定扫描的路径,那么该包以及下面所有的子包中的类都会被扫描

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

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

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

支持的过滤器类型:

Filter Type Example Expression Description
annotation org.example.SomeAnnotation An annotation to be present at the type level in target components.
assignable org.example.SomeClass A class (or interface) that the target components are assignable to (extend/implement).
aspectj org.example..*Service+ An AspectJ type expression to be matched by the target components.
regex org\.example\.Default.* A regex expression to be matched by the target components class names.
custom org.example.MyTypeFilter A custom implementation of the org.springframework.core.type .TypeFilter interface.

练习时请使用ApplicationContext,因为有些BeanFacory不支持自动依赖注入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值