Spring-Boot: How do I reference application.properties in an @ImportResource

前端 未结 2 463
盖世英雄少女心
盖世英雄少女心 2021-01-04 10:32

I have an applicationContext.xml file in my Spring Boot application. In this file, it has a property placeholder - ${profile.services.url} - that\'s used to configure the \"

2条回答
  •  一向
    一向 (楼主)
    2021-01-04 11:04

    I've got the following code:

    package demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    
    import java.util.Collection;
    
    @ComponentScan
    @EnableAutoConfiguration
    public class Application {
    
        public static void main(String[] args) {
            ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
            Collection shouldBeConfigured = applicationContext.getBeansOfType(Foo.class).values();
            System.out.println(shouldBeConfigured.toString());
        }
    }
    
    @Configuration
    @ImportResource("/another.xml")
    class XmlImportingConfiguration {
    }
    
    class Foo {
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Foo{" +
                    "name='" + name + '\'' +
                    '}';
        }
    
    }
    

    I have a Spring XML configuration file, another.xml:

    
    
    
        
    
        
        
            
        
    
        
        
            
        
    
    
    

    I have the following pom.xml:

    
    
        4.0.0
    
        org.demo
        demo
        0.0.1-SNAPSHOT
    
        demo
        Demo project
    
        
            org.springframework.boot
            spring-boot-starter-parent
            1.0.0.RC1
        
    
        
    
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
        
    
        
            demo.Application
            1.7
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
        
            
                spring-snapshots
                Spring Snapshots
                http://repo.spring.io/snapshot
                
                    true
                
            
            
                spring-milestones
                Spring Milestones
                http://repo.spring.io/milestone
                
                    false
                
            
        
        
            
                spring-snapshots
                Spring Snapshots
                http://repo.spring.io/snapshot
                
                    true
                
            
            
                spring-milestones
                Spring Milestones
                http://repo.spring.io/milestone
                
                    false
                
            
        
    
    
    

    Finally, I have two .properties files, another.properties, and application.properties:

    # application.properties 
    some.property=Test
    

    and..

    # another.properties 
    name.property=Another
    

    When I run this, the output is:

    [Foo{name='Another'}, Foo{name='Test'}]

    Which would seem to work.

    I'm not quite sure I am understanding the error. Can you elaborate, or confirm this seems satisfactory behavior for you, too please?

提交回复
热议问题