Spring uses PropertyPlaceholderConfigurer placeholders

Source: Internet
Author: User

1. in the Spring framework, org. springframework. beans. factory. config. propertyplaceholderpolicer class can be set. some dynamically set values in the properties (key/value form) file are replaced with values occupying the key ($ key $) in XML ,. the properties file can be customized based on customer needs. Such a design can provide program flexibility. 2. in Spring, PropertyPlaceholderConfigurer can be used to add an external attribute file to the XML configuration file. Of course, you can also specify the encoding of the external file, for example, <bean id = "propertyConfigurer" class = "org. springframework. beans. factory. config. propertyPlaceholderConfigurer "> <property name =" location "> <value> conf/sqlmap/jdbc. properties </value> </property> <property name = "fileEncoding"> <value> UTF-8 </value> </property> </bean> Of course, you can also introduce multiple attributes file, for example, <bean id = "propertyConfigurer" class = "org. s Pringframework. beans. factory. config. propertyPlaceholderConfigurer "> <property name =" locations "> <list> <value>/WEB-INF/mail. properties </value> <value> classpath: conf/sqlmap/jdbc. properties </value> // note the writing of these two values </list> </property> </bean>: xml Code <bean id = "propertypolicerforanalysis" class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locatio N "> <value> classpath:/spring/include/dbQuery. properties </value> </property> <property name = "fileEncoding"> <value> UTF-8 </value> </property> </bean> where classpath references. When multiple Properties files exist, you need to use locations for configuration: Xml Code <bean id = "propertyConfigurer" class = "org. springframework. beans. factory. config. propertyPlaceholderConfigurer "> <property name =" locations "> <list> <value> classpath:/spring/include/jdbc-parms.properties </value> <value> classpath: /spring/include/base-config.properties </value> <value> classpath *: config/jdbc. properties </value> </list> </property> </bean> PropertyPlaceholderConfigurer to distribute configurations, to integrate multiple scattered Properties files under multiple projects. The configuration is as follows: Xml Code <bean id = "propertypolicerforproject1" class = "org. springframework. beans. factory. config. propertyplaceholderholder "> <property name =" order "value =" 1 "/> <property name =" ignoreUnresolvablePlaceholders "value =" true "/> <property name =" location "> <value> classpath: /spring/include/dbQuery. properties </value> </property> </bean> Xml Code <bean id = "propertypolicerforproject2" class = "org. springframework. beans. factory. config. propertyplaceholderholder "> <property name =" order "value =" 2 "/> <property name =" ignoreUnresolvablePlaceholders "value =" true "/> <property name =" locations "> <list> <value> classpath: /spring/include/jdbc-parms.properties </value> <value> classpath:/spring/include/base-config.properties </value> </list> </ Property> </bean> where the order attribute represents the loading order, while ignoreUnresolvablePlaceholders indicates whether to ignore unresolvable Placeholder. If multiple propertyplaceholderregistrers are configured, set this parameter to true. 3. for example, jdbc. properties: jdbc. driverClassName = com. mysql. jdbc. driverjdbc. url = jdbc: mysql: // localhost/mysqldb? UseUnicode = true & amp; characterEncoding = UTF-8 & amp; zeroDateTimeBehavior = round; jdbc. username = rootjdbc. password = 123456 4. in the spring configuration file, we can write: <bean id = "propertyConfigurer" class = "org. springframework. beans. factory. config. propertyPlaceholderConfigurer "> <property name =" locations "> <list> <value> classpath: conf/sqlmap/jdbc. properties </value> </list> </property> </bean> <bean id = "dataSource" class = "org. a Pache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc. driverClassName} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean> 5. in this way, a simple data source is set up. It can be seen that the role of PropertyPlaceholderConfigurer is to put the configuration information of the database to which the placeholder points to the tool defined in bean. Java code <! -- DataSource --> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" $ {jdbc. driverClassName} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean> <! -- SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" mappingResources "> <list> <value> cn/xg/hibernate/spring/User. hbm. xml </value> <! -- The ing path problem here. This method can only be added one by one --> <value> cn/xg/hibernate/spring/Group. hbm. xml </value> </list> <! -- Load *. hbm. xml file method: <property name = "mappingDirectoryLocations"> <list> <value> classpath: /cn/xg/spring/model </value> </list> </property> --> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect ">$ {hibernate. dialect} </prop> <prop key = "hibernate. show_ SQL "> true </prop> </props> </property> </bean> <! -- DAO implementation class extends HibernateDaoSupport, injecting sessionFactory --> <bean id = "userMgrImpl" class = "cn. xg. hibernate. spring. userMgrImpl "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <bean id =" groupMgrImpl "class =" cn. xg. hibernate. spring. groupMgrImpl "> <property name =" sessionFactory "ref =" sessionFactory "/> <property name =" userImpl "ref =" userMgrImpl "/> <property name =" transactionTemplate "ref = "TransactionTemplate"/> </bean> <! -- Transaction Management --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <! -- Programming-style transaction Writing Method: inject transactionTemplate into Dao implementation class, mobilize its execute () method, interface callback new TransactionCallback () --> <bean id = "transactionTemplate" class = "org. springframework. transaction. support. transactionTemplate "> <property name =" transactionManager "ref =" transactionManager "/> </bean> <! -- The first way to write a sound-time transaction --> <! -- <Bean id = "groupMgr" class = "org. springframework. transaction. interceptor. transactionProxyFactoryBean "> <property name =" transactionManager "ref =" transactionManager "/> <property name =" target "ref =" groupMgrImpl "/> <property name =" transactionAttributes "> <props> <prop key = "add *"> PROPAGATION_REQUIRED </prop> <prop key = "get *"> PROPAGATION_REQUIRED </prop> <prop key = "*"> readOnly </prop> </props> </property> </Bean> --> <! -- Second Writing Method of acoustic-time transaction --> <! -- Propagation feature of transactions <tx: advice id = "txAdvice"> <tx: attributes> <tx: method name = "add *" propagation = "REQUIRED"/> <tx: method name = "get *" propagation = "REQUIRED"/> <tx: method name = "*" read-only = "true"/> </tx: attributes> </tx: advice> <aop: config> <aop: advisor pointcut = "execution (* cn. xg. hibernate. spring. *. *(..)) "advice-ref =" txAdvice "/> </aop: config> --> </beans> jdbc. properties jdbc. driverClassName = com. mysql. jdbc. driver jdbc. url = jdbc: mysql: // localhost: 3306/Database Name jdbc. username = database username jdbc. password = Database password hibernate. dialect = org. hibernate. dialect. mySQLDialect (dialect. mySql)

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.