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> |
分享到:
相关推荐
<context:component-scan base-package="com.example"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> ...
<param-value>classpath:config/springAnnotation-*.xml</param-value> </context-param> <!-- 启动Spring上下文监听器 --> <listener> <listener-class>org.springframework.web.context....
<context:component-scan base-package="com.example.controller"/> <!-- 启用Spring MVC注解驱动 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view....
<context:component-scan base-package="com.example.controller"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property ...
- 通过`<context:component-scan base-package="controller"/>`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图解析器以解析返回的视图名称为实际的视图资源。 ```xml <beans xmlns=...
<context:component-scan base-package="cn.springmvc"/> <!-- 配置MyBatis SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name=...
<context:component-scan base-package="com.example.controller" /> <!-- 自定义拦截器配置 --> <mvc:interceptors> <bean class="com.example.interceptor.MyInterceptor" /> </mvc:interceptors> </beans> ...
<context:component-scan base-package="cn.abc.controller"/> </beans> ``` ##### 5.2 配置web.xml 设置`context-param`和`ContextLoaderListener`以启动Spring和Spring MVC。 **示例代码**: ```xml <!-- web....
<context:component-scan base-package="com.zhaolongedu"/> ``` - 配置Spring事务管理器: ```xml <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">...
<context:component-scan base-package="com.service"/> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name...
<context:component-scan base-package="com.rd,com.rongdu"/> <context:annotation-config/> <!-- 数据源配置 --> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"...
<context:component-scan base-package="com.example.dao"/> <!-- 扫描业务逻辑包 --> <context:component-scan base-package="com.example.biz"/> <!-- 配置事务增强 --> <tx:annotation-driven transaction...
<context:component-scan base-package="com.example.demo.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views...
<context:component-scan base-package="com.zhiwen.service,com.zhiwen.dao,com.zhiwen.action,com.zhiwen.entity" /> <context:property-placeholder location="classpath:jdbc.properties" /> <aop:aspectj-...
<context:component-scan base-package="com.example"/> ``` 这将扫描指定的类包和其递归子包中的所有类,并将其注册到 Spring 容器中。 Spring支持四种类型的过滤方式: 1. 注解过滤:使用 `@SomeAnnotation` ...
<context:component-scan base-package="com.yourpackage"/> <mvc:annotation-driven/> <!-- 视图解析器配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> ...
<context:component-scan base-package="ch10.SpringAndIbatis"/> </beans> ``` 这里主要包括数据源配置、SqlSessionFactory的配置、Dao接口实现类的定义、事务管理器以及扫描指定包来自动注册Bean等功能。 #### ...
<context:component-scan base-package="com.example"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix...
<context:component-scan base-package="com.example.controller, com.example.service, com.example.dao"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /...