1.注解創(chuàng)建對(duì)象
步驟:
A. 在配置文件中,引入context命名空間
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring- beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
2.<context:component-scan base-package="com.hh"/>
掃描
@Component、@Service、@Controller、@Repository注解的類,并把這些類納入進(jìn)spring容器
中管理。它的作用和在xml文件中使用bean節(jié)點(diǎn)配置組件是一樣的。要使用自動(dòng)掃描機(jī)制,我們需
要打開(kāi)以下配置信息:
1、引入context命名空間 需要在xml配置文件中配置以下信息:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.hh"/>
</beans>
2、在配置文件中添加context:component-scan標(biāo)簽
<context:component-scan base-package="com.hh"/>
其中base-package為需要掃描的包(含子包)。
注:
1、在使用組件掃描元素時(shí),AutowiredAnnotationBeanPostProcessor 和
CommonAnnotationBeanPostProcessor會(huì)隱式地被包括進(jìn)來(lái)。 也就是說(shuō),連個(gè)組件都會(huì)被自動(dòng)檢
測(cè)并織入 - 所有這一切都不需要在XML中提供任何bean配置元數(shù)據(jù)。
2、功能介紹
@Service用于標(biāo)注業(yè)務(wù)層組件、
@Controller用于標(biāo)注控制層組件(如struts中的action)、
@Repository用于標(biāo)注數(shù)據(jù)訪問(wèn)組件,即DAO組件。
@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。
3.注解注入屬性
如果注解不創(chuàng)建對(duì)象
在配置文件中加入context:annotation-config標(biāo)簽
<context:annotation-config/>
這個(gè)配置隱式注冊(cè)了多個(gè)對(duì)注釋進(jìn)行解析處理的處理器
AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor,
RequiredAnnotationBeanPostProcessor
注:
@Resource
@Autowired
這兩個(gè)注解的區(qū)別是:
@Autowired 默認(rèn)按類型裝配,
@Resource默認(rèn)按名稱裝配,當(dāng)找不到與名稱匹配的bean才會(huì)按類型裝配。
@Autowired注解是按類型裝配依賴對(duì)象,默認(rèn)情況下它要求依賴對(duì)象必須存在,如果允許null值
可以設(shè)置它required屬性為false。
@Qualifier
如果我們想使用按名稱裝配,可以結(jié)合@Qualifier注解一起使用。
@Autowired
@Qualifier
@Resource
1、@Resource注解和@Autowired一樣,也可以標(biāo)注在字段或?qū)傩缘膕etter方法上.
? 當(dāng)注解標(biāo)注在字段上,即默認(rèn)取字段的名稱作為bean名稱尋找依賴對(duì)象
? 當(dāng)注解標(biāo)注在屬性的setter方法上,即默認(rèn)取屬性名作為bean名稱尋找依賴對(duì)象。
2、@Resource注解默認(rèn)按名稱裝配。
名稱可以通過(guò)@Resource的name屬性指定,如果沒(méi)有指定name屬性,
?注意:如果沒(méi)有指定name屬性,并且按照默認(rèn)的名稱找不到依賴對(duì)象時(shí),
@Resource注解會(huì)回退到按類型裝配。但一旦指定了name屬性,就只能按名稱裝配了。
@PostConstruct
指定Bean的初始化方法
@PreDestroy
指定Bean的銷毀方法
注解的過(guò)程
* 1、啟動(dòng)spring容器
* 2、把spring配置文件中的bean實(shí)例化(person,student)
* 3、當(dāng)spring容器解析配置文件
* <context:annotation-config></context:annotation-config>
* spring容器會(huì)在納入spring管理的bean的范圍內(nèi)查找哪些類的屬性上是否加有@Resource注解
* 4、如果在屬性上找到@Resource注解
* 如果@Resource的注解的name屬性的值為""
* 則把@Resource所在的屬性的名稱和spring容器中的id作匹配
* 如果匹配成功,則賦值
* 如果匹配不成功,則會(huì)按照類型進(jìn)行匹配
* 如果匹配成功,則賦值,匹配不成功,報(bào)錯(cuò)
* 如果@Resource的注解的name屬性的值不為""
* 則解析@Resource注解name屬性的值,把值和spring容器中的ID進(jìn)行匹配
* 如果匹配成功,則賦值
* 如果匹配不成功,則報(bào)錯(cuò)
自定義注解
@Target(ElementType.TYPE)//該注解能夠作用在類上
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassInfo {
String name() default "";
}
@Target(ElementType.METHOD)//該注解能夠作用于方法上
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo {
String name() default "";
}
@ClassInfo(name="我")
public class ItheimaCloud11 {
@MethodInfo(name="你")
public void java(){
}
}
注解解析器
public class AnnotationParse {
public static void parse(){
Class class1 = ItheimaCloud11.class;
//判斷該類上面是否有ClassInfo注解
if(class1.isAnnotationPresent(ClassInfo.class)){
ClassInfo classInfo = (ClassInfo)class1.getAnnotation(ClassInfo.class);
System.out.println(classInfo.name());
}
Method[] methods = class1.getMethods();
for (Method method : methods) {
//判斷當(dāng)前正在遍歷的方法上面是否存在MethodInfo注解
if(method.isAnnotationPresent(MethodInfo.class)){
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
System.out.println(methodInfo.name());
}
}
}
@Test
public void test(){
AnnotationParse.parse();
}