开发者社区 > 云原生 > 正文

采用javaConfig 配置AnnotationBean导致 controller,service

`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

展开
收起
大圣东游 2023-05-11 20:36:27 260 0
1 条回答
写回答
取消 提交回答
  • 两个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

    2023-05-12 12:01:40
    赞同 展开评论 打赏

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载