Introduction to propertyplaceholderconfigurer in spring, propertyconfigurer

Source: Internet
Author: User

Introduction to propertyplaceholderconfigurer in spring, propertyconfigurer

The Spring framework provides you with a BeanFactoryPostProcessor implementation class: org. springframework. beans. factory. config. PropertyPlaceholderConfigurer. With this category, you can remove some configuration settings. in the properties file, such an arrangement allows the XML definition file to be responsible for system settings. the properties file can be used as a custom parameter as needed.
Let's take a look at the actual example of a Bean definition file:
Beans-config.xml

Xml code:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>hello.properties</value>
</property>
</bean> <bean id="helloBean" class="onlyfun.caterpillar.HelloBean"> <property name="helloWord"> <value>${onlyfun.caterpillar.helloWord}</value> </property> </bean></beans>

Assume that there are many dependency injection attributes in helloBean, which are rarely changed, and helloWord will change frequently. properties, and the information has been set in the location attribute of configBean:
Hello. properties

onlyfun.caterpillar.helloWord=Welcome!

In the helloWord attribute of helloBean, $ {onlyfun. caterpillar. helloWord} will be replaced by helloWord of hello. properties.
If there are multiple. properties files, you can set them through the locations attribute, for example:
Beans-config.xml

Xml code:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations">         <list>            <value>hello.properties</value>             <value>welcome.properties</value>             <value>other.properties</value>         </list>    </property>    </bean>     <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">         <property name="helloWord">             <value>${onlyfun.caterpillar.helloWord}</value>         </property>           ...          </bean> </beans>

Propertyplaceholderpolicer class is a type of bean factory post-processor. It serves as a resource attribute configurator, which can put the content defined in BeanFactory in an object. file with the suffix propertis.
Example --- spring-context.xml ----

Xml code:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     <property name="locations">         <list>            <value>/WEB-INF/jdbc.properties</value>        </list>     </property> </bean>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     <property name="driverClassName"><value>${driver}</value></property>     <property name="url"><value>jdbc:${dbname}</value></property> </bean>

 

The actual jdbc. propertis file is

jdbc.driverClassName=org.hsqldb.jdbcDriver jdbc.url=jdbc:hsqldb:hsql://production:9002 jdbc.username=sa jdbc.password=root


How does jdbc. propertis reference it? register the preceding configuration in web. xml.

Xml code:

<Context-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/spring-context.xml </param-value> </context-param> of course, do not forget to register the <listener> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener>

In this way, a simple data source is set up.
In fact, PropertyPlaceholderConfigurer is used to place the configuration information of the database to which the placeholder points in bean for definition.
.


What is the following code in Spring?

Org. jasypt. encryption. pbe. StandardPBEStringEncryptor
Org. jasypt. spring. properties. EncryptablePropertyPlaceholderConfigurer
This probably means configuring two beans.
You can use their id configurationEncryptor and propertyConfigurer to call them without creating new ones.
StandardPBEStringEncryptor has two member variables algorithm. The default password value is PBEWithMD5AndDES and snsbh32011.
EncryptablePropertyPlaceholderConfigurer has a member variable location default classpath: jdbc. properties
There is also a constructor
EncryptablePropertyPlaceholderConfigurer (StandardPBEStringEncryptor se) // constructor-arg ref

How can I read the content in the Code through the properties file configured by Spring?

Public void getCsisUrl (){
Properties p = new Properties ();
Try {
FileInputStream in = new FileInputStream (ServletActionContext. getRequest (). getRealPath ("/WEB-INF/classes/demo. properties "));
P. load (in );
In. close ();
String csisUrl = p. getProperty ("csisUrl ");
// System. out. println (csisUrl );
} Catch (Exception e ){
E. printStackTrace ();
}
}

Related Article

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.