-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I use zk with spring and i would like get a value from application.properties file, that is in classpath, contains my values configurations.
At startup of tomcat I find this string:
[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
] Loading properties file from class path resource [application.properties
]
Farther in this file there are all properties that I use to configure DataSource in applicationContext.xml
, this data source works fine.
I can't get property value in ViewModel. I have tried with: @Value("${general.newLineChar}")
but doesn't work. How is the best way to get properties from a file in zk?
Thanks.
The best way to read properties in zk is defining a spring bean that reads values from properties file using @Value("${some.property}") annotation. For getting this value in ViewModel class you can inject spring bean using @WireVariable annotation. Example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:application.properties"/> </bean>
<bean id="propertiesUtil" class="my.package.MyClass"/>
MyClass
@Value("${some.property}") private String someProperty;
public String getSomeProperty() {
return someProperty;
}
@WireVariable MyClass myClass;
and get value:
myClass.getSomeProperty()
1.Define in applicationContext.xml: (correct)
bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:application.properties"/> </bean> <bean id="propertiesUtil" class="my.package.MyClass"/>
1.Viewmodel !!!!
@Value("${some.property}") private String someProperty;
create getter and if u need in the zul, do @load(someProperty)
Greetz chill.
Extends your vm with this:
package your.package;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zk.ui.Executions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractVM {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
private WebApplicationContext applicationContext;
public AbstractVM() {
this.autowire();
}
private void autowire() {
this.getApplicationContext().getAutowireCapableBeanFactory()
.autowireBean(this);
}
public ApplicationContext getApplicationContext() {
if (applicationContext == null) {
applicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
}
return applicationContext;
}
public ServletContext getServletContext() {
return Executions.getCurrent().getDesktop().getWebApp()
.getServletContext();
}
public void setApplicationContext(WebApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
Asked: 2013-11-13 20:13:43 +0800
Seen: 60 times
Last updated: Nov 18 '13
Zk + Spring Security redirect to strange page after login
ZK Spring webflow and flowScope
Spring security doesn't return user inside event listener
ZK Spring 3.1.1 MVVM Use @Scope/@Component or No?
Upgrading from Spring3.1 to 3.1.1 Maven.. pulls in 3.0.x jars... issues
When would I want to use ThreadLocalListener?
ZKoss with springsecurity using database