2.2关于各种注解

来源:互联网 发布:卷积神经网络算法流程 编辑:程序博客网 时间:2024/05/16 23:43

1. 关于各种注解:Spring注解@Component、@Repository、@Service、@Controller区别

自己的话:原来只有@Componenti一种注解,后来添加了其他三种,效果是一样的。都是xml文件里配置解析器去读配有这几个注解的类

,但是用不同的文意标示更能体现中三层架构的含义。

 

http://blog.csdn.net/zhang854429783/article/details/6785574

在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。
所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。

@Service用于标注业务层组件

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

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

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

复制代码
<?xml version="1.0" encoding="UTF-8" ?>   <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:context="http://www.springframework.org/schema/context"      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">            <context:annotation-config />      <context:component-scan base-package=”com.eric.spring”>     </beans>   
复制代码

<tx:annotation-driven transaction-manager="txManager"/>
负责解析注解: @service @transaction等

0 0
原创粉丝点击