package net.sourceforge.stripes.integration.spring; import net.sourceforge.stripes.action.*; import net.sourceforge.stripes.controller.*; import net.sourceforge.stripes.util.*; import org.springframework.beans.factory.config.*; import org.springframework.context.*; import org.springframework.util.*; import org.springframework.web.context.support.*; import javax.servlet.*; /** *

An {@link net.sourceforge.stripes.controller.Interceptor} that uses a Spring context to inject * Spring beans into newly created ActionBeans immediately following ActionBeanResolution. * Dependency injection is performed by Spring's native autowiring bean factory, so all of the * standard Spring dependency injection annotations are supported, including @Autowired, * @Resource, and @Qualifier. Fields and/or methods may be annotated with these annotations. *

*

Example:

*
 * public class SomeActionBean implements ActionBean
 * {
 *     // Autowire by type injection on a field (no setter is necessary)
 *     @Autowired private UserService userService;
 *
 *     private TravelService travelService;
 *
 *     // Autowire by name injection via a setter method
 *     @Resource
 *     public void setTravelService(TripService travelService) { this.travelService = travelService; }
 *
 *     //...
 * }
 * 
*

To configure the SpringInterceptor for use you will need to add the following to your * web.xml (assuming no other interceptors are yet configured):

*

*

 * <init-param>
 *     <param-name>Interceptor.Classes</param-name>
 *     <param-value>
 *         net.sourceforge.stripes.integration.spring.Spring25Interceptor
 *     </param-value>
 * </init-param>
 * 
*

*

If one or more interceptors are already configured in your web.xml simply separate the * fully qualified names of the interceptors with commas (additional whitespace is ok).

* *

This interceptor requires Spring 2.5 or newer.

* * @author Christian Nelson * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory * @see org.springframework.beans.factory.annotation.Autowired * @see org.springframework.beans.factory.annotation.Qualifier * @see javax.annotation.Resource * @since Stripes 1.5 */ @Intercepts(LifecycleStage.ActionBeanResolution) public class Spring25Interceptor implements Interceptor { private static final Log log = Log.getInstance(Spring25Interceptor.class); /** * Allows ActionBean resolution to proceed and then once the ActionBean has been located performs Spring dependency injection. * * @param context the current execution context * @return the Resolution produced by calling context.proceed() * @throws Exception if the Spring binding process produced unrecoverable errors */ public Resolution intercept(ExecutionContext context) throws Exception { Resolution resolution = context.proceed(); log.debug("Running Spring dependency injection for instance of ", context.getActionBean().getClass().getSimpleName()); ServletContext servletContext = StripesFilter.getConfiguration().getServletContext(); ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); beanFactory.autowireBeanProperties(context.getActionBean(), AutowireCapableBeanFactory.AUTOWIRE_NO, false); beanFactory.initializeBean(context.getActionBean(), StringUtils.uncapitalize(context.getActionBean().getClass().getSimpleName())); return resolution; } }