關(guān)于@Component注解的含義說明_第1頁
關(guān)于@Component注解的含義說明_第2頁
關(guān)于@Component注解的含義說明_第3頁
關(guān)于@Component注解的含義說明_第4頁
關(guān)于@Component注解的含義說明_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第關(guān)于@Component注解的含義說明目錄一、注解分類二、@Component注解含義三、基于@Component擴展的注解四、component的掃描組件五、方法初始化和銷毀1.@PostConstruct注解2.@PreDestroy注解3.示例

一、注解分類

1、@controller:controller控制器層(注入服務(wù))

2、@service:service服務(wù)層(注入dao)

3、@repository:dao持久層(實現(xiàn)dao訪問)

4、@component:標注一個類為Spring容器的Bean,(把普通pojo實例化到spring容器中,相當于配置文件中的beanid=""class=""/)

二、@Component注解含義

@Component:標注Spring管理的Bean,使用@Component注解在一個類上,表示將此類標記為Spring容器中的一個Bean。

三、基于@Component擴展的注解

@Repository:

@Repository本身是基于@Component注解的擴展,被@Repository注解的POJO類表示DAO層實現(xiàn),從而見到該注解就想到DAO層實現(xiàn),使用方式和@Component相同;

@Service:

@Service本身是基于@Component注解的擴展,被@Service注解的POJO類表示Service層實現(xiàn),從而見到該注解就想到Service層實現(xiàn),使用方式和@Component相同;

@Controller:

@Controller本身是基于@Component注解的擴展,被@Controller注解的類表示W(wǎng)eb層實現(xiàn),從而見到該注解就想到Web層實現(xiàn),使用方式和@Component相同;

Tips:使用@Component,@Service,@Controller,@Repository注解的類,表示把這些類納入到spring容器中進行管理,同時也是表明把該類標記為Spring容器中的一個Bean。

四、component的掃描組件

下面寫這行配置是引入component的掃描組件

context:component-scanbase-package=”com.mmnc”

其中base-package為指定需要掃描的包(含所有子包),掃描被@Service、@Controller、@Repository、@Component等注解標注的Java類,將其掃描注入到Spring容器,注入成Bean:

@Service用于標注業(yè)務(wù)層組件

@Controller用于標注控制層組件(如struts中的action)

@Repository用于標注數(shù)據(jù)訪問組件,即DAO組件.

@Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注,標識為一個Bean。

五、方法初始化和銷毀

從JavaEE5規(guī)范開始,Servlet增加了兩個影響Servlet生命周期的注解:@PostConstruct和@PreConstruct,這兩個注解被用來修飾一個非靜態(tài)的void()方法,而且這個方法不能有拋出異常聲明,標注方法的初始化和銷毀,當你需要在系統(tǒng)啟動時提前設(shè)置一下變量或者設(shè)置值操作時,可以使用@PostConstruct注解進行項目啟動時設(shè)置來完成,當你需要處理關(guān)閉資源或者發(fā)送通知相關(guān)操作時可以使用@PreConstruct完成。

1.@PostConstruct注解

被@PostConstruct修飾的方法會在服務(wù)器加載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次,類似于Servlet的inti()方法。被@PostConstruct修飾的方法會在構(gòu)造函數(shù)之后,init()方法之前運行。

2.@PreDestroy注解

被@PreDestroy修飾的方法會在服務(wù)器卸載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次,類似于Servlet的destroy()方法。被@PreDestroy修飾的方法會在destroy()方法之后運行,在Servlet被徹底卸載之前。

加載順序如圖:

在Spring中可以使用以下方式指定初始化方法和銷毀方法(方法名任意):

//對象創(chuàng)建并賦值之后調(diào)用

@PostConstruct

publicvoidinit(){

System.out.println("對象創(chuàng)建并賦值之后調(diào)用...");

}

//容器移除對象之前

@PreDestroy

publicvoiddetory(){

System.out.println("容器移除對象之前執(zhí)行...");

}

3.示例

@Controller

@Lazy(false)

publicclassServletInit{

@Autowired

privatePCASigninServletpcaSigninServlet;

@Autowired

privatePCASignoutServletpcaSignoutServlet;

@Autowired

privatePCAInitServletpcaInitServlet;

@Autowired

privatePCALogoutServletpcaLogoutServlet;

@Autowired

privatePCAInfoServletpcaInfoServlet;

@Autowired

privateHelloServlethelloServlet;

@Autowired@Qualifier("handler")

privateWebAppContextwebapp;

@PostConstruct

voidinit(){

Serverserver=newServer(8848);

ServletContextHandlercontextHander=newServletContextHandler(ServletContextHandler.SESSIONS);

contextHander.setContextPath("/");

System.out.println("startinit");

HandlerCollectionhc=newHandlerCollection();

hc.setHandlers(newHandler[]{webapp,contextHander});

server.setHandler(hc);

System.out.println("addServlet/hello");

contextHander.addServlet(newServletHolder(helloServlet),"/hello");

contextHander.addServlet(newServletHolder(pcaSigninServlet),"/pca/signin");

contextHander.addServlet(newServletHolder(pcaSignoutServlet),"/pca/signout");

contextHander.addServlet(newServletHolder(pcaInitServlet),"/pca/init");

contextHander.addServlet(newServletHolder(pcaLogoutServlet),"/pca/logout");

contextHander.addServlet(newServletHolder(pcaInfoServlet),"/pca/info");

System.out.p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論