0

Can't inject using @Value in ViewModel if not declaring as @Component.. so what do we use?

asked 2013-05-08 23:17:33 +0800

rickcr gravatar image rickcr
704 7

updated 2013-05-08 23:18:02 +0800

This is extremely frustrating and I hope there is a work around or else I'll have to go back to doing things the 'non standard away'

I want to inject a properties value into my ViewModel using @Value (or some annotation?) eg:

@Value("${someProp}")
private String someProp;

The issue is this only seems to work when I declare my ViewModel using the anti-pattern:

 @Component("myViewModel")
 @Scope("prototype")
  public class MyViewModel {

and then in my zul...

   <window  apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init(myViewModel)" >

However the zk spring docs recommend NOT declaring your ViewModel as a component, so if I follow the correct pattern, how do I inject a property? @Value doesn't work and @WireVariable does not work either.

Thanks. I really need to get this addressed since we need to inject properties in a lot of our view models.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-05-09 00:47:22 +0800

rickcr gravatar image rickcr
704 7

I came up with a work-around, but it's not nice. We should be able to leverage something similar to @Value... maybe spring-zk with an @WireValue ?

Anyway, my work around ...

1) Declare a util to the props file in your config:

<util:properties id="adminProps" location="classpath:web-application.properties" />

NOTE: previously using @Value all I needed to declare was:

  <context:property-placeholder location="classpath:web-application.properties" ignore-resource-not-found="true" ignore-unresolvable="true"/>

2) Directly inject the properties file in your view model with @WiveVariable

   @WireVariable
   protected Properties adminProps;

3) When you need a property access it from the properties object you injected

   String url = adminProps.getProperty("helpUrl");
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-05-08 23:17:33 +0800

Seen: 37 times

Last updated: May 09 '13

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