SpringBoot 2.0 @Configuration 非同级项目中无法加载

前言:该文章只是记录自己平时的点点滴滴.来激励自己成长.长成自己喜欢的人!

环境:

  1.  电脑 Mac 10.13.1
  2.  IntelliJ IDEA 2018.2 (Ultimate Edition)
  3.  Spring boot  2.0.1.RELEASE
  4.  项目全路径a.b.c.*
  5. Maven 项目


实现方案: (Maven项目)
    1.最高级pom 管理全部jar版本 dependencyManagement -> dependencies


        

            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            
            
                org.projectlombok
                lombok
                1.16.22
                provided
                true
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                1.3.2
            
            
            
                com.github.pagehelper
                pagehelper-spring-boot-starter
                1.2.5
            
            
            
                com.alibaba
                druid-spring-boot-starter
                1.1.9
            
            
            
                mysql
                mysql-connector-java
                5.1.46
                runtime
            
            
            
                io.springfox
                springfox-swagger-ui
                2.6.1
            
            
                io.springfox
                springfox-swagger2
                2.6.1
            
            
            
                org.springframework.boot
                spring-boot-starter-data-elasticsearch
                2.0.1.RELEASE
            
            
            
                io.searchbox
                jest
                5.3.3
            
            
                net.java.dev.jna
                jna
                4.5.1
            
            
                org.springframework.cloud
                spring-cloud-starter-openfeign
                2.0.0.RELEASE
            

            
            
                org.redisson
                redisson
                3.6.5
            
            
            
                org.apache.commons
                commons-lang3
                3.3.2
            
            
            
                com.alibaba
                fastjson
                1.2.49
            

        
    

    2.并且添加必要的依赖


        
        
            org.projectlombok
            lombok
        
    


    2.次高级parent -> pom


        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-config-client
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            io.springfox
            springfox-swagger-ui
        
        
            io.springfox
            springfox-swagger2
        
    


实现方式一:

全局扫描
    @SpringBootApplication(scanBasePackages="a.b.c") 

扫描局部

    @SpringBootApplication(scanBasePackages="a.b.c.d") 

eg:

@SpringBootApplication(scanBasePackages = "a.b.c")
@EnableEurekaClient
@Slf4j
public class TallyApplication implements CommandLineRunner{

    public static void main(String[] args){
        SpringApplication app = new SpringApplication(TallyApplication.class);
        app.run(args);
    }
    //程序首次启动调用
    public void run(String... strings){
        log.info("TallyApplication Server Successfully");
    }
}

注意:

在a.b.c.e 中添加了@Configuration 相关配置. 如果不开启全局扫描,会出现配置文件无法加载的问题. 请谨慎开启全局扫描.

 

实现方式二: 获取Spring bean

    实现接口ApplicationContextAware 

public class AppContextHolder implements ApplicationContextAware {  
  
    // Spring应用上下文环境  
    private static ApplicationContext applicationContext;  
  
    /** 
     * 实现ApplicationContextAware接口的回调方法。设置上下文环境 
     *  
     * @param applicationContext 
     */  
    public void setApplicationContext(ApplicationContext applicationContext) {  
        SpringContextUtil.applicationContext = applicationContext;  
    }  
  
    /** 
     * @return ApplicationContext 
     */  
    public static ApplicationContext getApplicationContext() {  
        return applicationContext;  
    }  
  
    /** 
     * 获取对象 
     *  
     * @param name 
     * @return Object
     * @throws BeansException 
     */  
    public static Object getBean(String name) throws BeansException {  
        return applicationContext.getBean(name);  
    }  
}

 

eg:

@SpringBootApplication
@EnableEurekaClient
@Slf4j
@Import(AppContextHolder.class)
public class TallyApplication implements CommandLineRunner{

    public static void main(String[] args){
        SpringApplication app = new SpringApplication(TallyApplication.class);
        app.run(args);
    }
    //程序首次启动调用
    public void run(String... strings){
        log.info("TallyApplication Server Successfully");
    }
}

注意:

AppContextHolder.class 获取当前微服务中的bean-包含maven-jar. 在需要使用的时候使用.eg:

public class RedissonClientUtil {

    private static final String KEY_PREFIX = "LOCK:";

	private static RedissonClient redissonClient;

	@Bean
	private static RedissonClient getRedisson(){
		RedisProperties redisProperties = (RedisProperties) AppContextHolder.getBean(RedisProperties.class);
		Config config = new Config();
		SingleServerConfig singleServerConfig = config.useSingleServer();
		singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());
		singleServerConfig.setDatabase(redisProperties.getDatabase());
		singleServerConfig.setPassword(redisProperties.getPassword());
		return Redisson.create(config);
	}
    /**
	 * @return
	 */
	public static RedissonClient getInstance(){
		if(redissonClient == null){
			synchronized (RedissonClient.class) {
				if(redissonClient == null){
					redissonClient = getRedisson();
				}
			}
		}
		return redissonClient;
	}

	/**
	 * 获取锁(key必须唯一)
	 * @param key
	 * @return
	 */
	public static RLock getRedisLock(String key){
		return getInstance().getLock(KEY_PREFIX+key);
	}
}

 

你可能感兴趣的:(spring,cloud)