spring 설정 에서 context: property - place holder 의 사용 설명

8245 단어 spring
1,             。

              url,username,password   
      client  server  ,   
2,                  

  :                             ,                       
  :spring3              <content:property-placeholder>   
    spring         :

<context:property-placeholder location="classpath:jdbc.properties"/>
1
  

<bean id="propertyPlaceholderConfigurer" class="org.springframework,beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>jdbc.properties<value/>
        list>
    property>
bean>


  ,   location           ,        src   ,                ,

#jdbc  
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

  #        

        spring   bean       ,  spring        

<bean id="dataSource" class="org.springframework,jdbc,datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
bean>

      ${}          spring       ,         (     )

Spring             ,    Spring       org.springframework.beans.config.PropertyPlaceholderConfigurer Bean       PropertyPlaceholderConfigurer   ,

    , Spring           PropertyPlaceholderConfigurer   <content:property-placeholder>     Spring  

  Spring       PropertyPlaceholderConfigurer,         ,         ,     ,    。

      , Spring      properties      :     properties      ,   Spring        org.springframework.beans.factory.config.PropertiesFactoryBean org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer     

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value> classpath*:application.propertiesvalue>
        list>
    property>
bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="properties" ref="configProperties" />
bean>

                 , configProperties   propertyConfigurer  ,        java         

@Component
public class Test{
    @Value("#{configProperties['userName']}")
    private String userName;

    public String getUserName(){
        return userName;
    }

}

         @Value     ,        #{configProperties['userName']}  configProperties            bean  id, userName            

util:properties context:property-placeholder                    
util:properties 
     bean     ,     bean,         SpEL   #{}  bean   。
1.    

2.
<util:properties id="config" location="classpath:db.properties" />

<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{config.driver}" />
<property name="url" value="#{config.url}" />
<property name="username" value="#{config.username}" />
<property name="password" value="#{config.password}" />
bean>

원문
https://blog.csdn.net/liuxin191863128/article/details/53406747

좋은 웹페이지 즐겨찾기