Two ways to use property file properties in spring

Source: Internet
Author: User

In real-world projects, configurable parameters are typically placed in a property file, such as database connection information, Redis connection information, and so on for unified management. It is then loaded into context through the IOC framework spring, making the program available directly.

Create the Mysql.properties file and put it in the classpath path, and if you use MAVEN build project, place it directly under the Resources folder. File contents:

Mysql.url=jdbc:mysql://192.168.1.101:3306/demomysql.username=root
mysql.password=123456

When the spring container starts, the property file information is loaded using the built-in bean, which is added in Applicationcontext.xml:

<!--Spring's property loader, loading properties from property file -<BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    < Propertyname= "Location">        <value>mysql. properties</value>    </ Property>    < Propertyname= "fileencoding"value= "UTF-8" /></Bean>

One of the ways in which property information is loaded is to refer to value directly from the key of the attribute information in other bean definitions, such as the configuration of the Mail sender bean:

<!--Configure the data source -<BeanID= "DataSource"class= "Org.apache.commons.dbcp2.BasicDataSource">    < Propertyname= "Driverclassname"value= "Com.mysql.cj.jdbc.Driver" />    < Propertyname= "url"value= "${mysql.url}" />    < Propertyname= "username"value= "${mysql.username}" />    < Propertyname= "Password"value= "${mysql.password}" /></Bean>

Another way to use this is to get the configured property information in your code, define a Java Bean:RedisConfig.java, and use annotations to inject the attribute information you need to use in your code. The following information is available in the code, as in the properties file:

redis.host=192.168.1.101redis.port=6379

Redisconfig.java the corresponding code:

1  PackageOrg.springinaction.weather.config;2 3 ImportOrg.springframework.beans.factory.annotation.Value;4 Importorg.springframework.stereotype.Component;5 6@Component ("Redisconfig")7  Public classRedisconfig {8 9@Value ("${redis.host}")Ten     PrivateString host; One  A@Value ("${redis.port}") -     PrivateString Port; -  the      PublicString gethost () { -         returnhost; -     } -  +      PublicString Getport () { -         returnPort; +     } A}

Get the corresponding property information through the Get method:

1 ImportOrg.springframework.context.ApplicationContext;2 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;3 ImportOrg.springinaction.weather.config.RedisConfig;4 5  Public classRedistest {6 7      Public Static voidMain (string[] args) {8ApplicationContext context =NewClasspathxmlapplicationcontext ("Spring-redis.xml");9Redisconfig redisconfig = Context.getbean (redisconfig.class);Ten System.out.println (Redisconfig.gethost ()); One System.out.println (Redisconfig.getport ()); A     } -  -}

Obtain the corresponding property information through the Get method, the advantage is that the code is easy to use, the disadvantage is that if you need new attribute information in the code, the Redisconfigjava should be added to the corresponding changes.

Two ways to use property file properties in spring

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.