@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 "";
}
@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
@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就不會出異常了
@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
@Documented
public @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 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不支持自動依賴注入。