springboot +dubbo项目启动后获取ApplicationContext为null

1,springboot+dubbo整合,启动项目获取ApplicationContext为空

package org.source.dsmh;

import java.util.concurrent.ThreadPoolExecutor;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.source.dsmh.service.RecordLogToUnifiedDb;
import org.source.dsmh.service.impl.RecordLogToUnifiedDbImp;
import org.source.dsmh.utils.mongodb.SpringContextUtil;
import org.source.dsmh.utils.config.ThreadPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class,MongoAutoConfiguration.class})
@EnableDubbo(scanBasePackages="org.source.dsmh.service")
public class DsmhThirdPhserviceWdApplication {

    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;
    /**
     * 异步线程池
     * 
     * @return
     */
    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(ThreadPoolConfig.getCorePoolSize()); // 线程池维护线程的最少数量
        taskExecutor.setMaxPoolSize(ThreadPoolConfig.getMaxPoolSize()); // 线程池维护线程的最大数量
        taskExecutor.setQueueCapacity(ThreadPoolConfig.getQueueCapacity()); // 缓存队列
        taskExecutor.setKeepAliveSeconds(ThreadPoolConfig.getKeepAliveSeconds()); // 允许的空闲时间
        // 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略
        // ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return taskExecutor;
    }
    
    @Bean
    public SpringContextUtil getSpringContextUtil() {
        return new SpringContextUtil();
    }
    
    @Bean
    public RecordLogToUnifiedDb getRecordLogToUnifiedDb() {
        return new RecordLogToUnifiedDbImp();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(DsmhThirdPhserviceWdApplication.class, args);
    }
    
    
    
    
    
}

2,pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.source.dsmh</groupId>
  <artifactId>dsmh-third-phserviceinfowd</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  
  
  <repositories>
        <!-- ds远程仓库 -->
        <repository>
            <id>maven-nexus-ds</id>
            <name>maven-nexus-ds</name>
            <layout>default</layout>
            <url>http://101.200.214.46:8082/nexus/repository/maven-releases/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <commons-lang3-version>3.4</commons-lang3-version>
        <commons-io-version>1.3.2</commons-io-version>
        <fastjson-version>1.2.8</fastjson-version>
        <boot-dubbo-version>2.0.0</boot-dubbo-version>
        <zkclient-version>0.10</zkclient-version>
        <dsmh-common-version>[1.0.0,)</dsmh-common-version>
        <dsmh-common-mongodb-version>RELEASE</dsmh-common-mongodb-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <!-- 排除springboot默认的logback日志框架 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 引入log4j日志依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>

        <!-- 引入spring aop依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        
        <dependency>
               <groupId>joda-time</groupId>
               <artifactId>joda-time</artifactId>
          </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- jetty -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <!-- 资源配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- apache 工具类 -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io-version}</version>
        </dependency>


        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson-version}</version>
        </dependency>


        <!-- alibaba dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!-- 因为使用的是 zookeeper 作为注册中心,所以要添加 zookeeper 依赖 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.12</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--使用curator 作为zookeeper客户端-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.source.dsmh</groupId>
            <artifactId>dsmh-common</artifactId>
            <!-- <version>${dsmh-common-version}</version> -->
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.source.dsmh</groupId>
            <artifactId>dsmh-common-mongodb</artifactId>
            <version>${dsmh-common-mongodb-version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
        </dependency>
        <!--Uber MongoDB Java Driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.0.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.60</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>org.source.dsmh.DsmhThirdPhserviceWdApplication</mainClass>
                    <layout>ZIP</layout>
                    
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <!-- 分离出代码包
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                         -->
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3,

package org.source.dsmh.service.vaccineorder;

import org.apache.dubbo.config.annotation.Service;
import org.source.dsmh.service.ThirdDataService;
import org.source.dsmh.service.ThirdEnterService;
import org.source.dsmh.utils.DataTemplate;
import org.source.dsmh.utils.SpringContextUtil;
import org.springframework.context.ApplicationContext;


/**
 * third-consult入口,只做功能分发,不处理具体业务
 * 
 * @author hzc
 *
 */

@Service(group = "third-vaccineorder")
public class ThirdVaccineorderServiceImpl implements ThirdEnterService {
    
    //private ThirdDataService thirdDataService;

    @Override
    public DataTemplate thirdVisit(String paramJson, String function, String method) {
        
        ApplicationContext ac=SpringContextUtil.getApplicationContext();
        System.out.println(ac.getBeanDefinitionNames());
        // 根据方法名获取实例
        ThirdDataService thirdDataService = SpringContextUtil.getBean("third-" + function + "-" + method, ThirdDataService.class);

        if (thirdDataService == null) {
            return DataTemplate.error("功能暂未实现!");
        } else {
            return thirdDataService.thirdData(paramJson, function, method);
        }
    }
}

 原因:获取bean的ApplicationContext引用的包与application包new的不是同一个类文件

解决方案:将调用的ApplicationContext改为同一类文件引用

springboot +dubbo项目启动后获取ApplicationContext为null

上一篇:Android开发学习——使用通知


下一篇:手机安装测试包的方法