How to have a class use applicationContext beans?


How to have a class use applicationContext beans?



I'm working on my first java struts2 webapp and want to be able to inject bean parameters into any arbitrary class that is called. But I find that I can only do this with struts action classes...

Let's say I have this bean in my applicationContext.xml file:

<bean id="BeanTest" class="BeanTest">     <property name="test" value="someval" /> </bean> 

If I have a struts action class setup called BeanTest (like so), and I add a setter (public void setTest()), then the test parameter will be set and I can access it.

import com.opensymphony.xwork2.ActionSupport;  public class BeanTest extends ActionSupport{     private String test;      public String execute(){         String str = getTest(); // returns "someval"         return "success";     }      public void setTest(String test){         this.test = test;     }      public String getTest(){         return test;     } } 

However, let's say I change the bean to BeanTest2 like so:

<bean id="BeanTest2" class="BeanTest2">     <property name="test" value="someval" /> </bean> 

And I have a standalone class like so:

public class BeanTest2{     private test;      public void setTest(String test){         this.test = test;     }      public String getTest(){         return test;     } } 

If I create an instance of BeanTest2 in BeanTest, and call getTest, it always returns null.

import com.opensymphony.xwork2.ActionSupport;  public class BeanTest extends ActionSupport{     public String execute(){          BeanTest2 bt = new BeanTest2();         String str = bt.getTest(); //returns null, but I want "someval"          return "success";     } } 

What I want to do is set up a bean in applicationContext so that I can point it to an arbitrary class and that class will always get whatever bean parameters I set (assuming I've created setters for them). Unfortunately, what happens is that only struts action classes are able to get these bean properties. Everything is not getting set.

Is this question clear? I feel like I'm missing something obvious about the way beans work.




weird grails error message

1:



Spring @Transactional merge and persist question
I think Spring will normally do dependency injection only for those classes that are created by Spring, not for the ones that you create yourself with new operator..
caching spring/hibernate webapp
BeanTest is created by Spring so it will get its dependencies injected, but BeanTest2 is not created by Spring, thus Spring knows nothing about BeanTest2 instances..
Mail for Blackberry's - multiple content attachments
You can add BeanTest2 as a field in BeanTest.
Servlet “newspring” is not available
public class BeanTest {    private BeanTest2 beanTest2;    public void setBeanTest2(BeanTest2 b) { this.beanTest2 = b; }    public BeanTest2 getBeanTest2() { return this.beanTest2; }; } 
Then you can inject beanTest2 to beanTest instance..
Creating temporary JMS jms topic in Spring
<bean id="beanTest2" class="BeanTest2">     <property name="test" value="someval" /> </bean>  <bean id="beanTest" class="BeanTest">     <property name="beanTest2" ref="beanTest2" /> </bean> 
This way beanTest2 should be injected into BeanTest instance..
Out of Memory Error in Eclipse


Dereferencing Aliases in LDAP using Spring


94 out of 100 based on 74 user ratings 574 reviews

*