Created
September 16, 2015 14:51
-
-
Save asouza/1200a7c8ac07bfeeb4b0 to your computer and use it in GitHub Desktop.
Processor that converts all controllers in request scoped objects :). Just an example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.casadocodigo.loja.infra.spring; | |
import org.springframework.beans.BeansException; | |
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; | |
import org.springframework.beans.factory.config.BeanDefinition; | |
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | |
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.type.AnnotationMetadata; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.context.WebApplicationContext; | |
@Configuration | |
public class ControllersMustBeRequestScope implements BeanFactoryPostProcessor { | |
@Override | |
public void postProcessBeanFactory( | |
ConfigurableListableBeanFactory factory) throws BeansException { | |
for (String beanName : factory.getBeanDefinitionNames()) { | |
BeanDefinition beanDef = factory.getBeanDefinition(beanName); | |
if(beanDef instanceof AnnotatedBeanDefinition){ | |
AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDef; | |
AnnotationMetadata metadata = annotatedBeanDefinition.getMetadata(); | |
if(metadata.hasAnnotation(Controller.class.getName())){ | |
beanDef.setScope(WebApplicationContext.SCOPE_REQUEST); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment