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

  • 相关阅读:
    LeetCode 230. 二叉搜索树中第K小的元素
    LeetCode 669. 修剪二叉搜索树
    LeetCode 94. 二叉树的中序遍历
    LeetCode 145. 二叉树的后序遍历
    LeetCode 144. 二叉树的前序遍历
    Not registered via @EnableConfigurationProperties or marked as Spring component
    maven依赖的报错Unable to import maven project: See logs for details
    GDIPlus的使用准备工作
    全局变量替代方案:控制反转,依赖注入
    MFC使用TRACKMOUSEEVENT触发mouseHover和mouseLeave
  • 原文地址:https://www.cnblogs.com/wujf/p/5287903.html
Copyright © 2011-2022 走看看