spring - Why can't @Value inject a JNDI value from the Environment? -


i'm having trouble injecting jndi values tomcat spring java config using @value annotation whereas have no trouble retreiving values via environment class. using java 1.7.0_17, spring 4.0.3, , tomcat 7.0.52 .

i have in context.xml following variables defined:

<environment name="user_name" value="initech" type="java.lang.string" /> <environment name="user_password" value="1n3+3ch!" type="java.lang.string" /> 

in java configuration file have following code working:

@bean public foo foo( environment env ){     return new foo( env.getproperty("user_name"), env.getproperty("user_password") ); } 

when server log see:

12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.s.c.e.propertysourcespropertyresolver -> searching key 'user_name' in [servletconfiginitparams] 12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.s.c.e.propertysourcespropertyresolver -> searching key 'user_name' in [servletcontextinitparams] 12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.s.c.e.propertysourcespropertyresolver -> searching key 'user_name' in [jndiproperties] 12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.springframework.jndi.jnditemplate -> looking jndi object name [java:comp/env/user_name] 12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.s.jndi.jndilocatordelegate -> located object jndi name [java:comp/env/user_name] 12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.s.jndi.jndipropertysource -> jndi lookup name [user_name] returned: [initech] 12:50:45.214 [rmi tcp connection(3)-127.0.0.1] debug o.s.c.e.propertysourcespropertyresolver -> found key 'user_name' in [jndiproperties] type [string] , value 'initech' 

however, use @value annotation make code cleaner if @ possible; along lines of

@bean public foo foo(         @value( "#{user_name}" ) string username,         @value( "#{user_password}" ) string userpassword     ){      return new foo( username, userpassword ); } 

but code results in error:

nested exception org.springframework.expression.spel.spelevaluationexception: el1008e:(pos 0): property or field 'user_name' cannot found on object of type 'org.springframework.beans.factory.config.beanexpressioncontext' - maybe not public?; nested exception org.springframework.beans.factory.beanexpressionexception: expression parsing failed; nested exception org.springframework.expression.spel.spelevaluationexception: el1008e:(pos 0): property or field 'user_name' cannot found on object of type 'org.springframework.beans.factory.config.beanexpressioncontext' - maybe not public?; nested exception org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'oauthtokenrequestclient' defined in class path resource [com/initech/set/gw/api/config/gatewayrepositoryconfig.class]: unsatisfied dependency expressed through constructor argument index 0 of type [java.net.uri]: : expression parsing failed; nested exception org.springframework.expression.spel.spelevaluationexception: el1008e:(pos 0): property or field 'user_name' cannot found on object of type 'org.springframework.beans.factory.config.beanexpressioncontext' - maybe not public?; nested exception org.springframework.beans.factory.beanexpressionexception: expression parsing failed; nested exception org.springframework.expression.spel.spelevaluationexception: el1008e:(pos 0): property or field 'user_name' cannot found on object of type 'org.springframework.beans.factory.config.beanexpressioncontext' - maybe not public? org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:747) org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:462) 

i have tried "#{systemenvironment.user_name}, "#{systemproperties.user_name}, "#{jndiproperties.user_name}, , "#{java:comp/env/user_name} well: yet, none of them work.

ps- i've tried simplify question if 1 of these should have worked let me know because may have made error in posting.

i solved using the selected answer question java spring: how use @value annotation inject environment property?.

so code looks like:

@bean public foo foo(         @value( "#{environment.user_name}" ) string username,         @value( "#{environment.user_password}" ) string userpassword     ){      return new foo( username, userpassword ); } 

i'm assuming content of the upvoted answer (ie "assuming have propertysourcesplaceholderconfigurer registered") quesiton means lacking configured propertysourcesplaceholderconfigurer.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -