@Component注解的含義
一、注解分類
1、@controller: controller控制器層(注入服務)
2、@service : service服務層(注入dao)
3、@repository : dao持久層(實現dao訪問)
4、@component: 標注一個類為Spring容器的Bean,(把普通pojo實例化到spring容器中,相當於配置文件中的) 1、@Service用於標注業務層組件
2、@Controller用於標注控制層組件(如struts中的action)
3、@Repository用於標注數據訪問組件,即DAO組件.
4、@Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注,標識為一個Bean。
二、@Component 注解含義
@Component: 標注Spring管理的Bean,使用@Component注解在一個類上,表示將此類標記為Spring容器中的一個Bean。
三、基於@Component擴展的注解
@Repository:
@Component注解的擴展,被@Repository注解的POJO類表示DAO層實現,從而見到該注解就想到DAO層實現,使用方式和@Component相同;
@Service:
@Component注解的擴展,被@Service注解的POJO類表示Service層實現,從而見到該注解就想到Service層實現,使用方式和@Component相同;
@Controller:
@Component注解的擴展,被@Controller注解的類表示Web層實現,從而見到該注解就想到Web層實現,使用方式和@Component相同;
使用@Component,@Service,@Controller,@Repository注解的類,表示把這些類納入到spring容器中進行管理,同時也是表明把該類標記為Spring容器中的一個Bean。
四、component的掃描組件
下面寫這個是引入component的掃描組件
<context:component-scan base-package=”com.mmnc”>
其中base-package為需要掃描的包(含所有子包)
可以使用以下方式指定初始化方法和銷毀方法(方法名任意):
@PostConstruct
public void init() { }