常用注解:
1.@Controller 控制器
用于標(biāo)注控制層,相當(dāng)于struts中的action層
2.@Service 服務(wù)
用于標(biāo)注服務(wù)層,主要用來(lái)進(jìn)行業(yè)務(wù)的邏輯處理
3.@Repository
用于標(biāo)注數(shù)據(jù)訪問(wèn)層,也可以說(shuō)用于標(biāo)注數(shù)據(jù)訪問(wèn)組件,即DAO組件
4.@Component (把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的<bean id="" class=""/>)
泛指各種組件,就是說(shuō)當(dāng)我們的類不屬于各種歸類的時(shí)候(不屬于@Controller、@Services等的時(shí)候),我們就可以使用@Component來(lái)標(biāo)注這個(gè)類。
用法:都是標(biāo)注在類名上,用于注冊(cè)一個(gè)bean到Spring上下文中。
區(qū)別:@Service 用于服務(wù)層;@Controller用于控制層;@Repository用于DAO層;不確定的用@Component
@Autowire和@Resource都是Spring支持的注解方式動(dòng)態(tài)裝配bean。
5.@Autowire默認(rèn)按照類型(by-type)裝配,默認(rèn)情況下要求依賴對(duì)象必須存在。
-如果允許依賴對(duì)象為null,需設(shè)置required屬性為false,即
@Autowire(required=false)
private InjectionBean beanName;
-如果使用按照名稱(by-name)裝配,需結(jié)合@Qualifier注解使用,即
@Autowire
@Qualifier("beanName")
private InjectionBean beanName;
6.@Resource默認(rèn)按照名稱(by-name)裝配,名稱可以通過(guò)name屬性指定。
-如果沒(méi)有指定name
1.當(dāng)注解在字段上時(shí),默認(rèn)取name=字段名稱裝配。
2.當(dāng)注解在setter方法上時(shí),默認(rèn)取name=屬性名稱裝配。
-當(dāng)按照名稱(by-name)裝配未匹配時(shí),按照類型(by-type)裝配。
1.當(dāng)顯示指定name屬性后,只能按照名稱(by-name)裝配。
-@Resoure裝配順序
如果同時(shí)指定name和type屬性,則找到唯一匹配的bean裝配,未找到則拋異常;
如果指定name屬性,則按照名稱(by-name)裝配,未找到則拋異常;
如果指定type屬性,則按照類型(by-type)裝配,未找到或者找到多個(gè)則拋異常;
既未指定name屬性,又未指定type屬性,則按照名稱(by-name)裝配;如果未找到,則按照類型(by-type)裝配。
下面通過(guò)一個(gè)實(shí)例講解如何使用這些注解
目錄結(jié)構(gòu)

1.創(chuàng)建Dao層
在ch3應(yīng)用的src中,創(chuàng)建annotation.Dao包,該包下創(chuàng)建TestDao接口和TestDaoImpl實(shí)現(xiàn)類,并將實(shí)現(xiàn)類TestDaoImpl使用@Repository注解標(biāo)注為數(shù)據(jù)訪問(wèn)層。
TestDao.java
package annotation.dao;
public interface TestDao {
public void save();
}
TestDaoImpl.java
package annotation.dao;
import org.springframework.stereotype.Repository;
@Repository("testDao")
public class TestDaoImpl implements TestDao {
public void save() {
System.out.println("testDao save");
}
}
2.創(chuàng)建Service層
在ch3應(yīng)用的src中,創(chuàng)建annotation.service包,該包下創(chuàng)建TestService接口和TestServiceImpl實(shí)現(xiàn)類,并將實(shí)現(xiàn)類TestServiceImpl使用@Service注解表注為業(yè)務(wù)邏輯層。
TestService.java
package annotation.service;
public interface TestService {
public void save();
}
TestServiceImpl
package annotation.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import annotation.dao.TestDao;
@Service("testService")
public class TestServiceImpl implements TestService{
@Resource(name="testDao")
private TestDao testDao;
public void save() {
testDao.save();
System.out.println("testService save");
}
}
3.創(chuàng)建Controller層
在ch3應(yīng)用的src中,創(chuàng)建annotation.controller包,該包下創(chuàng)建TestController類使用@Controller注解標(biāo)注為控制器層。
TestController.java
package annotation.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import annotation.dao.TestDao;
@Service("testService")
public class TestServiceImpl implements TestService{
@Resource(name="testDao")
private TestDao testDao;
public void save() {
testDao.save();
System.out.println("testService save");
}
}
4.配置注解
由于annotation.dao、annotation.service和annotation.controller包都屬于annotation包的子包,因此,不需要在配置annotationContext.xml中配置注解
annotationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用context命名空間,通過(guò)Spring掃描指定包下所有的Bean實(shí)現(xiàn)類,通過(guò)注釋解析 -->
<context:component-scan base-package="annotation"/>
</beans>
5.創(chuàng)建測(cè)試類
在ch3應(yīng)用的test包中,創(chuàng)建測(cè)試類
TestMoreAnnotation.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import annotation.AnnotationUser;
import annotation.controller.TestController;
public class TestMoreAnnotation {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml");
TestController testcon = (TestController)appCon.getBean("testController");
testcon.save();
}
}
運(yùn)行結(jié)果
