2015-02-26 30 views
13

Có ai đã vô hiệu hóa tự động cấu hình cho mongodb trong spring-boot không?Làm thế nào để vô hiệu hóa cấu hình tự động spring-data-mongodb trong spring-boot

Tôi đang thử khởi động mùa xuân bằng spring-data-mongodb; Sử dụng cấu hình dựa trên java; Sử dụng spring-boot 1.2.1.RELEASE, tôi nhập spring-boot-starter-web và 'pom cha mẹ của nó cho quản lý phụ thuộc. Tôi cũng nhập spring-data-mongodb (đã thử spring-boot-starter-mongodb).

Tôi cần kết nối với hai máy chủ MongoDB khác nhau. Vì vậy, tôi cần phải cấu hình hai bộ trường hợp cho kết nối mongo, MongoTemplate, vv Tôi cũng muốn tắt tự động cấu hình. Vì tôi đang kết nối với nhiều máy chủ, tôi không cần phải có một MongoTemplate và GridFsTemplate bean mặc định được tự động cấu hình.

lớp chính của tôi trông như thế này:

@Configuration 
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) 
@ComponentScan 
//@SpringBootApplication // @Configuration @EnableAutoConfiguration @ComponentScan 
public class MainRunner { 

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

hai lớp cấu hình Mongo của tôi trông như thế này:

@Configuration 
@EnableMongoRepositories(basePackageClasses = {Test1Repository.class}, 
     mongoTemplateRef = "template1", 
     includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test1Repository")} 
) 
public class Mongo1Config { 

    @Bean 
    public Mongo mongo1() throws UnknownHostException { 
     return new Mongo("localhost", 27017); 
    } 

    @Primary 
    @Bean 
    public MongoDbFactory mongoDbFactory1() throws UnknownHostException { 
     return new SimpleMongoDbFactory(mongo1(), "test1"); 
    } 

    @Primary 
    @Bean 
    public MongoTemplate template1() throws UnknownHostException { 
     return new MongoTemplate(mongoDbFactory1()); 
    } 
} 

@Configuration 
@EnableMongoRepositories(basePackageClasses = {Test2Repository.class}, 
     mongoTemplateRef = "template2", 
     includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test2Repository")} 
) 
public class Mongo2Config { 

    @Bean 
    public Mongo mongo2() throws UnknownHostException { 
     return new Mongo("localhost", 27017); 
    } 

    @Bean 
    public MongoDbFactory mongoDbFactory2() throws UnknownHostException { 
     return new SimpleMongoDbFactory(mongo2(), "test2"); 
    } 

    @Bean 
    public MongoTemplate template2() throws UnknownHostException { 
     return new MongoTemplate(mongoDbFactory2()); 
    } 
} 

Với thiết lập này mọi thứ hoạt động. Nếu tôi xóa các chú thích @Primary từ các bean mongoDbFactory1 và template1, ứng dụng sẽ thất bại với một ngoại lệ có vẻ như tự động cấu hình không bị vô hiệu hóa. nhắn ngoại lệ được liệt kê dưới đây:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950) 
    at com.fourexpand.buzz.web.api.template.MainRunner.main(MainRunner.java:26) 
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:98) 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:75) 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:378) 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:155) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) 
    ... 7 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.core.io.ResourceLoader org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.resourceLoader; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gridFsTemplate' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.mongodb.MongoDbFactory]: : No qualifying bean of type [org.springframework.data.mongodb.MongoDbFactory] is defined: expected single matching bean but found 2: mongoDbFactory2,mongoDbFactory1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.MongoDbFactory] is defined: expected single matching bean but found 2: mongoDbFactory2,mongoDbFactory1 
+0

Bạn có bất cứ lớp khác chú thích với 'SpringBootApplication' hoặc' EnableAutoConfiguration'? –

+3

Cố gắng loại trừ 'MongoRepositoriesAutoConfiguration'. –

+0

@AndyWilkinson lúng túng tôi đã có một lớp khác được chú thích bằng SpringBootApplication. Tôi đã có nhiều hơn một điểm vào - chính để thử nghiệm và Apache Daemon + jsvc runner cho sản xuất và tôi chỉ cần sao chép/dán tất cả các chú thích thay vì đặt chúng vào một nơi phổ biến ... Loại trừ MongoRepositoriesAutoConfiguration hóa ra là không cần thiết ... –

Trả lời

4

Như đã chỉ ra bởi Andy Wilkinson trong ý kiến, khi sử dụng EnableAutoConfiguration với danh sách loại trừ chắc chắn rằng không có các lớp khác chú thích với EnableAutoConfiguration hoặc SpringBootApplication.

0

Trường hợp sử dụng của tôi hơi khác một chút. Tôi đã có một yêu cầu cho 2 cơ sở dữ liệu khác nhau trong cùng một dự án. Tôi đã mở rộng các lớp cấu hình tự động và thêm chú thích hồ sơ.

@Profile("mongo") 
@Configuration 
public class CustomMongoAutoConfiguration extends MongoAutoConfiguration { 

    public CustomMongoAutoConfiguration(
     MongoProperties properties, 
     ObjectProvider<MongoClientOptions> options, 
     Environment environment) { 
     super(properties,options,environment); 
    } 
} 

@Profile("mongo") 
@Configuration 
@EnableConfigurationProperties(MongoProperties.class) 
public class CustomMongoDataAutoConfiguration extends MongoDataAutoConfiguration { 

    public CustomMongoDataAutoConfiguration(
     ApplicationContext applicationContext, 
     MongoProperties properties) { 
     super(applicationContext,properties); 
    } 

} 
Các vấn đề liên quan