On Thu, Apr 2, 2009 at 4:05 PM, Azazel Se <[email protected]> wrote:
>> Date: Thu, 2 Apr 2009 15:01:48 -0300
>> Subject: Re: Axis2 service and spring problem, can't find Spring's
>> ApplicationContext.
>> From: [email protected]
>> To: [email protected]
>>
>> On Thu, Apr 2, 2009 at 1:48 PM, Azazel Se <[email protected]> wrote:
>> >> From: [email protected]
>> >>
>> >> "ApplicationContextHolder.getContext()" isn't needed when using
>> >> SpringServletContextObjectSupplier. If spring and axis2 are working
>> >> together, you are done. You have to configure Spring to use
>> >> ApplicationContextHolder, otherwise it will return null values.
>> >>
>> >> - R
>> >
>> >> From: [email protected]
>> >> To: [email protected]
>> >
>> >> Here, how you try to use context .and can you provide error trace you
>> >> got ....? i cant get any clue with supplied details .
>> >> > .   ApplicationContext ctx = ApplicationContextHolder.getContext();
>> >> I don't think you need to have this line to access SpringContext
>> >> explicitly , because "MyServ " is already a Spring manged bean. if you
>> >> want to access any other bean inside "MyServ " use Spring injection
>> >> instead of access through ApplicationContextHolder.
>> >
>> >
>> >
>> >
>> > Hi, thanks for the replies.
>> >
>> > I have an application which I didn't make which uses spring, it has an
>> > API
>> > which hides the details and preferably I don't add any more stuff to it.
>> > The
>> > goal is to just use a few of the methods and get returns as normal and
>> > not
>> > interact directly with spring. I have never used Spring before so my
>> > knowledge about the innerworkings is small but if I get the API to work
>> > I
>> > shouldn't need to either. The springappl needs the context at
>> > initiating. I
>> > have tested it in a jsp file and it works, but I want to move it to a
>> > web
>> > service. In the jsp file I had the same context stuff added to the
>> > web.xml
>> > and this is in the jsp file:
>> >
>> > MyApplWithSpring  ap = new
>> >
>> > MyApplWithSpring(WebApplicationContextUtils.getWebApplicationContext(getServletContext()));
>> >
>> > This worked and I could use some of its methods afterwards.
>> > So I thought it would work in the web service as well if I used
>> > ApplicationContextHolder.getContext() and used new
>> > MyApplWithSpring(ApplicationContextHolder.getContext()). If possible how
>> > do
>> > I configure spring to use the ApplicationContextHolder so that it isn't
>> > null? Or is there any other(/better) way I can do this?
>> >
>> > -Az.
>> >
>>
>> So the "springappl" you didn't write requires an ApplicationContext
>> reference, even though you are successfully using
>> SpringServletContextObjectSupplier that doesn't really require an
>> ApplicationContext reference? If so, I'll show you how to do it.
>>
>> - R
>
>
>
> At the bottom are the first few lines of the code that works in eclipse. The
> problems arise when wanting to deploy to axis2 and tomcat. The application I
> am making a small wrapper for is very big and complex and the context.xml
> imports several other xml files. The goal of the web service is to receive a
> string from the client, use the application to do some stuff with a file and
> the string as input, then return the results to the client. The application
> uses spring, hibernate etc.
>
> -Az.
>
>
> //Create Platform.
> ApplicationContext context = new
> RavenAwareClassPathXmlApplicationContext("context.xml");
> TavernaBaseProfile profile = new TavernaBaseProfile(context);
> //Get workflow parser, load workflow.
> WorkflowParser parser = profile.getWorkflowParser();
> URL workflowURL = new URL("http://foo.com/workflow.xml";);
> Dataflow workflow = parser.createDataflow(workflowURL);
>

Apart from this code, is spring and axis2 running together in your web
service? I thought you said it was. I think I know what your are
asking for, but without knowing what "TavernaBaseProfile" is doing,
I'd be concerned that it creates two separate Spring worlds in your
app - you probably don't want that. Anyways, with that caveat, if you
just need "ApplicationContext" to pass into "TavernaBaseProfile" , I
would do the following:

Keep axis2 and spring as is, since its working. The idea behind the
axis2 ApplicationContextHolder is a known way to get an
ApplicationContext, and can be used outside axis2. So I think you
should create your own. Its easy, and it seems far less confusing, at
least to me, to use your own ApplicationContextHolder outside of axis2
when using SpringServletContextObjectSupplier. So you'd need this xml
in your Spring config:

     <!-- Use this trick to get access to Spring beans in junit and other places
         that are not spring aware
     -->
    <bean id="applicationContext"
      class="mypackage.spring.ApplicationContextHolder" />


And this class in your app:

package mypackage.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/** Implementation of a Spring interface who is configured in Spring's
 *  applicationContext.xml or some other Spring type of way. This class
 *  and the spring bean needed to wire it could be used as an alternative
 *  to getting the ApplicationContext from the ServletContext. It also can
 *  be used to get access to spring beans in places like junit that are not
 *  spring aware.
 */
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext appCtx;

    public ApplicationContextHolder() {}

    /** Spring supplied interface method for injecting app context. */
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
        appCtx = applicationContext;
    }

    /** Access to spring wired beans. */
    public static ApplicationContext getContext() {
        return appCtx;
    }

}

Then instead of:

ApplicationContext context = new
RavenAwareClassPathXmlApplicationContext("context.xml");

Do this:

ApplicationContext ctx = ApplicationContextHolder.getContext();
TavernaBaseProfile profile = new TavernaBaseProfile(context);

Good luck,
Robert

Reply via email to