Java configuration read external resource configuration file

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.