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> |
分享到:
相关推荐
<artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>...
赠送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="com.baobaotao.web"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation. ...
<context:component-scan base-package="org.whvcse"></context:component-scan> <tx:annotation-driven transaction-manager="txManager" /> <!-- <aop:config> <aop:pointcut id="defaultServiceOperation" ...
<context:component-scan base-package="com.org"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org....
<context:component-scan base-package="Controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> ...
赠送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;...
赠送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-...
<context:component-scan base-package="com.ccc"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /> <!-- 配置事务管理器 針對MES數據庫-...
赠送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简单学习项目 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ... <context:component-scan base-package="com.sixth" /> </beans>
<context:component-scan base-package="*" /> <!-- <aop:config>--> <!-- execution第一个星号代表任何返回类型,第二个星号代表com.sbz.service下的所有包,第三个星号代表所有方法,括号中的两个点代表任何...
<context:component-scan base-package="com.org.*" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org....
<context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <aop:config proxy-...
<context:component-scan base-package="com.test.controller" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation....
10. <context:annotation-config /> 11. <!-- 把标记了@Controller注解的类转换为bean --> 12. <context:component-scan base-package="com.mvc.controller" /> 13. <!-- 启动Spring MVC的注解功能,...
<context:component-scan base-package="com.bbs"/> <!--注解支持--> <mvc:annotation-driven/> <!--视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view....
@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....
SpringMVC第12讲:<mvc:annotation-driven/>