在Spring常用注解第一步部分【Spring框架一】Spring常用注解之Autowired和Resource注解(http://bit1129.iteye.com/blog/2114084)中介绍了Autowired和Resource两个注解的功能,它们用于将依赖根据名称或者类型进行自动的注入,这简化了在XML中,依赖注入部分的XML的编写,但是UserDao和UserService两个bean仍然要在XML中进行注册,能否将UserDao和UserService两个bean(将它们进行一般后,就是泛指持久化层bean和业务逻辑层bean)也通过注解的方式而无需在XML中进行声明?
答案是可以的,Spring提供了一组通过自动检测将对应的类对象注册为bean的注解,
- Component
- Repository
- Service
- Contoller
Component是一个广泛的概念,泛指一个项目中某一个组件。通常,一个项目根据分层设计为请求控制层,业务逻辑层,持久化层和模型层。由于模型是变化的(或者它的作用于是Request),模型层基本不会让Spring进行管理。Component可以用于请求控制层,业务逻辑层,持久化层任意一层的组件,为了使注解更加清晰,Spring针对每一层提供了不同的注解,
请求控制层,业务逻辑层,持久化层分别对应Controller,Service和Repository三个注解,这只是一个分层的约定,Spring不会强制业务逻辑层的Service一定使用@Service注解,也不会强制持久化层一定使用@Repository注解,或者说使用了@Repository注解就一定表示持久化层。@Component,@Repository,@Service,@Controller是可以通用的,只不过用了@Repository,@Service,@Controller更清晰直观的表示注解的类是一个持久化层的组件,业务逻辑层的组件,请求控制组件
正如为了使用@Autowired和@Resource注解,在Spring的配置文件中,需要加上一行XML配置
<context:annotation-config/>
为了使Spring自动识别项目中的自动注册bean的注解生效,需要在XML配置中添加如下一行:
<context:component-scan base-package="com.tom.user"/>
实例
1. UserDao.java需要添加@Repository注解
2. UserService.java需要添加@Service注解,UserServer依赖的IUserDao字段需要添加@Autowired注解
3. Spring配置文件的内容:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--使注解@Autowired,@Resource生效-->
<context:annotation-config/>
<!--使注解@Component,@Repository,@Service,@Controller生效-->
<context:component-scan base-package="com.tom.user"/>
</beans>
4. 如果@Service和@Repository注解,全部替换为@Component注解,结果也是一样的
5. 此时的UserService没有bean的名称,如何获得UserService在Spring中注册的实例,通过ApplicationContext的根据类型获得bean实例的方法ApplicationContext.getBean(Class beanClazz)
6. @Component,@Repository,@Service,@Controller都有一个value属性,为value指定一个有意义的名义,比如@Service(value="userService"),那么可以通过userService这个名字来获取UserService在Spring配置的实例
ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext-autowired.xml"); IUserService userService = cxt.getBean(UserService.class); // IUserService userService = cxt.getBean("userService", UserService.class);
相关推荐
Spring注解@Component、@Repository、@Service、@Controller区别.doc
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...
在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...
详细介绍了Spring Boot最常用的30个注解,包含概念、原理、示例 Spring Boot最常用的30个注解 一、 @SpringBootApplication 二、 Spring Bean 相关 1 @Controller 2 @Service 3 @Repository 4 @Component 5 @Bean 6 ...
SpringMVC 采用了松散耦合可插拔组件结构,更具扩展性和灵活性,开发 Web 应用流程也非常简单,越来越多的大公司开始使用 SpringMVC 作为开发框架。 核心内容: 1. RequestMapping 注解 2. Controller 注解 3. ...
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别
spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...
<context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...
Spring Boot应用程序中常用注释的列表 本文档包含Spring Boot应用程序中常用注释的不完整列表。 它应该用作快速查找列表,有关详细和全面的信息,请阅读官方的javadocs和文档。 内容 核心弹簧 注释的方法产生一个由...
类的注解步骤:开启类扫描、添加注解(Component、Repository 、Service 、Controller )
Spring MVC的常用注解 @Controller @RequestMapping @ResponseBody @RequestBody @PathVariable @RestController Spring MVC的基本配置 静态资源映射 拦截器配置 @ControllerAdivce ...
项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...
注意:使用注解的方式,最终通知和后置通知顺序换了,建议使用环绕通知 注解 配置 声明式事务管理 PlatFormTransactionManager:平台事务管理器:定义了commit/rollback Mybatis/jdbc:...
3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一...
3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一...
面试遇到的问题 1.spring的AOP/IOC怎么用 Ioc: ...Aop: ... 2.设计模式 单例模式 ...3.Spring的优越性 ...@Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊的@Compone
IOC 基于 注解方式 实现---------------------- 半自动化配置 ... @Component: 当一个类 , 分不清是 哪个层 可以用这个注解来修饰 @Controller: 一般用来修饰 控制层 @Autowired @Qualifier spring aop 实现
Spring组件自动扫描详解及实例代码 ...其他特定的注解有@Repository、@Service和@Controller,它们分别标识了持久层、服务处和表现层的组件。 实现方法 User.Java package com.zzj.bean; impor