How to inject a property from Tomcat's context

2019-07-17 18:45发布

问题:

The Seam documentation would have you believe that if you define a property in web.xml, or through a -D argument, it will find it and automatically set it on your bean. So if you have a bean called gateway with a property login and a setter, you can make a property called gateway.login in seam.properties, but if you define it in a -D, you have to use org.seam.properties.gateway.login. I got that to work (with the -D that is), but I could not get it to see either &Parameter or &Environment definitions from in context.xml.

I am doing this because I have keys to a merchant gateway that I cannot have in a text file in the project, and would rather not have in catalina.sh, since that might be used for other apps. The nice thing about context.xml is that it's on the server and it can be confined to the one application that uses the gateway.

回答1:

You should use the <Parameter/> option, not <Environment/> (to the best of my knowledge Seam does not search initialization parameters in JNDI). The name property should not include the org.jboss.seam.properties prefix (it is only for -D entries), so you should use:

<Parameter name="gateway.login" value="yourvalue" override="false" />

override="false" means that the value set here will have priority over equivalent <context-param/> tags in web.xml, if they exist (if you don't use the override option, context parameters defined in web.xml have priority over the one in context.xml).