2013-10-16 108 views
0

即时通讯使用弹簧我的Java Web应用程序。该网站已经变得更大,我想设置一些配置。春天java xml配置

我一直在研究和碰到像文件生成器工厂的东西,用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类?

+1

阅读[spring documentation](http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/)...它有超过所需的信息 –

+0

要求对于非现场资源而言,关于stackoverflow的话题已经不再。但是,您需要启动的地方是第5章Spring 3.2.4文档的IoC容器。 –

+0

确定tks man ..将查看文档。我似乎违反了这些天的计算器= | – nuttynibbles

回答

0

Spring Java配置是一个较新的特性,允许您使用Java类而不是XML文件来配置Spring应用程序。它只是XML配置的一种替代方案。 XML方式同样功能丰富。

从我能从你的问题中得出的结果,你想要将params(numberOfCars,webSiteName ..)的硬编码值移出你的配置文件。

如果是这样,你不必走得太远。

只需使用: -

<context:property-placeholder location="classpath:your_prop_file.properties" /> 
在Spring XML文件

和替换像帕拉姆值: -

<property name="webSiteName" value="${website.name}" /> 

你需要在你的classpath中your_prop_file.properties文件,像enteries : -

website.name=New Spring Web App 
+0

hi kumar tks的答复。即时在这里新鲜的如此原谅我。我应该在哪里添加?在servlet.xml中,还应该在哪里放置这个属性文件?在WEB-INF或资源目录中? – nuttynibbles

+0

应该添加到你的spring xml配置文件(例如applicationContext.xml)中。 将.properties文件放在包含在类路径中的任何源文件夹中。 –

+0

嘿库马尔,我输入了我的更新。请参阅上文。即时通讯不知道我应该在哪里设置用户名的值ConfigMgr类。我什至做得对吗?我甚至需要实现ConfigMgr.java? – nuttynibbles

0

你是不是注射日您在XML文件中创建的e ConfigMgr bean。
你在做什么是你正在控制器中创建一个新的对象,它没有关于属性文件的线索。
现在,您可以在控制器内使用@Autowired或通过xml配置注入它。
在基本的弹簧依赖注入的谷歌有很多可用的例子。

+0

雅男人我意识到我不得不使用汽车有线..即时通讯仍然试图使用j2ee来自开源。哈哈,这是一段旅程。 – nuttynibbles

+0

一旦你得到了它的味道,我会爱上它的.. ..顺便说一句,你可以upvote它,让其他人至少可以得到答案.. – Anubhab