Spring 2.5 中除了提供 @Component 注釋外,還定義了幾個擁有特殊語義的注釋,它們分別是:@Repository、@Service 和 @Controller。
在目前的 Spring 版本中,這 3 個注釋和 @Component 是等效的,但是從注釋類的命名上,很容易看出這 3 個注釋分別和持久層、業(yè)務(wù)層和控制層(Web 層)相對應(yīng)。
雖然目前這3 個注釋和 @Component 相比沒有什么新意,但 Spring 將在以后的版本中為它們添加特殊的功能。
所以,如果 Web 應(yīng)用程序采用了經(jīng)典的三層分層結(jié)構(gòu)的話,最好在持久層、業(yè)務(wù)層和控制層分別采用上述注解對分層中的類進(jìn)行注釋。
@Service用于標(biāo)注業(yè)務(wù)層組件
@Controller用于標(biāo)注控制層組件(如struts中的action)
@Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件
@Component泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。
- @Service
- public class VentorServiceImpl implements iVentorService {
- }
- @Repository
- public class VentorDaoImpl implements iVentorDao {
- }
@Service
public class VentorServiceImpl implements iVentorService {
}
@Repository
public class VentorDaoImpl implements iVentorDao {
}
在一個稍大的項目中,如果組件采用xml的bean定義來配置,顯然會增加配置文件的體積,查找以及維護(hù)起來也不太方便。
Spring2.5為我們引入了組件自動掃描機(jī)制,他在類路徑下尋找標(biāo)注了上述注解的類,并把這些類納入進(jìn)spring容器中管理。
它的作用和在xml文件中使用bean節(jié)點(diǎn)配置組件時一樣的。要使用自動掃描機(jī)制,我們需要打開以下配置信息:
代碼
- <?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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config />
- <context:component-scan base-package=”com.eric.spring”>
- </beans>
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package=”com.eric.spring”> </beans>
1.annotation-config是對標(biāo)記了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的類進(jìn)行對應(yīng)的操作使注解生效。
2.base-package為需要掃描的包(含所有子包),負(fù)責(zé)掃描那些類有注解。
getBean的默認(rèn)名稱是類名(頭字母小寫),如果想自定義,可以@Service(“aaaaa”)這樣來指定。
這種bean默認(rèn)是“singleton”的,如果想改變,可以使用@Scope(“prototype”)來改變。
可以使用以下方式指定初始化方法和銷毀方法:
- public void init() {
- }
- @PreDestroy
- public void destory() {
- }
@PostConstruct
public void init() {
}
@PreDestroy
public void destory() {
}
注入方式:
把DAO實(shí)現(xiàn)類注入到action的service接口(注意不要是service的實(shí)現(xiàn)類)中,注入時不要new 這個注入的類,因?yàn)閟pring會自動注入,如果手動再new的話會出現(xiàn)錯誤,
然后屬性加上@Autowired后不需要getter()和setter()方法,Spring也會自動注入。
在接口前面標(biāo)上@Autowired注釋使得接口可以被容器注入,如:
- @Qualifier("chinese")
- private Man man;
@Autowired
@Qualifier("chinese")
private Man man;
當(dāng)接口存在兩個實(shí)現(xiàn)類的時候必須使用@Qualifier指定注入哪個實(shí)現(xiàn)類,否則可以省略,只寫@Autowired。
浙公網(wǎng)安備 33010602011771號