In the development is the time we use spring, mainly he for our development to provide a lot of convenient way, control inversion, dependency injection and aspect-oriented programming is spring's three core ideas, configuration file reading can also be said to be a dependency injection, but this is from the configuration file dependency injected in, How do you see the data injected into the bean in the configuration file? (Java Learning Communication ③ Group: 256909960, Welcome to join)
first, through the @vaule to configureThis configuration is done by loading the configuration file with spring and injecting the corresponding value into the property using the @value annotation.
1. pom fileThe dependencies needed
<Dependency><groupId>Org.springframework</groupId><Artifactid>Spring-core</Artifactid><version>4.3.7.RELEASE</version></Dependency><Dependency><groupId>Org.springframework</groupId><Artifactid>Spring-context</Artifactid><version>4.3.7.RELEASE</version></Dependency><Dependency><groupId>Commons-logging</groupId><Artifactid>Commons-logging-api</Artifactid><version>1.1</version></Dependency>
(Java Learning Communication ③ Group: 256909960, Welcome to join)
2. Configuration ClassIn this case, the spring annotations are configured directly, and the XML configuration method is subsequently written
@Component // 1 @PropertySource (" Classpath:aaa.properties ") // 2 public class valueproperties {@Value ( "${name}") // 3 private String name; @Value ( "${age}") // 4 private Integer age; public void hello () {System.out.println ( "My Name:" +name+ "| Age:" +age); }}
1. @component allow spring to scan to this package
2. @PropertiesSource read the configuration file under Load Classpath (only properties)
3. Inject the data in the configuration file name and age,
For example, the data in the configuration file is such a host.ip, then the injection should write ${host.ip}
(Java Learning Communication ③ Group: 256909960, Welcome to join)
3. Call Test
@ComponentScan//1publicclass appconfiguration { public Static void Main (string[] args) { new annotationconfigapplicationcontext (appconfiguration. Class); Applicationcontext.getbean (valueproperties. class ). Hello (); }
1, scan package, this class to be in the above class valueproperties or higher package
(Java Learning Communication ③ Group: 256909960, Welcome to join)
second, through the @configurationproperties to configureThis is an annotation in springboot.
1. pom file
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter</Artifactid></Dependency><Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-configuration-processor</Artifactid></Dependency>
(Java Learning Communication ③ Group: 256909960, Welcome to join)
2. Configuration Class
@Component//1@ConfigurationProperties (prefix = "Server.irich")//2
The configuration file cannot be loaded here with @propertysource
Public classbootconfiguration {PrivateString name; PrivateInteger age; Public voidHello () {System.out.println ("My Name:" +name+ "| Age: "+Age ); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; }
Configuration file
Server.irich.name= Leslie Server.irich.name=45
configuration file
1. Add this bean to the container
2, load the configuration file, prefix is only load the file specified prefix data
There must be a setter method.
Note: After 1.5 @configurationproperties did not have locations this function, but he can and @propertysource, @Import combination use
(Java Learning Communication ③ Group: 256909960, Welcome to join)
3. Call Test
@ComponentScan//1@EnableConfigurationProperties//2@PropertySource ("Classpath:aaa.properties")//3//@SpringBootApplication Public classBootconfigurationapp { Public Static voidMain (string[] args) {ApplicationContext ApplicationContext=NewAnnotationconfigapplicationcontext (Bootconfigurationapp.class); Applicationcontext.getbean (bootconfiguration.class). Hello ();//Springapplication.run (Bootconfigurationapp.class,args); }}
1. Configure the Scan package
2. When @enableconfigurationproperties annotations are applied to your @configuration, any beans that are @configurationproperties annotated will be automatically configured with the environment attribute
3, load the configuration file before @configurationproperties for the property assignment, can not write the configuration file that needs to be added in the same location as @configurationproperties
(Java Learning Communication ③ Group: 256909960, Welcome to join)
Second, spring configuration file read