<wbr><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">在基于主机方式配置</span><span lang="EN-US" style="padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">Spring</span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">的配置文件中,你可能会见到</span><span style="color:#ff00; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><span lang="EN-US" style="padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><context:annotation-config/></span></span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">这样一条配置,他的作用是式地向</span><span lang="EN-US" style="padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><wbr>Spring<wbr></wbr></wbr></span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">容器注册</span></wbr>
AutowiredAnnotationBeanP<wbr>ostProcessor</wbr>、CommonAnnotationBeanPost<wbr>Processor</wbr>、
PersistenceAnnotationBea<wbr>nPostProcessor<wbr></wbr></wbr>以及<wbr>RequiredAnnotationBeanPo<wbr>stProcessor<wbr></wbr></wbr></wbr>这<wbr>4<wbr></wbr></wbr>个BeanPostProcessor。
注册这4个<wbr></wbr>BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
例如:
如果你想使用@Autowired注解,那么就必须事先在<wbr>Spring<wbr></wbr></wbr>容器中声明<wbr>AutowiredAnnotationBeanP<wbr>ostProcessorBean</wbr></wbr>。传统声明方式如下
<bean<wbr></wbr>class="org.springframework.beans.factory.annotation.<wbr>AutowiredAnnotationBeanP<wbr>ostProcessor<wbr>"</wbr></wbr></wbr>/><wbr></wbr>
如果想使用@
Resource<wbr></wbr>、@
PostConstruct、@
PreDestroy等注解就必须声明CommonAnnotationBeanPost<wbr>Processor</wbr>
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBea<wbr>nPostProcessor</wbr>的Bean。
如果想使用<wbr>@Required</wbr>的注解,就必须声明RequiredAnnotationBeanPo<wbr>stProcessor</wbr>的Bean。同样,传统的声明方式如下:
<bean<wbr></wbr>class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPo<wbr>stProcessor"</wbr>/><wbr></wbr>
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
<wbr><wbr>不过,呵呵,<span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">我们使用注解一般都会配置</span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">扫描包路径选项</span></wbr></wbr>
<context:component-scan<wbr></wbr>base-package=”XX.XX”/><wbr></wbr>
<wbr><wbr><wbr><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">该配置项其实也包含了自动注入上述</span><span lang="EN-US" style="padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">processor</span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">的功能,因此当使用</span><span lang="EN-US" style="padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><wbr><context:component-scan/><wbr></wbr></wbr></span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">后,就可以将</span><span lang="EN-US" style="padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><wbr><context:annotation-config/><wbr></wbr></wbr></span><span style="font-family:宋体; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">移除了。</span></wbr></wbr></wbr>
分享到:
相关推荐
NULL 博文链接:https://xj84.iteye.com/blog/2065681
<artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>...
SpringMVC第12讲:<mvc:annotation-driven/>
NULL 博文链接:https://lgbolgger.iteye.com/blog/2105151
NULL 博文链接:https://yihuawuye1.iteye.com/blog/2105063
赠送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...
<context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...
<display-name>Spring Annotation MVC Sample</display-name> <!-- Spring 服务层的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext...
</beans><context:annotation-config/> <context:component-scan base-package="com.spring.*"/> 两行为开启spring的注解配置 <aop:aspect id="aspect" ref="logIntercepter"> 引入具体的AOP操作类 <aop:pointcut ...
<context:annotation-config /> <context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-...
<context:annotation-config></context:annotation-config> <context:component-scan base-package="org.whvcse"></context:component-scan> <tx:annotation-driven transaction-manager="txManager" /> <!-- ...
<context:annotation-config /> <!-- <bean id="sessionFactory"--> <!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">--> <!-- <property name="configLocation"--> <!-- value=...
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> ...
<mvc:annotation-driven /> <context:component-scan base-package="Controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/...
<context:annotation-config/> <!-- 扫描包 --> <context:component-scan base-package="com.org"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver...
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- apache.dbcp连接池的配置 --> <bean id="dataSource" class="org.apache.commons.dbcp....
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <ref bean=...
10. <context:annotation-config /> 11. <!-- 把标记了@Controller注解的类转换为bean --> 12. <context:component-scan base-package="com.mvc.controller" /> 13. <!-- 启动Spring MVC的注解功能,...
<context:annotation-config/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbc...