@@ PropertySource spring @value and simple to use annotations

@Value Note: You can use the basic injection string EL expression data is read from the configuration file 
@PropertySource for introducing a single configuration file
@ PropertySources for introducing a plurality of profiles
@PropertySource or @ PropertySources data into the environment variables are present ConfigurableEnvironment in
a new cat.properties resouces file folder is written as parent = Tiger
public  class Cat { 
    @Value ( "cat") // direct injection string 
    Private String name; 
    @Value ( "# 12 is {2} +") // support EL expressions 
    Private  int Age; 
    @Value ( "{$ parent } ") // configuration file read 
    Private String parent;
     public cat () { 
        System.out.println ( " cat initialized " ); 
    } 

    @Override 
    public String toString () {
         return " {cat "+ 
                " name = ' "+ name +' \ '' +  
                ", = Age "+ age +
                ", parent = ' "+ parent + '\'' +
                '}';
    }
}
@Import({Cat.class})
@PropertySources({@PropertySource(value ="cat.properties")})
public class Appconfig {
}
public class Demo {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Appconfig.class);
        Cat bean = context.getBean(Cat.class);
        System.out.println(bean); //Cat{name='猫', age=14, parent='tiger'}
        ConfigurableEnvironment environment = context.getEnvironment();
        System.out.println(environment.getProperty("parent")); //tiger


    }
}

 

 

 

 




Guess you like

Origin www.cnblogs.com/yangxiaohui227/p/11961948.html