spring学习(十七)--annotion注解

时间:2019-10-30
本文章向大家介绍spring学习(十七)--annotion注解,主要包括spring学习(十七)--annotion注解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring的配置一般可以完全在XML文件的<beans>空间中配置完成,但是在庞大的企业应用程序中,可能会定义数百个bean,显然这种方式是很难基于Spring源码去调试的,所以基于这个考虑出现了混合配置的概念。

混合配置的核心是组件扫描和注解配置。通过使用组件扫描,Spring将扫描通过特定注解指定的包查找类,标注了@Component(@org.springframework.stereotype.Component)、@Controller、@Repository、@Service的类将成为Spring管理的bean,其中也可以创建自己的组件注释;注解配置的一个关键注解是:@org.springframework.beans.factoryannotation.Autowired,@Autowired声明了Spring应该在实例化之后注入的依赖。

Warnning:组件扫描默认将扫描所有的@Component注解,要想做到分离bean,使得根应用上下文保存服务、仓库和其他业务逻辑片段,而DispatcherServlet的应用上下文只包含Web控制器,可以通过filter增加黑白名单来实现:

applicationContext.mxl中:
 <context:component-scan base-package="com.wrox">
      <context:exclude-filter expression="org.springframework.stereotype.Controller"    type="annotation" /> 
 </context:component-scan>
 
spring-mvc.xml中:
  <context:component-scan base-package="com.wrox"   use-default-filters="false"> 
      <context:include-filter expression="org.springframework.stereotype.Controller"    type="annotation" /> 
 </context:component-scan>

原文地址:https://www.cnblogs.com/gllegolas/p/11765105.html