@Component @Service @Controller @Repository注解使用

来源:互联网 发布:mac linux双系统 编辑:程序博客网 时间:2024/05/21 22:24

阿里云服务器购买!


@Component 相当于实例化类的对象,相当于在配置文件中注入了一个bean

要使用自动扫描机制,我们在spring的配置文件中应该如下配置:

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 

注意:表示扫描com.fangleijiang包以及它的子包



四个注解的区别:

@Service用于标注业务层组件、

 @Controller用于标注控制层组件(如Struts中的action)

 @Repository用于标注数据访问组件即DAO组件

 @Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解)