-
1. Re: Bean property configuration
alin.heyoulin.qq.com Dec 27, 2009 6:36 PM (in response to johaneltes)I have the same question. So i like spring xml property inject. Does weld can do it?
-
2. Re: Bean property configuration
gavin.king Dec 27, 2009 11:45 PM (in response to johaneltes)
Using Spring with an xml bean source, I typically use the xml file to...
a) Determine which implementation class to use for an interfaceIn CDI, we use @Altenative for that.
b) Configure values for bean properties of a bean (e.g. a JMSTemplate).There are various ways to approach this. You could use a portable extension to read values from properties files or XML, or define your properties in Java. I'm not quite sure precisely which route I would go down myself. Probably the Java route.
-
3. Re: Bean property configuration
alin.heyoulin.qq.com Dec 28, 2009 2:13 AM (in response to johaneltes)If i want inject property into an other java class of jar it is not manager bean. eg:org.apache.commons.dbcp.BasicDataSource
So i would like using xml to config manager bean and have highest priority. So it is not restricted in one jar and it can overide java class define.
-
4. Re: Bean property configuration
alin.heyoulin.qq.com Dec 28, 2009 2:23 AM (in response to johaneltes)I hope seam3 will have components.xml config file
-
5. Re: Bean property configuration
gavin.king Dec 28, 2009 2:55 AM (in response to johaneltes)Yes, the plan is to implement something very similar as a portable extension for CDI. Actually, some work has already been done on that.
-
6. Re: Bean property configuration
meetoblivion Dec 28, 2009 2:57 AM (in response to johaneltes)what exactly do you feel components.xml will do that you can't do with weld? it seems like the overall goal in weld is injecting via a very little bit of code rather than a verbose xml document (and yes, i've worked at places that have excruciatingly painful spring xml files)
-
7. Re: Bean property configuration
gavin.king Dec 28, 2009 3:10 AM (in response to johaneltes)
John Ament wrote on Dec 28, 2009 02:57:
what exactly do you feel components.xml will do that you can't do with weld? it seems like the overall goal in weld is injecting via a very little bit of code rather than a verbose xml document (and yes, i've worked at places that have excruciatingly painful spring xml files)Agreed, the kinds of things that you use XML for in Spring can be accomplished easily in Java in CDI. For example:
@Alternative class Testing { @Produces @LoggedIn User user = new User("gavin", "foobar"); @Produces @Admin User admin = new User("admin", "nimda"); @Produces @UserDatabase EntityManager getUserDatabaseEntityManager(@New MockEntityManager em) { em.add(user); em.add(admin); return em; } @Produces Level logLevel = Level.DEBUG; }
class Production { @Produces @UserDatabase @PersistenceContext(unitName="UserDB") EntityManager em; @Produces Level logLevel = Level.WARN; }
when you enable Testing in beans.xml, you get its definition of stuff. Otherwise you get the defaults defined in Production. No need for complex XML files.
-
8. Re: Bean property configuration
alin.heyoulin.qq.com Dec 28, 2009 3:16 AM (in response to johaneltes)xml config file can let you reuse your exist old pojo and third part java bean and make it become maneger bean. I don't know how weld to do easily.
-
9. Re: Bean property configuration
gavin.king Dec 28, 2009 3:25 AM (in response to johaneltes)
he youlin wrote on Dec 28, 2009 03:16:
xml config file can let you reuse your exist old pojo and third part java bean and make it become maneger bean. I don't know how weld to do easily.Write a producer method or field:
@Produces @Special @Named @SessionScoped Foo foo = new Foo(....);
If the constructor is not enough to init the object, use a producer method:
@Produces @Special @Named @SessionScoped Foo getFoo() { Foo f = new Foo(); f.init(); return f; }
If you need to inject into the Foo, or add interceptors/decorators (unlikely, since it is legacy code), use @New:
@Produces @Special @Named @SessionScoped Foo getFoo(@New Foo f) { return f; }
Easy!
-
10. Re: Bean property configuration
meetoblivion Dec 28, 2009 3:27 AM (in response to johaneltes)i guess one of the big things missing from weld as it stands (and will likely be addressed via an extension) is there's not native way to just wire a bean without any code, it requires a producer method. spring did this via the properties set in the xml, and really the difference here is you need to write a little bit of code, instead of having it just there in an xml file.
but if you think about it, most things that you inject via spring's xml can already be injected automatically in weld - other beans, database connections, property files, etc.
-
11. Re: Bean property configuration
alin.heyoulin.qq.com Dec 28, 2009 3:28 AM (in response to johaneltes)Somtimes i don't want change the java code at app start up. Just change xml config
-
12. Re: Bean property configuration
gavin.king Dec 28, 2009 3:35 AM (in response to johaneltes)
i guess one of the big things missing from weld as it stands (and will likely be addressed via an extension) is there's not native way to just wire a bean without any code, it requires a producer method. spring did this via the properties set in the xml, and really the difference here is you need to write a little bit of code, instead of having it just there in an xml file.Cos Spring XML is not code? Nonsense, it's totally code. It's virtually a scripting language, in fact.
Like I said, we will implement XML configuration as a portable extension, but it's a kinda old-fashioned thing that's not really needed in the world of CDI.
Somtimes i don't want change the java code at app start up. Just change xml configRight. That's the purpose of @Alternative. You can active the particular @Alternative you're interested in just by changing beans.xml.
Read this stuff:
- http://in.relation.to/Bloggers/MakingExternalResourcesConfigurableWithoutLotsOfXML
- http://in.relation.to/Bloggers/MakingExternalResourcesConfigurableWithoutLotsOfXMLPart2
- http://in.relation.to/Bloggers/ModularDependencies
-
13. Re: Bean property configuration
meetoblivion Dec 28, 2009 3:46 AM (in response to johaneltes)Actually, I think the appropriate use case is when you want to change the value of some primitive type from n to 2n.
and yes, spring xml is its own scripting language, i wasn't trying to infer otherwise. actually another thing I want to try to get done (once I have the what i need to get dones done) is groovy script extension for producing beans. that'd be cool, right?
-
14. Re: Bean property configuration
alin.heyoulin.qq.com Dec 28, 2009 3:54 AM (in response to johaneltes)Alternative still have some hard code. And do i have all preconcerted Alternative?
BWT Alternative is not clear than xml