`
u011721609
  • 浏览: 40164 次
社区版块
存档分类
最新评论

Spring配置文件详解一:<:<context:annotation-config/>与<context:component-scan base-package="com.xx

 
阅读更多

1.<context:annotation-config/>
<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"></wbr></wbr></wbr>

在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<context:annotation-config/>这样一条配置,它的作用是隐式的向Spring容器注册

AutowiredAnnotationBeanP<wbr style="line-height:22px"><wbr style="line-height:22px">ostProcessor</wbr></wbr>,CommonAnnotationBeanPost<wbr style="line-height:22px"><wbr style="line-height:22px">Processor</wbr></wbr>

PersistenceAnnotationBea<wbr style="line-height:22px"><wbr style="line-height:22px">nPostProcessor</wbr></wbr>,RequiredAnnotationBeanPo<wbr style="line-height:22px"><wbr style="line-height:22px">stProcessor</wbr></wbr>

<wbr style="line-height:22px">这4个BeanPostProcessor.注册这4个bean处理器<span style="color:#ff00ff; line-height:22px">主要的作用是为了你的系统能够识别相应的注解。</span><wbr style="line-height:22px"><wbr style="line-height:22px"><wbr style="line-height:22px"><br style="line-height:22px"> 例如:</wbr></wbr></wbr></wbr>

如果想使用@Autowired注解,需要在Spring容器中声明AutowiredAnnotationBeanP<wbr style="line-height:22px">ostProcessor</wbr>Bean。

传统的声明方式如下:

<beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanP<wbr style="line-height:22px">ostProcessor"</wbr>/>

如果想使用@PersistenceContext注解,需要在Spring容器中声明PersistenceAnnotationBea<wbr style="line-height:22px">nPostProcessor<span></span></wbr>Bean。

传统的声明如下:

<beanclass="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPo<wbr style="line-height:22px">stProcessor"</wbr>/>

如果想使用@Required注解,需要在Spring容器中声明RequiredAnnotationBeanPo<wbr style="line-height:22px">stProcessor<span></span></wbr>Bean。

传统声明方式如下:

<beanclass="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPo<wbr style="line-height:22px">stProcessor"</wbr>/>

如果想使用@Resource、@ PostConstruct、@ PreDestroy等注解就要声明CommonAnnotationBeanPost<wbr style="line-height:22px">Processor</wbr>

传统申明方式如下:
<beanclass="org.springframework.beans.factory.annotation.CommonAnnotationBeanPost<wbr style="line-height:22px">Processor"<span></span></wbr>/>

以,如果按照传统声明一条一条去声明注解Bean,就会显得十分繁琐。因此如果在Spring的配置文件中事先加上<context:annotation-config/>这样一条配置的话,那么所有注解的传统声明就可以被<wbr style="line-height:22px"><span></span>忽略,即不用在写传统的声明,Spring会自动完成声明。</wbr>



2.<context:component-scan base-package="com.xx" /><wbr style="line-height:42px"><br style="line-height:42px"></wbr>

<wbr style="line-height:28px"></wbr>

<context:component-scan/>的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
值得注意的是<context:component-scan/>不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanP<wbr style="line-height:22px">ostProcessor<span></span></wbr>和<wbr style="line-height:22px"><span></span><span style="color:#0000ff; line-height:22px">CommonAnnotationBeanPost<wbr style="line-height:22px">Processor</wbr></span>),因此当使用</wbr>
<context:component-scan/>后,就可以将<context:annotation-config/>移除了。

<wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

3. @Autowired<wbr style="line-height:42px"></wbr><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><wbr style="line-height:28px"><br style="line-height:28px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:28px"><span style="font-size:12px; line-height:22px"><span style="color:#ff0000; line-height:22px"><strong style="line-height:22px">@Autowired</strong></span>可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。注解之后就不需要set/get方法了。<br style="line-height:22px"></span><wbr style="line-height:28px"></wbr></wbr>

分享到:
评论

相关推荐

    springboot 基础简易实例, maven项目

    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt; &lt;/parent&gt; &lt;groupId&gt;com.example&lt;/groupId&gt; &lt;artifactId&gt;...

    jakarta.annotation-api-1.3.5-API文档-中文版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    springweb3.0MVC注解(附实例)

    &lt;context:component-scan base-package="com.baobaotao.web"/&gt; &lt;!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation. ...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:component-scan base-package="org.whvcse"&gt;&lt;/context:component-scan&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; &lt;aop:pointcut id="defaultServiceOperation" ...

    springmvc-ibatis

    &lt;context:component-scan base-package="com.org"/&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org....

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;context:component-scan base-package="Controller" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; ...

    javax.annotation-api-1.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.2.jar; 赠送原API文档:javax.annotation-api-1.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-1.2.pom;...

    javax.annotation-api-1.3.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...

    spring applicationContext 配置文件

    &lt;context:component-scan base-package="com.ccc"/&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /&gt; &lt;!-- 配置事务管理器 針對MES數據庫-...

    jakarta.annotation-api-1.3.5-API文档-中英对照版.zip

    赠送jar包:jakarta.annotation-api-1.3.5.jar; 赠送原API文档:jakarta.annotation-api-1.3.5-javadoc.jar; 赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation...

    维生药业小项目 SSH简单学习项目

    维生药业小项目 SSH简单学习项目 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" ... &lt;context:component-scan base-package="com.sixth" /&gt; &lt;/beans&gt;

    spring3.2+strut2+hibernate4

    &lt;context:component-scan base-package="*" /&gt; &lt;!-- &lt;aop:config&gt;--&gt; &lt;!-- execution第一个星号代表任何返回类型,第二个星号代表com.sbz.service下的所有包,第三个星号代表所有方法,括号中的两个点代表任何...

    SpringMVC+Hibernate全注解整合

    &lt;context:component-scan base-package="com.org.*" /&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org....

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;aop:config proxy-...

    springjdbc

    &lt;context:component-scan base-package="com.test.controller" /&gt; &lt;!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation....

    spring_MVC源码

    10. &lt;context:annotation-config /&gt; 11. &lt;!-- 把标记了@Controller注解的类转换为bean --&gt; 12. &lt;context:component-scan base-package="com.mvc.controller" /&gt; 13. &lt;!-- 启动Spring MVC的注解功能,...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view....

    Spring AOP配置源码

    @Component("userService")等价于在spring配置文件中定义一个&lt;bean id="userService"/&gt; @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....

    (代码)SpringMVC第12讲:<mvc:annotation-driven/>

    SpringMVC第12讲:&lt;mvc:annotation-driven/&gt;

Global site tag (gtag.js) - Google Analytics