|
|
how to get application context from bean class?
Hi:
I am new to spring.
My question is how to get application context from bean class?
Basically I want to know how to do the following like what I did in servlet class.
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContex t(config.getServletContext());
EmailService service = (EmailService) ctx.getBean(quot;emailServicequot;);Many Thanks!!!
Code:
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { quot;myFirstApplicationContext.xmlquot;, quot;mySecondApplicationContext.xmlquot;, quot;myAsManyAsYouNeedApplicationContext.xmlquot; });
MyFancyBean bean = (MyFancyBean) ctx.getBean(quot;fancyBeanquot;);If the bean is from a Spring context itself, you can make it ApplicationContextAware (its an interface) and it will invoke the method on it when its created.
Can you describe where you want to do this? I usually create my application contexts as part of J2EE or servlet container created object (usually an EJB or Servlet). AbstractStatelessSessionBean is a convenience class for EJB 2.x objects that creates the application context and provides a getBeanFactory method.
Thanks...
But can you be more specific?
Such as where should I specify the location of the applicationContext.xml?
And what interface should I implement or which method I need to implement?
And what I need to do in ejb-jar.xml file?
Basically, I need to use EJB to send email regularly.
I am using IBM RAD.
So in my bean class, I also implement TimedObjct interface to have the timer object.
So I need to get the application context object first,
then I can get the EmailService to send the email.
Many Thanks
Take a look at this thread.
Also look at the reference manual about EJBs and the JavaDoc for AbstractStatelessSessionBean. It should give you more insight.
I take a look at the thread.
There is a xml file called jboss.xml
I have another question.
I am using IBM RAD 6.0, I tried to follow the sample code to create a project.
But I don't know how to handle this jboss.xml file....
Is there a corresponding xml file in IBM RAD6.0 ?
I got the following error...
org..beans.factory.BeanCreationExce ption: Error creating bean with name 'echoService' defined in file [C:\Documents and Settings\HP_Administrator\IBM\rationalsdp6.0\works pace\EchoService\beans-config.xml]: Initialization of bean failed; nested exception is javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.
javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.Many Thanks!!!
jboss.xml is the JBoss specific deployment descriptor. In weblogic its called weblogic-ejb-jar.xml. I'm not sure what its called in Webspere. Although XDoclet would help you generate it if you were doing it that way. |
|