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

  • 相关阅读:
    Git 基础
    SharePoint 2013 对象模型操作"网站设置"菜单
    SharePoint 2013 隐藏部分Ribbon菜单
    SharePoint 2013 Designer系列之数据视图筛选
    SharePoint 2013 Designer系列之数据视图
    SharePoint 2013 Designer系列之自定义列表表单
    SharePoint 2013 设置自定义布局页
    SharePoint 2013 "通知我"功能简介
    SharePoint 2013 创建web应用程序报错"This page can’t be displayed"
    SharePoint 禁用本地回环的两个方法
  • 原文地址:https://www.cnblogs.com/wujf/p/5287903.html
Copyright © 2011-2022 走看看