The spring configuration file is flexible. For example, when you need to configure JDBC or other information related to the host, you can further abstract it.
Generate the following property file (/WEB-INF/jdbc. properties ):
JDBC. Driver = org. PostgreSQL. driverjdbc. url = JDBC: PostgreSQL: // localhost/testjdbc. User = ssjdbc. Password =
The bean configuration is as follows:
<Bean id = "propertyconfigurer" class = "org. springframework. beans. factory. config. propertyplaceholderconfigurer "> <property name =" location "> <value>/WEB-INF/jdbc. properties </value> </property> </bean> <bean id = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource "> <property name =" driverclassname "> <value >$ {JDBC. driver} </value> </property> <property name = "url"> <value >$ {JDBC. URL} </value> </property> <property name = "username"> <value >$ {JDBC. user} </value> </property> <property name = "password"> <value >$ {JDBC. password} </value> </property> </bean>
As mentioned above, we have defined an instance of the propertyplaceholderconfigurer class and set its location attribute to our attribute file. This class is implemented as the post-processor of the Bean Factory, and all placeholders ($ {...} value) are replaced by attributes defined in the file ).
With this technology, we can remove all host-specific configuration attributes from applicationcontext. xml. In this way, we can add new beans to the file freely without worrying about the synchronization of host attributes. This simplifies production deployment and maintenance.