[apache/dubbo]采用javaConfig 配置AnnotationBean导致 controller,service等bean 重复初始化。

2024-07-31 957 views
6

`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 // // @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);
}

`

回答

1

构造器调用了两次,我把这段代码@Bean public static AnnotationBean annotationBean() { AnnotationBean annotationBean = new AnnotationBean(); annotationBean.setPackage("com.lincomb.linktown.front"); return annotationBean; }注释了就不会了,我确定就是这段代码引起的,但是不知道什么原因,求各位老师替我看看

8

现在推荐使用xml配置,注解配置功能当前并不是很完善。注解功能将会是2.5.7版本优先解决的,预计10月中下旬发布

1

你的具体问题我稍后看一下

5

你这个应该是启动了两个spring上下文,各自扫描了一遍。

2

检查一下spring配置,是否重复扫描了

6

两个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+ 的环境中,重复扫描的问题!

8

大佬,我也想用这种方式配个不用xml的,可是一直报错,能不能帮我看一下,蟹蟹 (@. @)