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

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>

分享到:
评论

相关推荐

    SSH整合配置.doc

    &lt;context:component-scan base-package="com.example"/&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"/&gt; ...

    spring mvc基础

    &lt;param-value&gt;classpath:config/springAnnotation-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- 启动Spring上下文监听器 --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context....

    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    &lt;context:component-scan base-package="com.example.controller"/&gt; &lt;!-- 启用Spring MVC注解驱动 --&gt; &lt;mvc:annotation-driven/&gt; &lt;!-- 视图解析器 --&gt; &lt;bean class="org.springframework.web.servlet.view....

    springMVC框架搭建及详解

    &lt;context:component-scan base-package="com.example.controller"/&gt; &lt;!-- 视图解析器 --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property ...

    自己写网页spring框架搭建

    - 通过`&lt;context:component-scan base-package="controller"/&gt;`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图解析器以解析返回的视图名称为实际的视图资源。 ```xml &lt;beans xmlns=...

    开发框架搭建

    &lt;context:component-scan base-package="cn.springmvc"/&gt; &lt;!-- 配置MyBatis SqlSessionFactory --&gt; &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt; &lt;property name=...

    springmvc+mybatis+oracle

    &lt;context:component-scan base-package="com.example.controller" /&gt; &lt;!-- 自定义拦截器配置 --&gt; &lt;mvc:interceptors&gt; &lt;bean class="com.example.interceptor.MyInterceptor" /&gt; &lt;/mvc:interceptors&gt; &lt;/beans&gt; ...

    [新手-图文]整合ssm框架-从mybatis到spring-mybatis再到ssm-sping-mybatis-spingmvc

    &lt;context:component-scan base-package="cn.abc.controller"/&gt; &lt;/beans&gt; ``` ##### 5.2 配置web.xml 设置`context-param`和`ContextLoaderListener`以启动Spring和Spring MVC。 **示例代码**: ```xml &lt;!-- web....

    SSH全注解环境搭建

    &lt;context:component-scan base-package="com.zhaolongedu"/&gt; ``` - 配置Spring事务管理器: ```xml &lt;bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;...

    SSM环境搭建

    &lt;context:component-scan base-package="com.service"/&gt; &lt;!-- 配置事务管理器 --&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;property name...

    JSP 中spring事务配置详解.docx

    &lt;context:component-scan base-package="com.rd,com.rongdu"/&gt; &lt;context:annotation-config/&gt; &lt;!-- 数据源配置 --&gt; &lt;bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"...

    ssm框架整合

    &lt;context:component-scan base-package="com.example.dao"/&gt; &lt;!-- 扫描业务逻辑包 --&gt; &lt;context:component-scan base-package="com.example.biz"/&gt; &lt;!-- 配置事务增强 --&gt; &lt;tx:annotation-driven transaction...

    maven-test

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

    ssh整合步骤一站式

    &lt;context:component-scan base-package="com.zhiwen.service,com.zhiwen.dao,com.zhiwen.action,com.zhiwen.entity" /&gt; &lt;context:property-placeholder location="classpath:jdbc.properties" /&gt; &lt;aop:aspectj-...

    Spring注解详解

    &lt;context:component-scan base-package="com.example"/&gt; ``` 这将扫描指定的类包和其递归子包中的所有类,并将其注册到 Spring 容器中。 Spring支持四种类型的过滤方式: 1. 注解过滤:使用 `@SomeAnnotation` ...

    Spring MVC简单实例

    &lt;context:component-scan base-package="com.yourpackage"/&gt; &lt;mvc:annotation-driven/&gt; &lt;!-- 视图解析器配置 --&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; ...

    spring+ibatis集成文档

    &lt;context:component-scan base-package="ch10.SpringAndIbatis"/&gt; &lt;/beans&gt; ``` 这里主要包括数据源配置、SqlSessionFactory的配置、Dao接口实现类的定义、事务管理器以及扫描指定包来自动注册Bean等功能。 #### ...

    Spring MVC学习指南

    &lt;context:component-scan base-package="com.example"/&gt; &lt;!-- 视图解析器 --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix...

    ssm 框架配置

    &lt;context:component-scan base-package="com.example.controller, com.example.service, com.example.dao"&gt; &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /...

Global site tag (gtag.js) - Google Analytics