Properties in Spring

Source: Internet
Author: User

Properties Injection

    • Through XML configuration
    • by @PropertySource Configuration
    • Propertyplaceholderconfigurer
    • Propertysourcesplaceholderconfigurer

Use of Properties

    • Using in an XML configuration file
    • Using @Value Injection
    • Get through environment
    • Get through Application.properties

Spring Boot Related

    • @ConfigurationProperties
    • Configuration priority
The Properties configuration is configured through XML

<context:property-placeholder location ="classpath:sys.properties" />

by @PropertySource Configuration
@PropertySource ("Classpath:sys.properties") @Configurationpublic class Democonfig {}

@PropertySource must be used in conjunction with @Configuration here.

Propertyplaceholderconfigurer
<bean class= "Org.springframework. beans.factory . config. Propertyplaceholderconfigurer ">    <property name=" Locations ">        <list>            <value> classpath:sys.properties</value>        </list>    </property>    <property name= " Ignoreunresolvableplaceholders "value=" true "/>      <!--Here you can configure some properties--></bean>

Java Configuration version

@Beanpublic propertyplaceholderconfigurer propertiess () {    Propertyplaceholderconfigurer PPC = new Propertyplaceholderconfigurer ();    resource[] Resources = new Classpathresource[]{new Classpathresource ("Sys.properties")};    Ppc.setlocations (resources);    Ppc.setignoreunresolvableplaceholders (true);    return PPC;}
Propertysourcesplaceholderconfigurer
<bean class= "Org.springframework. Context.support. Propertysourcesplaceholderconfigurer ">    <property name=" Locations ">        <list>            <value >classpath:sys.properties</value>        </list>    </property>    <property name= " Ignoreunresolvableplaceholders "value=" true "/>    <!--Here you can configure some properties--></bean>

Java Configuration version

@Beanpublic Propertysourcesplaceholderconfigurer Properties () {    Propertysourcesplaceholderconfigurer pspc = new Propertysourcesplaceholderconfigurer ();    resource[] Resources = new Classpathresource[]{new Classpathresource ("Sys.properties")};    Pspc.setlocations (resources);    Pspc.setignoreunresolvableplaceholders (true);    return PSPC;}
Use of Properties in an XML configuration file
<bean id= "xxx" class= "com.demo.Xxx" >      <property name= "url" value= "${mysql.jdbc.url}"/></bean>
Using @Value Injection
@Value ("${demo.jdbc.url}") Private String URL;
Get through environment

It can be used only when using annotations @PropertySource, otherwise it will be null.

@Autowiredprivate Environment Env;public String getUrl () {    return Env.getproperty ("Demo.jdbc.url");}
Get through Application.properties

Demo.database.url=jdbc:mysql:

Spring Boot Related @configurationproperties
Application.properties
Demo.db.url=jdbc:mysql:demo.db.username=testdemo.db.password=123456@configuration@configurationproperties ( prefix = "demo.db") @Datapublic class DataBase { String URL; String username; String password;}
Configuration priority

Java -dspring.profiles.active=env -jar App.jar

If there are two files,application.properties and application-env.properties , then the configuration in the two files will be registered, if there are duplicate keys, Higher priority in the Application-env.properties file.

Summary: startup parameters > application-{env}.properties > application.properties

  

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.