Skip to content

Instantly share code, notes, and snippets.

@martinlau
Created November 30, 2012 00:29
Show Gist options
  • Save martinlau/4172896 to your computer and use it in GitHub Desktop.
Save martinlau/4172896 to your computer and use it in GitHub Desktop.
A hook which uses an external spring context to enter into spring managed transactions and dependency injection
package au.com.permeance.hook;
import au.com.permeance.service.FakeService;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.SimpleAction;
import com.liferay.portal.kernel.servlet.ServletContextPool;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
public class StartupHookAction extends SimpleAction {
@Override
public void run(final String[] ids) throws ActionException {
ServletContext servletContext = ServletContextPool.get("spring-hook");
BeanFactory beanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
FakeService fakeService = beanFactory.getBean(FakeService.class);
try {
fakeService.doStuff();
}
catch (Exception e) {
throw new ActionException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment