一、@controller 控制器(注入服務)spring
二、@service 服務(注入dao)spa
三、@repository dao(實現dao訪問)prototype
四、@component (把普通pojo實例化到spring容器中,至關於配置文件中的<bean id="" class=""/>)code
@Component,@Service,@Controller,@Repository註解的類,並把這些類歸入進spring容器中管理。 component
下面寫這個是引入component的掃描組件 blog
<context:component-scan base-package=」com.mmnc」>
其中base-package爲須要掃描的包(含全部子包) get
一、@Service用於標註業務層組件
二、@Controller用於標註控制層組件(如struts中的action)
三、@Repository用於標註數據訪問組件,即DAO組件.
四、@Component泛指組件,當組件很差歸類的時候,咱們能夠使用這個註解進行標註。 it
@Service public class UserServiceImpl implements UserService { } @Repository public class UserDaoImpl implements UserDao { }
getBean的默認名稱是類名(頭字母小寫),若是想自定義,能夠@Service(「***」)這樣來指定,這種bean默認是單例的,若是想改變,能夠使用io
@Service(「beanName」) @Scope(「prototype」) public class User { }
來改變。能夠使用如下方式指定初始化方法和銷燬方法(方法名任意):class
@PostConstruct
public void init() { }