Spring自动检测Bean
来源:互联网 发布:设置左键连击宏编程 编辑:程序博客网 时间:2024/05/16 07:23
当在Spring注解中增加<context:annotation-config>时,我们希望Spring特殊对待我们所定义的Bean里的某一组注解,并使用这些注解知道Bean的装配,从而达到减少甚至完全消除<property>和<constructor-arg>元素的使用,但是我们仍然需要<bean>元素显式地定义Bean.
因此,Spring提供了一种强大的方式。<context:component-scan>元素除了完成与<context:annotation-config>一样的工作,还允许Spring自动检测Bean和定义Bean.这意味着不使用<bean>元素,Spring都可以实现自动定义和装配
为了配置Spring自动检测,需要使用<context:component-scan>来代替<context:annotation-config>:
<context:component-scan base-package="test.Controller" />
<context:component-scan>会自动扫描指定的包及其子包,并查找出能够自动注册为Spring Bean的类,base-package指定了<context:component-scan base-package="test.Controller" /> 所扫描的包默认情况下,<context:component-scan>查找使用构造型(stereotype)注解所标注的类,这些特殊的注解如下:
1、@component 通用的构造型注解,标识该类为Spring组件
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
@Componentpublic class UserDaoImpl extends HibernateDaoSupport implements UserDao { ... }
使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是userDaoImpl。你也可以指定Bean的名称:
@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。推荐使用@Repository、@Service、@Controller来替代@Component。
2.@Controller 将对应类定义为Spring MVC Controller
@Controller注解是一个特殊的Component,它允许了实现类可以通过扫描类配置路径的方式完成自动注入,通常@Controller是结合@RequestMapping注解一起使用的。for example:
import com.baobaotao.service.BbtForumService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import java.util.Collection;@Controller //<——①@RequestMapping("/forum.do")public class BbtForumController { @Autowired private BbtForumService bbtForumService; @RequestMapping //<——② public String listAllBoard() { bbtForumService.getAllBoard(); System.out.println("call listAllBoard method."); return "listBoard"; }}
从上面代码中,我们可以看出 BbtForumController 和一般的类并没有区别,它没有实现任何特殊的接口,因而是一个地道的 POJO。让这个 POJO 与众不同的魔棒就是 Spring MVC 的注解!
在 ① 处使用了两个注解,分别是 @Controller 和 @RequestMapping。@Controller让一个类成为 Spring 容器的 Bean。由于 Spring MVC 的 Controller 必须事先是一个 Bean,所以 @Controller 注解是不可缺少的。
真正让 BbtForumController 具备 Spring MVC Controller 功能的是 @RequestMapping 这个注解。@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;还可以标注在方法签名处,以便进一步对请求进行分流。在 ① 处,我们让 BbtForumController 关联“/forum.do”的请求,而 ② 处,我们具体地指定 listAllBoard() 方法来处理请求。所以在类声明处标注的 @RequestMapping 相当于让 POJO 实现了 Controller 接口,而在方法定义处的 @RequestMapping 相当于让 POJO 扩展 Spring 预定义的 Controller(如 SimpleFormController 等)。
如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value="UserAction")】或者【@Controller("UserAction")】,则使用value作为bean的名字。
@Repository代表仓库. 一般注解在DAO实现类上, 别人看代码时, 就知道这个类是一个跟数据存储有关的类.
4.@Service 将对应类定义为服务类
- spring 自动检测bean
- Spring自动检测Bean
- spring(自动检测bean)
- Spring自动检测Bean
- Spring自动检测并装配bean
- 【Spring学习16】自动检测Bean
- spring的自动装配Bean与自动检测Bean
- spring的自动装配Bean与自动检测Bean
- 【spring教程之十】spring注自动检测标注bean
- 简化Spring的xml文件配置-自动检测bean
- Spring杂记之Bean自动检测与自动装配
- [Spring实战系列](14)Bean的自动检测
- Spring3.x--自动检测Bean
- 3.3 自动检测Bean
- 自动检测注入Bean
- (笔记)Spring实战_最小化Spring XML配置(3)_自动检测Bean
- Spring自动注解装配、自动检测装配Bean配合过滤组件使用
- 从头认识Spring-2.7 自动检测Bean(1)-@Component @Repository @Service @Controller
- 导航栏与状态栏
- 安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法
- Spring+Quartz定时器执行2次问题解决
- Java中一年有几周、周的计算
- jquery 不执行
- Spring自动检测Bean
- display:box的兼容写法
- 从印象笔记转入有道云
- 张书润:黄金多头开启新征程 美国大选千四或可期
- css 的块级元素和行内元素
- Linux查看程序端口占用情况
- 第四周项目5——猴子选大王
- 进度条
- POJ3061-Subsequence【尺取法】