- 浏览: 543644 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
jsdsh:
自己写的就是不一样.
Spring3 MVC 注解(二)---@RequestMapping -
jsdsh:
吼,非常吼.学习了
Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释 -
爱情是一种错觉:
我爱你 i love 你[color=red][/color] ...
Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释 -
fs08ab:
大哥,目前这个问题有什么可靠的解决方案吗
@ResponseBody注解 -
lhs295988029:
说的很清楚,明白了~
Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释
1. 扫描过滤方式
过滤器类型 说明
注释 假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。
类名指定 通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。
正则表达式 通过正则表达式定义过滤的类,如下所示: com.baobaotao.Default.*
AspectJ 表达式 通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+
下面是一个简 单的例子:
- <context:component-scan base-package="com.baobaotao">
- <context:include-filter type="regex"
- expression="com.baobaotao.service..*"/>
- <context:exclude-filter type="aspectj"
- expression="com.baobaotao.util..*"/>
- </context:component-scan>
值得注意的是 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标,如以下代码所示:
清单 24. 通过 @Scope 指定 Bean 的作用范围
- package com.baobaotao;
- import org.springframework.context.annotation.Scope;
- …
- @Scope("prototype")
- @Component("boss")
- public class Boss {
- …
- }
这样,当从 Spring 容器中获取 boss Bean 时,每次返回的都是新的实例了。
采用具有特殊语义的注释
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。
注释配置和 XML 配置的适用场合
是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因:
注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。
如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。
注释配置 往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于 @Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。
所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。
Spring applicationContext.xml的<context:component-scan>標籤用途比我想像的還要實用。而且後來才知道,有了<context:component-scan>,另一個<context:annotation-config/>標籤根本可以移除掉,因為被包含進去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下尋找有@Component和@Configuration的target Class。而現在如下的飯粒: <context:component-scan base-package="com.foo" use-default-filters="false">
<context:include-filter type="regex" expression="com.foo.bar.*Config"/>
<context:include-filter type="regex" expression="com.foo.config.*"/>
</context:component-scan>
<context:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。
filter標籤在Spring3有五個type,如下:
Filter Type | Examples Expression | Description |
annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class |
assignable | org.example.SomeClass | 指定class或interface的全名 |
aspectj | org.example..*Service+ | AspectJ語法 |
regex | org\.example\.Default.* | Regelar Expression |
custom | org.example.MyTypeFilter | Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter |
所以上例用的regex就有個語病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因為小數點在Regex是任意字元,是故要用\.把小數點跳脫為佳。(2010/3/15補充:但要使用\.方式,其use-default-filters不能為false,否則抓不到,感覺是Bug)
Spring3提供豐富的Filter支援,有益配置策略,不需面臨Configuration Hell,比如Regex的com\.foo\.*\.action\.*Config,這樣就可以找到com.foo package下所有action子package的*Config的target class。
2010/3/18補充:後來在AppConfig前忘了加@Component,AppConfig內尚留有@Bean,奇怪的是還是能work。我猜有加@Bean的method的class,若沒特別註解AppConfig是@Repository、@Service還是@Configuration,一律被Spring3視為@Component。
评论
eclipse项目里这么配置每问题。部署出来以后,就说找不到BEAN。
我的不是WEB 项目。怎么办。。
如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释
注入bean:
@Inject
private WebService webService;
如果一个接口有多个实现要 为每个实现指定bean 名, @Service("webService1")
注入bean
@Inject
@Named("webService1")
private WebService webService;
eclipse项目里这么配置每问题。部署出来以后,就说找不到BEAN。
我的不是WEB 项目。怎么办。。
发表评论
-
Spring @Transactional
2011-08-01 11:53 1808Spring事务的传播行为 在service类前加上@ ... -
spring mvc java.lang.IllegalStateException: Errors/BindingResult argument declar
2011-01-21 10:21 5052关于BindingResult Spring ... -
kaptcha 验证码在spring mvc 中的使用
2010-11-11 14:46 15403kaptcha 是一个非常实用的验证码生成工具。有了它,你可 ... -
@ResponseBody注解
2010-11-04 17:44 29820在SpringMVC中可以在Contr ... -
spring 标签介绍
2010-10-27 14:42 3034Spring 标记库分类如下: 1、spring:has ... -
spring roo
2010-10-22 14:50 3154Spring Roo 是一种 Spring 开发的辅助工具,当 ... -
spring mvc 实现的验证码功能
2010-10-19 15:56 8810import java.awt.BasicStroke; i ... -
spring3 Validation, Data Binding, and Type Conversion
2010-10-18 17:52 38075. Validation, Data Binding, ... -
spring 基于注解的控制器配置
2010-10-18 16:10 385913.12. 基 于注解的控制器配置 现时对于一些类型的配 ... -
打散 <mvc:annotation-driven>
2010-10-18 13:10 4762Spring3.0的基于注解的MVC非常好用,特别在加入了新的 ... -
spring mvc使用注解后的校验和绑定处理
2010-10-12 15:18 1389spring mvc使用注解后,依然可以使用自带的Valida ... -
使用 Spring 2.5 基于注解驱动的 Spring MVC
2010-10-12 10:45 1559概述 继 Spring 2.0 对 Spring MV ... -
Spring MVC的表单控制器
2010-10-11 15:36 3927概述 大多数Web应 ... -
Spring MVC
2010-09-21 16:12 4404Spring提供了一个细致完整的MVC框架。该框架为模型、视图 ... -
领略Spring 3.x 时代的Spring MVC
2010-09-21 16:07 2000鼎鼎大名的Spring框架3.0版在12月5日由其作者之 ... -
Spring入门之三: 进入Spring MVC
2010-09-13 11:04 2434Spring入门之三: 进入Sprin ... -
Spring3 MVC Restful 多请求类型(json,xml,k-v),多视图配置(Json,Xml,JSP,Freemarker,Volacity等)
2010-09-13 10:59 5850beans xmlns="http://www.sp ... -
Spring3 MVC (三)---请求处理方法 参数及返回值总结
2010-09-13 10:50 8559@RequestMapping("/xx ... -
Spring3 MVC 注解(二)---@RequestMapping
2010-09-13 10:47 155621 问题:有多个 @RequestMapping @ ... -
Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释
2010-09-13 10:46 93107一:配置web.xml 1)问题:spri ...
相关推荐
主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下
NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898
<context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...
<context:component-scan base-package="com.bbs"/> <!--注解支持--> <mvc:annotation-driven/> <!--视图解析--> ...
配置使用注解的Handler和Service等等使用<context:component-scan> 不过springBoot已经省略了这些配置 常用注解:@Controller @RestController(Controller+ResponseBody) @Service @Transactional @Mapper @...
<context:component-scan base-package="org.whvcse"></context:component-scan> <tx:annotation-driven transaction-manager="txManager" /> <!-- <aop:config> <aop:pointcut id="defaultServiceOperation" ...
//applicationContext.xml文件中添加 ... xmlns:xsi=... <context:component-scan base-package="com.haijian" />
<context:component-scan base-package="com.createnets.springmvc.web" /> (2)新建一个Student类 用于测试注解 (3)配置注解 @Controller @RequestMapping("/list") (3)配置视图名称 <!-- 配置视图名称 -...
<context:component-scan base-package="com.jxy.java.service" /> <!-- 导入数据库配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据库连接池 --> ...
<context:component-scan base-package="com.spring.*"/> <aop:config> <aop:aspect id="aspect" ref="logIntercepter"> <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/> ...
<context:component-scan base-package="cn.ly.quartz.service" /> <!-- job --> class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- trigger触发器 --> ...
维生药业小项目 SSH简单学习项目 <?xml version="1.0" encoding="UTF-8"?> ... xmlns:context="http://www.springframework.org/schema/context" ... <context:component-scan base-package="com.sixth" /> </beans>
学习spring组件扫描(Component Scanning)的代码 使用方法:直接把工程导入,直接Run ...<context:component-scan base-package="com.test"></context:component-scan> 2.在需要装配的类的上面全部加上@Component
1.26 context:component-scan扫描使用上的容易忽略的use-default-filters 1.27 idea内嵌jetty运行springmvc项目报ConversionFailedException 1.28 springmvc 3.2 @MatrixVariable注解 1.29 spring3.2 带matrix变量的...
context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml配置文件中的bean元素,简化配置 context:component-scan是上下文:annotation-config的超集,配置了前者则不需要配置...
spring配置文件中唯一需要配置的bean是WeixinConfigurer类,<context:component-scan base-package="com.github.jweixin.jwx.weixin.service" />是可选配置,但里面封装了微信接口服务类,建议一定要配置进spring...
<context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <aop:config proxy-...
<context:component-scan base-package="com.itheima.service"/> <!--aop面向切面编程,切面就是切入点和通知的组合--> <!--配置事务管理器--> <!--配置事务的通知--> <tx:advice id="advice"> <tx:...
<context:component-scan base-package="com.baobaotao.web"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> AnnotationMethodHandlerAdapter"/> <!-- ③:对模型视图名称的解析,即在...