关于属性注入

来源:互联网 发布:软件期刊 编辑:程序博客网 时间:2024/06/01 07:55
Spring 利用PropertyPlaceholderConfigurer占位符1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:复制代码<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>当然也可以引入多个属性文件,如:复制代码<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  <property name="locations">   <list>    <value>/WEB-INF/mail.properties</value>      <value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法   </list>  </property></bean>复制代码3.譬如,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.那么在spring配置文件中,我们就可以这样写:复制代码<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.apache.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.这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。6.查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

使用注解方式完成属性的注入:

<!-- 属性文件加载  使用${}作为取值的方式-->    <bean id="webPropertyConfig"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:conf/main-setting.properties</value>            </list>        </property>        <property name="placeholderPrefix" value="${" />        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />        <property name="ignoreUnresolvablePlaceholders" value="true" />    </bean>使用:main-setting.properties#图片服务器地址uimgServer=${uimgServer}使用的Java@Repository("retailerSkuCmmdtyDaoImpl")public class RetailerSkuCmmdtyDaoImpl {/**     * 图片服务器访问地址     */    @Value("${uimgServer}")    private String uimgServer;}

maven资源配置属性注入的方式
主要pom

<profiles>        <profile>            <id>dev</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <build>                <filters>                    <filter>../${project.parent.artifactId}/vars/vars.dev.properties</filter>                </filters>            </build>        </profile>        <profile>            <id>sit</id>            <build>                <filters>                    <filter>../${project.parent.artifactId}/vars/vars.sit.properties</filter>                </filters>            </build>        </profile>        <profile>            <id>pre</id>            <build>                <filters>                    <filter>../${project.parent.artifactId}/vars/vars.pre.properties</filter>                </filters>            </build>        </profile>        <profile>            <id>prod</id>            <build>                <filters>                    <filter>../${project.parent.artifactId}/vars/vars.prod.properties</filter>                </filters>            </build>        </profile>    </profiles>    <build>        <pluginManagement>            <plugins>                <!-- war plugin config: 对静态资源进行过滤,替换变量。 如果你不需要,请移除这一段 -->                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-war-plugin</artifactId>                    <configuration>                        <warName>${project.artifactId}</warName>                        <webResources>                            <resource>                                <directory>src/main/webapp</directory>                                <filtering>true</filtering>                                <includes><!--可以用来替换的资源-->                                    <include>**/*.html</include>                                    <include>**/*.js</include>                                    <include>**/*.css</include>                                    <include>**/*.xml</include>                                </includes>                            </resource>                        </webResources>                    </configuration>                </plugin>            </plugins>        </pluginManagement>        <!-- 该配置可以让Maven属性在资源文件(src/main/resources、src/test/resources)中被解析 -->        <resources>            <resource>                <directory>${basedir}/src/main/resources</directory>                <filtering>true</filtering>            </resource>        </resources>        <testResources>            <testResource>                <directory>${basedir}/src/test/resources</directory>            </testResource>        </testResources>    </build>引用方式:${resRoot};<link rel="stylesheet" href="${resRoot}/css/lib/print-bill.css">

xml中使用properties文件中的属性

 <property name="freemarkerVariables">            <map>                <entry key="base" value="@{base}"/>                <entry key="xml_escape" value-ref="fmXmlEscape"/>                <entry key="html_escape" value-ref="fmHtmlEscape"/>            </map>        </property>        <property name="defaultEncoding" value="utf-8"/>    </bean>

ftl文件中引用变量的方式

main-setting-web.proertiesbase=resRoot=${resRoot}appVersion=$[project.version]rcaLoginUrl=${rca.login.url}loginDesKey=${login.password.des.key}xml配置<bean id="viewResolver" abstract="true">        <property name="attributes">            <props>                <prop key="base">@{base}</prop>                <prop key="resRoot">@{resRoot}</prop>                <prop key="rcaLoginUrl">@{rcaLoginUrl}</prop>                <prop key="loginDesKey">@{loginDesKey}</prop>            </props>        </property>        <property name="prefix" value="/WEB-INF/freemarker/"/>        <property name="suffix" value=".ftl"/>    </bean>freemarker文件配置<!-- FreeMarker配置文件 -->    <bean id="freemarkerConfig"        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">        <description>Required for Freemarker to work in web tier</description>        <property name="configuration" ref="freemarkerConfiguration" />    </bean>    <!-- FreeMarker配置文件 -->    <bean id="freemarkerConfiguration"        class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">        <description>Using the Config directly so we can use it outside the            web tier        </description>        <!-- 模板加载路径 -->        <property name="templateLoaderPaths">            <list>                <value>/WEB-INF/freemarker/</value>                <value>/</value>                <!-- <value>/WEB-INF/ftl_macro</value> -->            </list>        </property>        <property name="configLocation">            <value>classpath:conf/freemarker.properties</value>        </property>        <!--全局变量部分 -->        <property name="freemarkerVariables">            <map>                <entry key="xml_escape" value-ref="fmXmlEscape" />                <entry key="html_escape" value-ref="fmHtmlEscape" />                <entry key="base" value="@{base}" />                <entry key="simpUrl" value="@{simpUrl}" />                <entry key="envName" value="@{envName}" />                <entry key="resRoot" value="@{resRoot}" />                <entry key="passport" value="$[admin.sso.auth.server.url]"/>                <entry key="adressdomain" value="@{adressdomain}"/>                <entry key="uimgUrl" value="@{uimgUrl}"/>            </map>        </property>        <property name="defaultEncoding" value="utf-8" />    </bean>

freemarker 引用宏

引入默认的宏文件auto_import="/common/index.ftl"

针对maven的统一的资源配置 在
在静态文件(html,css,js)
使用${}进行引用

但是针对其他文件 一版使用 $[]进行引用操作…

原创粉丝点击