是这样的项目用的是Struts2 和Spring。我想在action层中测试下配置文件读取是否正常,Spring 版本有点低是3.1如下的,无法用@Value注解 读取配置文件。java
还不让经过文件读取的形式直接读配置文件,那我只能经过xml 配置Bean 的形式注入了json
1、首先在Spring 配置bean的xml中,将action 当成一个bean配置好安全
(有一个须要注意的地方就是Spring bean 默认是单例的,而Struts2 的 action不是,因此须要修改 scope。)测试
<bean id="TestAction" class="com.zjl.TestAction" scope = "prototype"> <property name="username" value="${username}"/> <property name="estoken" value="${estoken}"/> </bean>
2、在struts2 的配置文件中也有变化 :下面action标签 的class属性中要把原来的全类路径,改成Spring bean name。加密
(若是仍是全类路径,那这个action还规struts2 管理)spa
<package name="testProperties" namespace="/json/properties" extends="json-protected-default"> <action name="*" class="TestAction" method="{1}"><!--这个class 用Spring bean name --> <!-- 返回信息 --> <result name="infoResult" type="json"> <param name="root">infoJson</param> </result> </action> </package>
3、在action 中读取配置文件.net
public class TestAction extends BaseAction { private String estoken; private String username; /** * 测试配置文件安全的返回json */ private String infoJson; /** * 测试配置文件安全加密 */ public String testProperties() { infoJson = "zjl:" + "username:" + username + "---" + "estoken:" + estoken; return "infoResult"; }
4、在properties 配置文件中写好配置prototype
XXX.propertiescode
estoken=hahaha username=lalala
参考 :https://blog.csdn.net/u012881904/article/details/50976100xml
:https://blog.csdn.net/StackFlow/article/details/79255916