Spring in Action Learning 11: Property placeholder XML way to avoid injecting external properties hard code

Source: Internet
Author: User

Here is a language specific to placeholder or the expression: the ${},spring in action is described as follows:

In spring wiring, placeholder values is property names wrapped with ${...},as an exampl,you can resolve the constructor a Rguments for a blankdisc in XML like this:

<bean id= "Sgtpeppers"

Class= "Soundsystem. Blankdisc "

C:_titile= "${disc.title}"

C:_artist= "${disc.artist}"/>

The following case uses ${...} Read the content from the configuration file,

Be sure to write the following statement on the configuration file:

1   <!--load the. Properties file--2     <context:property-placeholder location= "classpath:com/ Advancedwiring/ambiguityiniautowiring2/student.properties "/>

The directory structure of the case is as follows:

The code for the case is as follows:

The code for the Student class is as follows:

1  Packagecom.advancedWiring.ambiguityIniAutowiring2;2 3 ImportOrg.springframework.beans.factory.BeanNameAware;4 Importorg.springframework.beans.factory.config.ConfigurableBeanFactory;5 ImportOrg.springframework.context.annotation.Scope;6 ImportOrg.springframework.context.annotation.ScopedProxyMode;7 Importorg.springframework.stereotype.Component;8 ImportOrg.springframework.web.context.WebApplicationContext;9 Ten /** One * Created by ${Quinlinson} on 2017/6/9. A  */ -  Public classStudentImplementsbeannameaware{ -     PrivateString name; the     PrivateInteger age; -  -      PublicString GetName () { -         returnname; +     } -  +      Public voidsetName (String name) { A          This. Name =name; at     } -  -      PublicInteger getage () { -         returnAge ; -     } -  in      Public voidsetage (Integer age) { -          This. Age =Age ; to     } +  - @Override the      Public voidsetbeanname (String name) { *System.out.println ("The Student Bean name is:" +name); $     }Panax Notoginseng  -     /** the * Write this constructor mainly to inject external property value. +      * @paramname the student ' s name A      * @paramAge of the student ' s age the      */ +      PublicStudent (String name, Integer age) { -          This. Name =name; $          This. Age =Age ; $     } -}

The code for the Student.properties file is as follows:

1 #用配置文件的形式, avoid hard-coding the injected property values.  2 name=abrahamlincoln3 age=21

The code for Ambiguity.xml is as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5Xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"6xmlns:context= "Http://www.springframework.org/schema/context"7Xsi:schemalocation= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8http//www.springframework.org/schema/aop/spring-aop.xsd9http//Www.springframework.org/schema/contextTenhttp//www.springframework.org/schema/context/spring-context.xsd Onehttp//Www.springframework.org/schema/beans Ahttp//www.springframework.org/schema/beans/spring-beans.xsd "> -<context:annotation-config/> -<!--load the. Properties File-- the<context:property-placeholder location= "classpath:com/advancedwiring/ambiguityiniautowiring2/ Student.properties "/> -<bean id= "Student" -           class= "Com.advancedWiring.ambiguityIniAutowiring2.Student" -C:name= "${name}" +C:age= "${age}" > -</bean> +</beans>

The code for testing the class test is as follows:

1  Packagecom.advancedWiring.ambiguityIniAutowiring2;2 3 ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7 * Created by ${Quinlinson} on 2017/6/9.8  */9  Public classTest {Ten      Public Static voidMain (string[] args) { One  AClasspathxmlapplicationcontext AC =NewClasspathxmlapplicationcontext ("Com/advancedwiring/ambiguityiniautowiring2/ambiguity.xml"); -Student Student = Ac.getbean (Student.class); -         /** the * The output is: The student name Is:abrahamlincoln and age is:19 - * You can see that he took out the value of the configuration file.  -          */ -System.out.println ("The Student name is:" +student.getname () + "and": "+student.getage ()); +     } -}

Spring in Action Learning 11: Property placeholder XML way to avoid injecting external properties hard code

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.