springmvc架构和springboot架构通用的引入配置方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sun5769675/article/details/81482963

当我们在写一些插件或者通用功能时,可能会把他们打成jar包,如果是给web项目使用时,可能会出一个部署文档,比如要配置一些什么配置用来支持我们这个jar包里面的功能,这里建议这些可配置的参数在我们jar包中的引入方式使用@Value("${wechat.appid}")这个对于不同架构方式下都可以很容易的配置出来,下面举了三个常见的例子:


1. 使用了spring的项目需要配置xml的项目在注入参数时使用下面这种

java代码

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

xml配置

<beans xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <util:properties id="configProperties" location="classpath:config.properties"></util:properties>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties"/>
    </bean>
    
</beans>    

config.properties配置

wechat.appid=xxxxxxxx


2. 使用了springboot的项目使用application.properties作为配置时

java代码

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

application.properties配置

wechat.appid=xxxxxxxx


3. 使用了springboot的项目使用application.yml作为配置时

java代码

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

application.yml配置(json格式,参数值和冒号之间一定要有一个空格,否则解析失败)

wechat: 
    appid: xxxxxx

猜你喜欢

转载自blog.csdn.net/sun5769675/article/details/81482963