The profile of the injection SpringBoot

@PropertySource&@ImportResource&@Bean

@ PropertySource : load the specified configuration file;

/ ** 
 * The value of the attribute of each configuration file is mapped to the component 
 * @ConfigurationProperties: SpringBoot will tell all of the properties and the profile of this class of binding the relevant configuration; 
 * prefix = "Person ": profile in which all of the following attributes one mapping 
 * 
 * only function of this component is @ConfigurationProperties container components to the container provided; 
 * @ConfigurationProperties (prefix =" the Person ") acquired from the global default configuration file value; 
 * 
 * / 
@PropertySource (value = { "CLASSPATH: person.properties" }) 
@Component 
@ConfigurationProperties (prefix = "Person" )
 // @Validated 
public  class the Person { 
    / ** 
     * <the bean class = "the Person ">
     * <Property name = "lastName" value = " literal / $ {key}, the value profile acquired from the environment variable / # of SpEL {}"> </ Property> 
     * <the bean /> 
     * / // lastName must mailbox format
    // @email
     // @Value ( "person.last-$ {name}") Private String lastName;
     // @Value ( "#. 11 * {2}") Private Integer Age;
     // @Value ( "to true") Private Boolean BOSS;
   
    
    
    

@ ImportResource : Importing Spring configuration file, so that the contents of the configuration file which is to take effect;

Spring Boot there is no Spring configuration file, write our own configuration files, it can not automatically identify;

Would like Spring configuration file to take effect, came loaded; @ ImportResource marked on a configuration class

@ImportResource (locations = { "CLASSPATH: the beans.xml" }) 
introducing allowed to take effect profile Spring

Not to write the Spring configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
​
​
    <bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>

SpringBoot recommended way to add components to a container; recommended way to use the full annotated

1, configuration class @Configuration ------> the Spring profile

2, using @Bean add a component to the container

/ ** 
 * @Configuration: indicates the current class is a class configured; Spring is replaced prior to the configuration file 
 * 
 * in the configuration file with <bean> <bean /> tag add components 
 * 
 * / 
@Configuration 
public  class MyAppConfig { 
    // the return value is added to the vessel; container id this component is the default method name 
    @Bean
     public the HelloService helloService02 () { 
        System.out.println ( "class configuration @Bean component is added to the container ... " );
         return  new new the HelloService (); 
    } 
}

 

 

Guess you like

Origin www.cnblogs.com/dalianpai/p/11772382.html