Spring 中 ConfigBean 注入配置文件属性

本贴最后更新于 1911 天前,其中的信息可能已经渤澥桑田

Spring 中 ConfigBean 注入配置文件属性

背景

主要涉及依赖 版本
spring-context 4.2.7.RELEASE
spring-core 4.2.7.RELEASE
spring-test 4.2.7.RELEASE
junit 4.11
commons-lang 2.5

配置文件

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
">
	<!-- 自动扫描(自动注入) -->
	<context:component-scan base-package="com"></context:component-scan>
</beans>

config.properties

#微信公众号的appid
wechat.pay.appId=123
#微信支付商户号
wechat.pay.mchId=456
#微信支付商户密钥
wechat.pay.mchKey=789
#apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
wechat.pay.keyPath=/root/apiclient_cert.p12

实体类

package com.spring.demo.anotation;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration//把要实例化的对象转化成一个Bean,放在IoC容器中,使用的时候可以用·@Autowired·等注解注入
//@ComponentScan("com.spring.demo")//定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中
@PropertySource(value = "classpath:config.properties")
public class WxConfig {

	//使用PropertySourcesPlaceholderConfigurer,它能够基于Spring Environment及其属性源来解析占位符。如下的@Bean方法在Java中配置了PropertySourcesPlaceholderConfigurer;否则解析会被解析成${wechat.pay.appId} 等。
   @Bean//产生一个bean的方法,并且交给Spring容器管理,生这个Bean对象的方法Spring只会调用一次,默认bean的名称就是其方法名。但是也可以指定名称:
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Value("${wechat.pay.appId}")
    private String appId;

    @Value("${wechat.pay.mchId}")
    private String mchId;

    @Value("${wechat.pay.mchKey}")
    private String mchKey;

    @Value("${wechat.pay.keyPath}")
    private String keyPath;

    //省略了get set方法

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this,
                ToStringStyle.MULTI_LINE_STYLE);
    }

}

Junit 测试类

package com.anotation;

import com.BasicTest;
import com.spring.demo.anotation.WxConfig;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class TestAnotation extends BasicTest {

    @Autowired
    private WxConfig wxConfig;

    @Test
    public void testConfig() {
        System.out.println(wxConfig);
    }
}

执行结果

...
信息: Refreshing org.springframework.context.support.GenericApplicationContext@6442b0a6: startup date [Thu Sep 13 11:47:53 CST 2018]; root of context hierarchy
com.spring.demo.anotation.WxConfig$$EnhancerBySpringCGLIB$$77416101@58359ebd[
  appId=123
  mchId=456
  mchKey=789
  keyPath=/root/apiclient_cert.p12
]
九月 13, 2018 11:47:53 上午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@6442b0a6: startup date [Thu Sep 13 11:47:53 CST 2018]; root of context hierarchy

Process finished with exit code 0

成功啦!!!😄

本 demo 地址:springdemo

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    940 引用 • 1458 回帖 • 158 关注
  • JUnit
    18 引用 • 11 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...