Spring框架是Java企业级应用开发中的主流轻量级IoC容器,它通过依赖注入(DI)和AOP(面向切面编程)机制简化了应用程序的设计和管理。在Spring 2.5及后续版本中,引入了四个核心的注解:@Component、@Repository、@Service和@Controller,它们分别对应不同的角色和职责,有助于组织和管理应用程序的不同组件。 1. **@Component**: 这个注解是Spring 2.5引入的基础注解,用于标记任何可以被Spring管理的组件,包括业务层、服务类等。默认情况下,Spring会自动扫描带有@Component注解的类,将其注册为Bean,除非有特定的扫描规则(如<context:component-scan>)阻止其被发现。 2. **@Repository**: 主要用于标记数据访问层的对象,比如JDBC Dao或者Hibernate Dao。这些对象通常与数据库操作有关,Spring会将其自动装配到业务逻辑中,提供对数据的操作支持。 3. **@Service**: 代表服务层,主要处理业务逻辑和业务规则,不直接涉及数据访问。它通常不直接与用户界面交互,而是通过其他组件如Controller来调用。 4. **@Controller**: 定义在web应用中的控制器,它是Spring MVC的核心,负责接收HTTP请求并进行处理,将请求转发到相应的处理方法或视图。@Controller注解的类会自动注册为Spring MVC的前端控制器。 5. **Scanning Strategy**: Spring通过<context:component-scan>标签进行组件扫描,可以指定扫描特定包或基于特定注解(如@Component)查找Bean。设置base-package属性用于指示Spring在哪些包下查找组件,如果设置为"*",则表示扫描整个项目。 6. **Bean Scope**: 对于单例(Singleton)模式的bean,Spring默认为@Service和@Controller中的类,而@Repository和@Component可以通过@Service(beanName)配合@Scope(prototype)指定为原型(Prototype),这使得一个bean实例能够服务于多个请求。 7. **Autowired**: 在Spring中,使用@Autowired注解可以实现自动装配,无需显式地在代码中设置依赖关系。例如,当一个字段被@Autowired注解时,Spring会尝试找到一个符合要求的bean并将其注入到该字段中。 8. **XML vs Annotation**: 在Spring 2.5版本以后,随着注解的流行,大部分配置可以通过注解完成,但为了兼容旧的Spring配置,仍然可以使用XML配置文件。@Component等注解的使用意味着XML bean定义不再必需,但有时仍需在XML中进行更细致的配置。 9. **Customizing Bean Names**: 如果希望自定义bean的名字,可以在@Service或@Controller注解中使用beanName属性,同时还可以使用@Scope进行更精细的控制,如设置为prototype时,每次请求都会创建一个新的bean实例。 Spring的这些注解在应用架构设计中起着关键作用,通过合理的注解使用,可以提升代码的可读性和可维护性,同时降低了配置复杂度,使得Spring成为现代Java应用开发的首选框架之一。
Spring2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的Spring版本中,
这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然目前这 3 个注释和 @Component 相比没有什么新意,但Spring将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,
最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。
在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。
Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了 @Component,@Service,@Controller,@Repository注解的类,
并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:
5.</beans>
6. 其中base-package为需要扫描的包(含所有子包) @Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),
@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
7. @Service public class VentorServiceImpl implements iVentorService {
8. } @Repository public class VentorDaoImpl implements iVentorDao {
9. } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单
例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。
注解:
在 spring的配置文件里面只需要加上<context:annotation-config/>和<context:component-scan base-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。
<context:component-scan base-package=”com.eric.spring”>
其中base-package为需要扫描的包(含所有子包)
@Autowired可以代替set方法注入,如:
@Autowired
private Man man;
否则可以省略,只写@Autowired 。
@Service服务层组件,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,如果需要自己改名字则:@Service("你自己改的bean名")。
下载后可阅读完整内容,剩余1页未读,立即下载
- 粉丝: 0
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 掌握数学建模:层次分析法详细案例解析
- JSP项目实战:广告分类系统v2.0完整教程
- 如何在没有蓝牙的PC上启用并使用手机蓝牙
- SpringBoot与微信小程序打造游戏助手完整教程
- 高效管理短期借款的Excel明细表模板
- 兄弟1608/1618/1619系列复印机维修手册
- 深度学习模型Sora开源,革新随机噪声处理
- 控制率算法实现案例集:LQR、H无穷与神经网络.zip
- Java开发的HTML浏览器源码发布
- Android闹钟程序源码分析与实践指南
- H3C S12500R升级指南:兼容性、空间及版本过渡注意事项
- Android仿微信导航页开门效果实现教程
- 深度研究文本相似度:BERT、SentenceBERT、SimCSE模型分析
- Java开发的zip压缩包查看程序源码解析
- H3C S12500S系列升级指南及注意事项
- 全球海陆掩膜数据解析与应用