【发布时间】:2013-10-24 01:02:32
【问题描述】:
我在我的 java web 应用程序中使用 spring。网站变大了,我想设置一些配置。
我一直在研究并遇到了诸如文档构建器工厂、用 java config 替换 spring xml 之类的东西。我不知道从哪里开始。
我正在考虑在 xml (WEB/newConfig.xml) 中实现配置并让 java bean 读取它。基本上我想将我的配置值输入到 xml 中并让它由一个 java bean 加载,以便我可以在控制器和 jstl 中使用它。
我只是在这里举一些例子。例如xml配置:
<property name="numberOfCars" value="3" />
<property name="webSiteName" value="New Spring Web App" />
....
我在我的 java 类中读到它:
class Config {
public getNumberOfCars() {
return numOfCars;
}
public getWebSiteName() {
return webSiteName;
}
}
我应该从哪里开始,我可以阅读哪些在线资料?
================================
更新
这是我创建的。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.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-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="/WEB-INF/your_prop_file.properties" />
<bean id="ConfigMgr" class="org.domain.class.ConfigMgr">
<property name="username" value="${username}">
</bean>
</beans>
you_prop_file.properties
username=hello world name
ConfigMgr.java
public class ConfigMgr {
private String username;
...getter
...setter
}
在我的控制器中,这是我所做的:
ConfigMgr config = new ConfigMgr();
sysout.out.println(config.getUsername());
我得到空值,我确定我在这里遗漏了一些东西。我应该在哪里将用户名值设置为 ConfigMgr 类?
【问题讨论】:
-
阅读spring documentation...它包含的信息超出了要求
-
在 stackoverflow 上请求站外资源是题外话。但是,您需要从 Spring 3.2.4 文档的第 5 章 IoC 容器开始。
-
ok tks man.. 将查看文档。这些天我似乎违反了stackoverflow =|
-
可以将配置属性保存在
properties文件中吗?
标签: java spring spring-mvc