简体   繁体   English

合并许多application.properties文件而不是Spring Boot上的替换?

[英]merge many application.properties files instead of replace on Spring Boot?

i am using Spring Booot MVC 1.2.2.RELEASE. 我正在使用Spring Booot MVC 1.2.2.RELEASE。 I have my multi-module project which is structured like this: 我有我的多模块项目,结构如下:

example-backend-development(parent)
---> example-backend-development-domain
---> example-backend-development-service

I want to make both modules independent, or at least domain from service, since service has domain module in its classpath. 我想让两个模块独立,或者至少从服务域创建域,因为服务在其类路径中具有域模块。 So both modules have their application.properties file in their respective resources folder. 因此,两个模块都将application.properties文件放在各自的资源文件夹中。 I want to do it like this way because i don't want to handle all properties from service module (eg Jpa properties, which belongs to domain module). 我想这样做,因为我不想处理来自服务模块的所有属性(例如,属于域模块的Jpa属性)。

So for example i have this application.properties in service module 例如,我在服务模块中有这个application.properties

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.level.com.example.movies: DEBUG

And this one in domain module 而这一个在域模块中

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db
spring.datasource.username=root
spring.datasource.password=mypassword

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true

The problem is that when i boot my service, as they both application.properties are in the service classpath, then it replace the application.properties from service with the other one from domain. 问题是,当我启动我的服务时,因为它们都是application.properties在服务类路径中,然后它将来自服务的application.properties替换为来自域的另一个。 And i want to merge them if they have the same name, as this case. 如果它们具有相同的名称,我想合并它们,就像这种情况一样。 Here the spring logs (DEBUG+INFO) 这里的春天记录(DEBUG + INFO)

 :: Spring Boot ::        (v1.2.2.RELEASE)

2015-05-12 15:06:24.794 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:24.797 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:24.798 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-05-12 15:06:24.798 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-05-12 15:06:24.798 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
2015-05-12 15:06:24.840  INFO 7728 --- [           main] c.e.movies.service.ApplicationRunner     : Starting ApplicationRunner on despubuntu-ThinkPad-E420 with PID 7728 (/home/despubuntu/Documents/Workspace/example-backend-development/example-backend-development/example-backend-development-service/target/classes started by despubuntu in /home/despubuntu/Documents/Workspace/example-backend-development/example-backend-development/example-backend-development-service)
2015-05-12 15:06:24.841 DEBUG 7728 --- [           main] c.e.movies.service.ApplicationRunner     : Running with Spring Boot v1.2.2.RELEASE, Spring v4.1.6.RELEASE
2015-05-12 15:06:24.915  INFO 7728 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@55782201: startup date [Tue May 12 15:06:24 ART 2015]; root of context hierarchy
2015-05-12 15:06:25.034 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [class path resource [application.properties]] PropertySource with lowest search precedence
2015-05-12 15:06:25.083 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Replacing [class path resource [application.properties]] PropertySource with [class path resource [application.properties]]
2015-05-12 15:06:26.033  INFO 7728 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-05-12 15:06:26.753 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [applicationConfig: [classpath:/application.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties]
2015-05-12 15:06:26.753 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Removing [applicationConfigurationProperties] PropertySource
2015-05-12 15:06:26.753 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Removing [defaultProperties] PropertySource
2015-05-12 15:06:27.178  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.datasource.CONFIGURATION_PROPERTIES' of type [class org.springframework.boot.autoconfigure.jdbc.DataSourceProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.179  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration' of type [class org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.473  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'dataSource' of type [class org.apache.tomcat.jdbc.pool.DataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.474  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.example.movies.domain.config.PersistenceConfiguration' of type [class com.example.movies.domain.config.PersistenceConfiguration$$EnhancerBySpringCGLIB$$1afe0f6c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.665  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$8c497c14] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.812  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionAttributeSource' of type [class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.823  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionInterceptor' of type [class org.springframework.transaction.interceptor.TransactionInterceptor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:27.827  INFO 7728 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.config.internalTransactionAdvisor' of type [class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2015-05-12 15:06:28.350  INFO 7728 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-05-12 15:06:28.737  INFO 7728 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-05-12 15:06:28.739  INFO 7728 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.20
2015-05-12 15:06:28.873  INFO 7728 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-05-12 15:06:28.873 DEBUG 7728 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2015-05-12 15:06:28.873  INFO 7728 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3962 ms
2015-05-12 15:06:29.077 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:29.077 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:29.078 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-05-12 15:06:29.078 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-05-12 15:06:29.079 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
2015-05-12 15:06:29.132 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:29.132 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:29.133 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-05-12 15:06:29.133 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-05-12 15:06:29.133 DEBUG 7728 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
2015-05-12 15:06:29.746  INFO 7728 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-05-12 15:06:29.751  INFO 7728 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-05-12 15:06:29.751  INFO 7728 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-05-12 15:06:29.781 DEBUG 7728 --- [ost-startStop-1] o.s.web.filter.HiddenHttpMethodFilter    : Initializing filter 'hiddenHttpMethodFilter'
2015-05-12 15:06:29.781 DEBUG 7728 --- [ost-startStop-1] o.s.web.filter.HiddenHttpMethodFilter    : Filter 'hiddenHttpMethodFilter' configured successfully
2015-05-12 15:06:29.787 DEBUG 7728 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2015-05-12 15:06:29.857  INFO 7728 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2015-05-12 15:06:31.701  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@55782201: startup date [Tue May 12 15:06:24 ART 2015]; root of context hierarchy
2015-05-12 15:06:31.770 DEBUG 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking for request mappings in application context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@55782201: startup date [Tue May 12 15:06:24 ART 2015]; root of context hierarchy
2015-05-12 15:06:31.780  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v3/clients],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<com.example.movies.api.models.response.ClientResponseDTO> com.example.movies.service.controller.ClientController.postClient(com.example.movies.api.models.request.ClientRequestDTO) throws com.example.movies.domain.exception.ValidationException
2015-05-12 15:06:31.780  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v3/clients/{clientId}],methods=[PUT],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<com.example.movies.api.models.response.ClientResponseDTO> com.example.movies.service.controller.ClientController.putClient(com.example.movies.api.models.request.ClientRequestDTO,java.lang.String) throws com.example.movies.domain.exception.ValidationException
2015-05-12 15:06:31.780  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v3/clients],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<com.example.movies.domain.feature.commons.model.PaginatedResponse<com.example.movies.api.models.response.ClientResponseDTO>> com.example.movies.service.controller.ClientController.getClients(com.example.movies.api.models.request.model.PagingRequestDTO) throws com.example.movies.domain.exception.ValidationException
2015-05-12 15:06:31.781  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v3/clients/{clientId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<com.example.movies.api.models.response.ClientResponseDTO> com.example.movies.service.controller.ClientController.getClient(java.lang.String) throws com.example.movies.domain.exception.ValidationException
2015-05-12 15:06:31.781  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v3/clients/{clientId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.example.movies.service.controller.ClientController.deleteClient(java.lang.String) throws com.example.movies.domain.exception.ValidationException
2015-05-12 15:06:31.782  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/health-check],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<com.example.movies.service.model.HealthCheck> com.example.movies.service.controller.MainController.doHealthCheck()
2015-05-12 15:06:31.782  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/version],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.example.movies.service.controller.MainController.getVersion()
2015-05-12 15:06:31.785  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-05-12 15:06:31.786  INFO 7728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-05-12 15:06:31.816 DEBUG 7728 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Looking for URL mappings in application context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@55782201: startup date [Tue May 12 15:06:24 ART 2015]; root of context hierarchy
2015-05-12 15:06:31.816 DEBUG 7728 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
2015-05-12 15:06:31.816 DEBUG 7728 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
2015-05-12 15:06:31.816 DEBUG 7728 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'org.springframework.context.annotation.internalRequiredAnnotationProcessor': no URL paths identified
....

I searched a lot but i couldn't find anything. 我搜索了很多,但我找不到任何东西。 Do you have any idea? 你有什么主意吗? Thanks in advance! 提前致谢!

Assuming You are booting your application from example-backend-development-service , I would suggest doing the following 假设您正在从example-backend-development-service启动应用程序,我建议您执行以下操作

Rename application.properties in example-backend-development-domain to `domainapplication.properties. example-backend-development-domain application.properties重命名为`domainapplication.properties。 While Booting Your application, Override the property configuration as shown below. 在引导您的应用程序时,覆盖属性配置,如下所示。 I tested this and it does work for me. 我对此进行了测试,它对我有用。

 /**
     * Hello world!
     *
     */
    @SpringBootApplication
    @PropertySource(value = { "application.properties","domainapplication.properties" })
    public class App 
    {
        @Autowired
        private ModuleService moduleService;
        public static void main(String[] args) throws Exception {
            ApplicationContext applicationContext= SpringApplication.run(App.class, args);
            ModuleService moduleService = (ModuleService) applicationContext.getBean(ModuleService.class);
            System.out.println(moduleService.getProp());
            String[] beanNames = applicationContext.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        }
    }

EDIT:- 编辑:-

If you are booting from domain as well just use the following in the reference here 如果您也从域启动,请在此处参考中使用以下内容

If you don't like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. 如果您不喜欢application.properties作为配置文件名,则可以通过指定spring.config.name环境属性来切换到另一个。 You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths). 您还可以使用spring.config.location环境属性(以逗号分隔的目录位置列表或文件路径)来引用显式位置。

$ java -jar myproject.jar --spring.config.name=myproject

or 要么

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? Spring 引导类路径配置文件覆盖外部 application.properties - Spring boot classpath configuration files overrides external application.properties spring boot jar中的application.properties文件在哪里? - Where are the application.properties files in a spring boot jar? spring boot application.properties 文件不会自动完成代码 - spring boot application.properties files does not autocomplete the code spring-boot如何添加多个application.properties文件? - How to add multiple application.properties files in spring-boot? 如何将 application.properties 与 application-default.properties 合并并包含在 Spring Boot &gt;2.4 的其他测试配置文件中 - How to merge application.properties with application-default.properties and include in other test profiles with Spring Boot >2.4 Spring Boot未加载application.properties - Spring Boot not loading application.properties 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? Spring Boot application.properties生命周期 - Spring Boot application.properties lifecyle 来自application.properties的Spring Boot配置 - Spring Boot configuration from application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM