1. 程式人生 > >spring @component的作用

spring @component的作用

repo scan 組件 普通 class 方法 pub spring 初始

該文轉載自:http://tomfish88.iteye.com/blog/1497557

[email protected] 控制器(註入服務)
[email protected] 服務(註入dao)
[email protected] dao(實現dao訪問)
[email protected] (把普通pojo實例化到spring容器中,相當於配置文件中的<bean id="" class=""/>)


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

其中base-package為需要掃描的包(含所有子包)
[email protected]
/* */
[email protected](如struts中的action)
[email protected],即DAO組件.
[email protected],當組件不好歸類的時候,我們可以使用這個註解進行標註。
@Service public class UserServiceImpl implements UserService { }
@Repository public class UserDaoImpl implements UserDao { } getBean的默認名稱是類名(頭字母小寫),如果想自定義,[email protected]
/* */(“***”) 這樣來指定,這種bean默認是單例的,如果想改變,[email protected](“beanName”)
@Scope(“prototype”)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @PostConstruct public void init() { }

spring @component的作用