`public class LDFrontWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/** * 指定 Root WebApplicationContext 类,这个类必须@Configuration来注解,从而代替XML配置文件 */ @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{RootConfig.class}; }
/** * 指定 Servlet WebApplicationContext 类,这个类必须@Configuration来注解,从而代替XML配置文件 */ @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebMvcConfig.class}; }
/** * 指定 Servlet mappings */ @Override protected String[] getServletMappings() { return new String[]{"/"}; }
} //@order(2) @configuration @EnableWebMvc // <mvc:annotation-driven /> // @EnableAspectJAutoProxy @componentscan(basePackages = "com.lincomb.linktown.front.controller",excludeFilters = {@componentscan.Filter(type = FilterType.ANNOTATION, value = Service.class) ,@componentscan.Filter(type = FilterType.ANNOTATION, value =Configuration.class)
}) //@propertysource(value = { "classpath:config.properties" }) //@import(DubboConfig.class) public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Bean public static AnnotationBean annotationBean() { AnnotationBean annotationBean = new AnnotationBean(); annotationBean.setPackage("com.lincomb.linktown.front"); return annotationBean; }
..............................
` @configuration @componentscan(basePackages = "com.lincomb.linktown.front" ,excludeFilters = { @componentscan.Filter(type = FilterType.ANNOTATION, value = RestController.class),@componentscan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)}) @propertysource(value = { "classpath:config.properties" }) //@ImportResource({ "classpath*:spring-redis.xml", "classpath*:spring-fastdfs.xml" }) public class RootConfig {
//@Autowired LinktownConfig linktownConfig;
/** * 注入dubbo上下文 * * @return */ @Bean public ApplicationConfig applicationConfig() { // 当前应用配置 ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("ld-counsmss"); return applicationConfig; }
/** * 注入dubbo注册中心配置,基于zookeeper * * @return */ @Bean public RegistryConfig registryConfig() { // 连接注册中心配置 RegistryConfig registry = new RegistryConfig(); registry.setProtocol("zookeeper"); registry.setAddress("172.16.107.217:2181,172.16.107.218:2181,172.16.107.219:2181"); return registry; }
``
`package com.lincomb.linktown.front.service;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@service @slf4j public class HelloServiceImpl implements HelloService {
// @reference IAdvertService advertService;
public HelloServiceImpl() { super();
log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>HelloServiceImpl构造器>>>>");
}
@PostConstruct public void aa(){ log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>initHelloServiceImpl>>>>"); // log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>initHelloServiceImpl ---advertService>>>>"+advertService); }
`
原提问者GitHub用户shaotongyao
两个spring 的ApplicationContext,在两个Configuration 中配置不同的@componentscan。
比如:在MvcConfiguration中加入: @componentscan(basePackages = "com.a.b", includeFilters = { @filter(classes = Controller.class)}, useDefaultFilters = false)
让getServletConfigClasses,只处理Controller注解! 其实也就是让所有的Configuration 都不处理 @configuration 就可以避免重复扫描。 Configuration 都通过: com.egege.mini.configuration.ServletInitializer#getRootConfigClasses; com.egege.mini.configuration.ServletInitializer#getServletConfigClasses
这两个方法手工指定。就可以彻底解决在Servlet 3.0+ 的环境中,重复扫描的问题!
原回答者GitHub用户roc-cn
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。