springboot跨package掃描

springboot啓動文件

追加@ComponentScan

啓動文件SpringApplication.java在com.aaa的package下,@SpringBootApplication自動加載com.aaa掃描。

如果只追加@ComponentScan(basePackages = "com.bbb"),會是com.aaa掃描配置覆蓋掉。

需要追加package數組(包含自動掃描com.aaa)來覆蓋springboot的自動配置@ComponentScan(basePackages = {"com.aaa","com.bbb"})

package com.aaa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@ComponentScan(basePackages = {"com.aaa","com.bbb"})
@SpringBootApplication
public class SpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackOfficeApplication.class, args);
    }

	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("classpath:messages_backend");
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章