spring @component的做用详细介绍

spring @component的做用详细介绍

一、@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() { }
相关文章
相关标签/搜索