zoukankan      html  css  js  c++  java
  • 学习之spring属性文件注入

    package com.my.proper;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("configInfo")
    public class ConfigInfo {
        @Value("${pagesize}")
        private String pagesize;
    
        @Value("${aaa}")
        private String aaa;
    
        public String getAaa() {
            return aaa;
        }
    
        public void setAaa(String aaa) {
            this.aaa = aaa;
        }
    
        public String getPagesize() {
            return pagesize;
        }
    
        public void setPagesize(String pagesize) {
            this.pagesize = pagesize;
        }
    }
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
        <context:component-scan base-package="com.my.proper" />
        <context:component-scan base-package="com.my.action" />
    
        <!-- spring的属性加载器,加载properties文件中的属性 -->
        <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:messages.properties</value>
                    <value>classpath:messages2.properties</value>
                </list>
            </property>
            <property name="fileEncoding" value="utf-8" />
        </bean>
    </beans>
        public void propertyTest() {
            ConfigInfo config = (ConfigInfo) cx.getBean("configInfo");
            System.out.println(config.getPagesize());
            System.out.println(config.getAaa());
        }

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    Jmeter接口测试01
    JSON数据
    HTTP协议概念
    接口测试-概念
    appium 元素定位
    从一个猜单词的小程序开始---征服OOP的思维方式01
    WINDOWS程序设计(003)----窗口类的注册
    WINDOWS程序设计(002)----HELLOWIN程序(源代码及详细解析) WINDOWS程序原理
    Windows下Apache Tomcat?的下载安装和使用
    Windows10下配置虚拟机Virtual Box安装CentOS(Linux)详细教程
  • 原文地址:https://www.cnblogs.com/wujf/p/5287903.html
Copyright © 2011-2022 走看看