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}