- @PostConstruct关于这个没有生效,主要是@PostConstruct执行是在类实例化之后,如果bean归Spring管理,bean不能延迟加载(Spring xml 中default-lazy-init=false或在实体类上@Lazy(false))
- context:component-scan 是Spring 提供的扫描注解类,加载注解并实例化,如果MVC 采用的是Spring自己的, 就有存在父子容器的问题,bean的扫描重复覆盖,直至到时事务失效等等系列问题,context:component-scan 组件有一系列属性,主要如下
<context:component-scan base-package="" resource-pattern="**/*.class" name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator" use-default-filters="true" annotation-config="true"> <context:include-filter type="aspectj" expression=""/> <context:exclude-filter type="regex" expression=""/> </context:component-scan>
详情可以查询 http://www.iteye.com/topic/1121913 和 http://jinnianshilongnian.iteye.com/blog/1762632
文章中作者主要是修改了 base-package的路径,bean中扫描的和mvc中扫描的路径不一样,从而避免上述问题, 但这依赖于路径规划,
比如com.a.b.web com.a.b.servie
<context:component-scan base-package="com.a.b.web" use-default-filters="false">
<context:component-scan base-package="com.a.b.service" >
这里说下 use-default-filters , context:component-scan默认是为true的,标示扫描@Component、@ManagedBean、@Named , 默认帮你include-filter 这几个Annotion,include-filter和exclude-filter,会再如下进行过滤排除
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException { for (TypeFilter tf : this.excludeFilters) { if (tf.match(metadataReader, this.metadataReaderFactory)) { return false; } } for (TypeFilter tf : this.includeFilters) { if (tf.match(metadataReader, this.metadataReaderFactory)) { AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); if (!metadata.isAnnotated(Profile.class.getName())) { return true; } AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class); return this.environment.acceptsProfiles(profile.getStringArray("value")); } } return false; }
如果项目路径相同(mvc和bean),则需要如下配置
MVC(Spring-mvc.xml):
<context:component-scan base-package="com.a.b" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
Bean(application.xml)
<context:component-scan base-package="com.a.b"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
服务器启动application.xml先执行,此时use-default-filters=true是默认值会扫描(@Component、@ManagedBean、@Named),@Component是@Service、@Repository、@Controller的子集,但会被context:exclude-filter过滤到Controller,留下Service、Repository、Component
接着加载Spring-mvc.xml,此时use-default-filters=false,则是会检索到include-filter对应的Controller
正好mvc 和 application 分开加载注解。解决了重复加载的一些问题,如下
1、事务失效,以及Spring data jpa @Modifying注解异常
2、Spring的异步执行 @Async
总结:
主要是理解Spring context:component-scan的 注解和相关属性,会交给ContextNamespaceHandler类,具体是ComponentScanBeanDefinitionParser类读取,并有ClassPathBeanDefinitionScanner进行处理
相关推荐
java @PostConstruct和@PreConstruct注解整理.........
获取springbean对象
NULL 博文链接:https://nhy520.iteye.com/blog/889276
主要给大家介绍了关于Spring定时任务中@PostConstruct被多次执行异常的分析与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
Component:不分层的注解 Controller:web层 Service:service层 Repository:dao层 管理对象 注入 AutoWired Qualifier Resource Value 声明周期 Scope PostConstruct PreDestroy 新注解 ...
主要介绍了SpringBoot @PostConstruct原理用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
主要介绍了Spring实战之使用@POSTConstruct和@PreDestroy定制生命周期行为操作,结合实例形式详细分析了Spring使用@POSTConstruct和@PreDestroy定制生命周期相关接口定义、配置与功能实现技巧,需要的朋友可以参考下
一.说在前面(结论思考) @postConstruct 所标注的方法 内部是靠的spring提供的两个后置处理器...是各自又是在 bean 的生命周期的哪一个阶段`起的作用? 二.弄清InitDestroyAnnotationBeanPostProcesso
spring 注解初始化,初始化开始@postconstruct 对象销毁@predestroy javax.annotation.jar文件
ip2region-spring-boot-starter 用于ip2region的Spring Boot Starter说明基于ip2region的Spring Boot Starter实现最新IP数据下载地址: : ...import javax.annotation.PostConstruct ;import org.springframework.beans
EJB容器 EJB 容器的个人实现。 特征: 单例 bean 和无状态 bean 的管理。 为此,框架用户只需要在实现 EJB 的代码中放置 @Singleton 或 @Stateless 注释。 EJB 注入。 可以使用@EJB 注释一个类的一个或多个字段,...
在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @PostConstruct 和 @PreDestroy 注释: 为了定义一个 bean...
注解介绍等详细说明及使用: • @Controller • @Service • @Autowired • @RequestMapping • @RequestParam ... • @Component (不推荐使用) • @Scope • @SessionAttributes • @InitBinder
spring4框架系列 [ 5 ] 基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy
3.6.4 @PostConstruct 28 3.6.5 @PreDestroy 28 注解注入拓展: 28 3.7扫描注入 30 注解扫描拓展: 32 Mvc用注解写: 34 Spring容器IOC和di的整个启动过程: 38 3.8 spring中的继承 38 拓展spring为类中的属性赋值:...
@PostConstruct and @PreDestroy. DETAILS Please see the doc at https://github.com/Netflix/governator/wiki BUILDING Governator is built via Gradle (http://www.gradle.org). To build from the command line...
使用@PostConstruct 跟随项目服务启动; 异常处理:最大连接数控制; Client方法:连接服务器并发送数据;接收来自服务器的数据;使用@SpringBootTest启动,用于测试连接、数据通信; spring上下文工具类:服务端...
Java中 @PostConstruct和@PreDestroy注解有什么作用?编写Webservice接口服务端(框架)有几种方式?常用的缓存框架有哪几个?简述TCP三次握手的理解 Linux下你常用的命令有哪些?Nginx有什么作用?