0

Get value from properties file in classpath

asked 2013-11-13 20:13:43 +0800

hf gravatar image hf
0

updated 2013-11-14 05:20:06 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

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.

delete flag offensive retag edit

4 Answers

Sort by ยป oldest newest most voted
0

answered 2013-11-16 11:59:12 +0800

hf gravatar image hf
0

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:

  1. Define in applicationContext.xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:application.properties"/> </bean>

<bean id="propertiesUtil" class="my.package.MyClass"/>
  1. MyClass

    @Value("${some.property}") private String someProperty;

    public String getSomeProperty() {
        return someProperty;
    }
    
    1. in ViewModel class

    @WireVariable MyClass myClass;

and get value:

myClass.getSomeProperty()
link publish delete flag offensive edit
0

answered 2013-11-17 18:48:43 +0800

chillworld gravatar image chillworld flag of Belgium
5337 4 9
https://github.com/chillw...

updated 2013-11-17 18:49:01 +0800

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.

link publish delete flag offensive edit
0

answered 2013-11-17 19:10:16 +0800

hf gravatar image hf
0

This was my first try, but it didn't work. I got every null using @Value("${some.property}") in my ViewModel, because this class is manage from ZK and it isn't a Spring Bean.

Regards

link publish delete flag offensive edit
0

answered 2013-11-18 09:57:29 +0800

chillworld gravatar image chillworld flag of Belgium
5337 4 9
https://github.com/chillw...

updated 2013-11-18 21:42:40 +0800

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;
}

}

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-11-13 20:13:43 +0800

Seen: 52 times

Last updated: Nov 18 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More