Showing posts with label ApplicationContext. Show all posts
Showing posts with label ApplicationContext. Show all posts

Friday, September 19, 2014

Access Spring-ApplicationContext/ServletContext From Everywhere

Create a java class such named 'AppUtils.java' with following contents:


package com.pkm.maven.common;

import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class AppUtils implements ApplicationContextAware {
    private static ApplicationContext ctx;
    private static ServletContext __servletContext;
    
    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        AppUtils.ctx = ctx;
    }
    
    public static ApplicationContext getApplicationContext() {
        return ctx;
    }
    
    public static ServletContext getServletContext() {
        if (__servletContext == null) {
            __servletContext = (ServletContext) ctx.getBean("servletContext");
        }
        return __servletContext;
    }
}

Now make a entry to 'applicationContext.xml'


<bean id="contextApplicationContextProvider" class="com.pkm.maven.common.AppUtils"></bean>

Finally access application context/servlet context from everywhere like this:

package com.pkm.maven.app;

import com.pkm.maven.common.AppUtils;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;

public class AppHandler {
    
    public static String getContextPath() {
        try {
            ApplicationContext applicationContext = AppUtils.getApplicationContext();
            ServletContext servletContext = AppUtils.getServletContext();
            return servletContext.getContextPath();
        }
        catch (Exception ex) {
            return "<b>ERROR</b>";
        }
    }
}

Monday, February 10, 2014

Use grails service from java class


package com.groovy.exception

import com.services.TestService
import org.springframework.context.ApplicationContext
import org.codehaus.groovy.grails.web.util.WebUtils

/**
 * Created by pritom on 9/02/14.
 */
class CustomException extends RuntimeException {
    private static TestService testService;
    String message;
    List args = [];

    public CustomException(String message, List args) {
        this.message = message;
        this.args = args;
    }

    public String getMessage() {
        return this.toString();
    }

    public List getArgs() {
        return this.args;
    }

    public String toString() {
        String _message = testService.getMessage(this.message, this.args);
        return _message
    }

    static {
        if(!testService) {
            ApplicationContext applicationContext = WebUtils.retrieveGrailsWebRequest().getApplicationContext();
            testService = applicationContext.getBean("testService");
        }
    }
}

Wednesday, August 7, 2013

How do I get an instance of a Grails service programmatically

The Grails documentation describes a way to get a service when in a servlet. This might be useful, if you can obtain the same objects in your context:

ApplicationContext ctx = (ApplicationContext)ApplicationHolder.getApplication().getMainContext();
CountryServiceInt service = (CountryServiceInt) ctx.getBean("countryService");
String str = service.sayHello("Some String!");