Spring basics -- use the external attribute file in Spring Config, springconfig

Source: Internet
Author: User

Spring basics -- use the external attribute file in Spring Config, springconfig

1. When configuring beans in the Spring Config file, you sometimes need to add detailed information about system deployment in Bean configuration, such as file path and data source configuration information. These deployment details need to be defined outside the configuration file.

2. Spring provides a BeanFactory postprocessor for PropertyPlaceholderConfigurer. This processor allows you to move the Bean configuration content out of the property file, and then you can go to the Bean configuration file

Variables in the form of $ {var} are used. PropertyPlaceholderConfigurer loads attributes from the attribute file and uses these attributes to replace the variables.

3. Spring also allows $ {key} to be used in attribute files for mutual reference between attributes.

4. Use: Register PropertyPlaceholderConfigurer. Use <context: property-placeholder location = "props. properties"/> to specify the property file.

V. Example:

1. directory structure

2. spring-config.xml

<context:property-placeholder location="db.properties"/><bean class="com.nucsoft.spring.bean.Employee" id="employee" p:empName="${abc}" p:age="${cde}" scope="prototype"/>

3. db. properties

abc=123456cde=${abc}

4. Test

@Testpublic void test01() {  Employee employee = ctx.getBean(Employee.class);  System.out.println(employee);}

5. Console output

Employee {empName = '000000', age = 123456}

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.