With @propertysource, you can specify the read configuration file and get the value by @value annotations, using:
PackageCN.QLQ;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.PropertySource; @Configuration//This annotation indicates that the class is a spring configuration, which is equivalent to an XML file@ComponentScan (basepackages = "CN.QLQ")//Configuring the Scan package@PropertySource (value = {"Classpath:jdbc.properties", "xxxxx", "yyyyy"}, Ignoreresourcenotfound =true) Public classSpringconfig {@Bean//This annotation indicates that it is a bean object, which is equivalent to the <bean> in XML PublicUserdao Getuserdao () {return NewUserdao ();//Direct New Object Demo }}
Problem:
1. Read multiple configuration files:
2. Ignore error if file does not exist:
Open the @propertysource annotations source to see:
--------------------Configuration database Connection Pool Example-------------
Import dependency:
<!--Connection Pool - <Dependency> <groupId>Com.jolbox</groupId> <Artifactid>Bonecp-spring</Artifactid> <version>0.8.0.RELEASE</version> </Dependency>
Before the spring XML Configuration Connection pool:
<!--defining a data source - <BeanID= "DataSource"class= "Com.jolbox.bonecp.BoneCPDataSource"Destroy-method= "Close"> <!--Database-driven - < Propertyname= "Driverclass"value= "${jdbc.driverclassname}" /> <!--Jdbcurl of the corresponding drive - < Propertyname= "Jdbcurl"value= "${jdbc.url}" /> <!--user name of the database - < Propertyname= "username"value= "${jdbc.username}" /> <!--password for the database - < Propertyname= "Password"value= "${jdbc.password}" /> <!--Check the time interval for idle connections in the database connection pool, in units of minutes, default: 240, set to 0 if you want to cancel - < Propertyname= "Idleconnectiontestperiod"value= "$" /> <!--maximum surviving time for unused links in the connection pool, in units of minutes, default: 60, if you want to live forever set to 0 - < Propertyname= "Idlemaxage"value= "+" /> <!--maximum number of connections per partition - <!--Basis of judgment: number of concurrent requests - < Propertyname= "Maxconnectionsperpartition"value= "+" /> <!--minimum number of connections per partition - < Propertyname= "Minconnectionsperpartition"value= "5" /> </Bean>
The reference XML configuration is transformed into a Java configuration method:
@Value ("${jdbc.url}") PrivateString Jdbcurl; @Value ("${jdbc.driverclassname}") PrivateString Jdbcdriverclassname; @Value ("${jdbc.username}") PrivateString Jdbcusername; @Value ("${jdbc.password}") PrivateString Jdbcpassword; @Bean (Destroymethod= "Close") PublicDataSource DataSource () {Bonecpdatasource Bonecpdatasource=NewBonecpdatasource (); //Database-drivenBonecpdatasource.setdriverclass (jdbcdriverclassname); //Jdbcurl of the corresponding driveBonecpdatasource.setjdbcurl (Jdbcurl); //user name of the databaseBonecpdatasource.setusername (jdbcusername); //password for the databaseBonecpdatasource.setpassword (jdbcusername); //Check the time interval for idle connections in the database connection pool, in units of minutes, default: 240, set to 0 if you want to cancelBonecpdatasource.setidleconnectiontestperiodinminutes (60); //maximum surviving time for unused links in the connection pool, in units of minutes, default: 60, if you want to live forever set to 0Bonecpdatasource.setidlemaxageinminutes (30); //maximum number of connections per partitionBonecpdatasource.setmaxconnectionsperpartition (100); //minimum number of connections per partitionBonecpdatasource.setminconnectionsperpartition (5); returnBonecpdatasource;}
Think: How do I use the DataSource object?
In spring, use a method similar to how you normally use it.
Java configuration read external resource configuration file