`
wuhuajun
  • 浏览: 92173 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

注解横行的年代

 
阅读更多

@Component 

@Respository  dao层

@Service         service层

@Controller     controller层

@Autowised     根据类型注入  如果加载set方法上面 那么参数会根据类型进行注入

@Qualifer("XXXX")  Autowised  注入时候如果有多个对象 可以再加上@Qualifer("XXXX")这个注解确定是哪个 同理用在方法上面

这些都是一类注解  被context:component-scan 扫描  只是赋予了不同的意义有不同的处理比如异常

<context:component-scan base-package="com.test"></context:component-scan>

这些都是用来创建bean使用的。

 

<aop:aspectj-autoproxy />aop注解切面的扫描 扫描标注@Aspect切面 生成代理。

<tx:annotation-driven/>这个是事务注解扫描 专门为事务注解提供的扫描@Transactional会被扫描到

这些是提供增强功能的。

 

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案,

会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的,

还会注册一个默认的配置ConversionService接口实现类FormattingConversionServiceFactoryBean ,实现了数据绑定格式化和错误信息处理等。

 

具体参考http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html  To achieve the same in XML use the mvc:annotation-driven element:

 

 

 

 

 

 

============补充======================================

 springmvc事宜失效 

 在主容器中(applicationContext.xml),将Controller的注解排除掉

<context:component-scan base-package="com">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
  </context:component-scan>
 
正确的做法:spring-mvc.xml的context:component-scan只扫描Controller,而 applicationContext.xml里的不包含Controller. 否则你定义在applicationContext.xml里的事务就要失效了
原因:spring-mvc先开始扫描如果把service层扫描了 那么这个时候出来的service层对象是没有事务功能的 也就是在applicationContext.xml的哪些后处理bean是作用不到springmvc扫描的哪些bean上面去的。
所以导致事务失效。
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics