spring 各個註解的作用

來源:互聯網
上載者:User
1、@controller 控制器(注入服務)2、@service 服務(注入dao)3、@repository dao(實現dao訪問)4、@component (把普通pojo執行個體化到spring容器中,相當於設定檔中的<bean id="" class=""/>)


  @Component,@Service,@Controller,@Repository註解的類,並把這些類納入進spring容器中管理。 
下面寫這個是引入component的掃描組件 
<context:component-scan base-package=”com.mmnc”>    

其中base-package為需要掃描的包(含所有子包) 
       1、@Service用於標註業務層組件 
       2、@Controller用於標註控制層組件(如struts中的action) 
       3、@Repository用於標註資料訪問組件,即DAO組件. 
       4、@Component泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。    
           @Service public class UserServiceImpl implements UserService { } 
           @Repository public class UserDaoImpl implements UserDao { } getBean的預設名稱是類名(頭字母小寫),如果想自訂,可以@Service(“***”)               這樣來指定,這種bean預設是單例的,如果想改變,可以使用@Service(“beanName”) 
           @Scope(“prototype”)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @PostConstruct public void init() { } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.