Java Code Examples for org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator

The following examples show how to use org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public HandlerMethodAnnotationDetectionTests(final Class<?> controllerType, boolean useAutoProxy) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.registerBeanDefinition("controller", new RootBeanDefinition(controllerType));
	context.registerBeanDefinition("handlerMapping", new RootBeanDefinition(RequestMappingHandlerMapping.class));
	context.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(RequestMappingHandlerAdapter.class));
	context.registerBeanDefinition("exceptionResolver", new RootBeanDefinition(ExceptionHandlerExceptionResolver.class));
	if (useAutoProxy) {
		DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
		autoProxyCreator.setBeanFactory(context.getBeanFactory());
		context.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
		context.registerBeanDefinition("controllerAdvice", new RootBeanDefinition(ControllerAdvisor.class));
	}
	context.refresh();

	this.handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
	this.handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
	this.exceptionResolver = context.getBean(ExceptionHandlerExceptionResolver.class);
	context.close();
}
 
Example 2
Source Project: spring-analysis-note   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncMethodsWithQualifier() throws Exception {
	originalThreadName = Thread.currentThread().getName();
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodWithQualifierBean.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	context.registerBeanDefinition("e0", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.refresh();

	AsyncMethodWithQualifierBean asyncTest = context.getBean("asyncTest", AsyncMethodWithQualifierBean.class);
	asyncTest.doNothing(5);
	asyncTest.doSomething(10);
	Future<String> future = asyncTest.returnSomething(20);
	assertEquals("20", future.get());
	Future<String> future2 = asyncTest.returnSomething2(30);
	assertEquals("30", future2.get());
}
 
Example 3
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerclass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
				throws BeansException {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());
}
 
Example 4
Source Project: spring-analysis-note   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncMethodListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodListener.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 1);
	context.close();
}
 
Example 5
Source Project: spring-analysis-note   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncClassListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	listenerConstructed = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassListener.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	context.close();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 2);
	assertEquals(1, listenerConstructed);
}
 
Example 6
Source Project: spring-analysis-note   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncPrototypeClassListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	listenerConstructed = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class);
	listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("asyncTest", listenerDef);
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	context.close();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 2);
	assertEquals(2, listenerConstructed);
}
 
Example 7
@Test
public void genericsBasedInjectionWithEarlyGenericsMatchingOnCglibProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setProxyTargetClass(true);
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));

	String[] beanNames = beanFactory.getBeanNamesForType(Repository.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo")));
}
 
Example 8
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnCglibProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setProxyTargetClass(true);
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(Repository.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo")));
}
 
Example 9
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnCglibProxyAndRawFactoryMethod() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawFactoryMethodRepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setProxyTargetClass(true);
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(Repository.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo")));
}
 
Example 10
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerclass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
				throws BeansException {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setProxyTargetClass(true);
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());
}
 
Example 11
@Test
public void genericsBasedInjectionWithEarlyGenericsMatchingOnJdkProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}
 
Example 12
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}
 
Example 13
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxyAndRawFactoryMethod() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawFactoryMethodRepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}
 
Example 14
Source Project: spring-analysis-note   Source File: JdkProxyControllerTests.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerclass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerclass));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());
}
 
Example 15
Source Project: spring-analysis-note   Source File: CglibProxyControllerTests.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setProxyTargetClass(true);
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			Pointcut pointcut = new AnnotationMatchingPointcut(Controller.class);
			DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleTraceInterceptor(true));
			wac.getBeanFactory().registerSingleton("advisor", advisor);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());
}
 
Example 16
@Test
public void proxiedFormController() throws Exception {
	initServlet(wac -> {
		wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class));
		DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
		autoProxyCreator.setBeanFactory(wac.getBeanFactory());
		wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
		wac.getBeanFactory()
				.registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	}, MyFormController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("name", "name1");
	request.addParameter("age", "value2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myView-name1-typeMismatch-tb1-myValue", response.getContentAsString());
}
 
Example 17
public HandlerMethodAnnotationDetectionTests(Class<?> controllerType, boolean useAutoProxy) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.registerBeanDefinition("controller", new RootBeanDefinition(controllerType));
	context.registerBeanDefinition("handlerMapping", new RootBeanDefinition(RequestMappingHandlerMapping.class));
	context.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(RequestMappingHandlerAdapter.class));
	context.registerBeanDefinition("exceptionResolver", new RootBeanDefinition(ExceptionHandlerExceptionResolver.class));
	if (useAutoProxy) {
		DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
		autoProxyCreator.setBeanFactory(context.getBeanFactory());
		context.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
		context.registerBeanDefinition("controllerAdvice", new RootBeanDefinition(ControllerAdvisor.class));
	}
	context.refresh();

	this.handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
	this.handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
	this.exceptionResolver = context.getBean(ExceptionHandlerExceptionResolver.class);
	context.close();
}
 
Example 18
Source Project: java-technology-stack   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncMethodsWithQualifier() throws Exception {
	originalThreadName = Thread.currentThread().getName();
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodWithQualifierBean.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	context.registerBeanDefinition("e0", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.refresh();

	AsyncMethodWithQualifierBean asyncTest = context.getBean("asyncTest", AsyncMethodWithQualifierBean.class);
	asyncTest.doNothing(5);
	asyncTest.doSomething(10);
	Future<String> future = asyncTest.returnSomething(20);
	assertEquals("20", future.get());
	Future<String> future2 = asyncTest.returnSomething2(30);
	assertEquals("30", future2.get());
}
 
Example 19
Source Project: java-technology-stack   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncMethodsWithQualifierThroughInterface() throws Exception {
	originalThreadName = Thread.currentThread().getName();
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(SimpleAsyncMethodWithQualifierBean.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	context.registerBeanDefinition("e0", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.refresh();

	SimpleInterface asyncTest = context.getBean("asyncTest", SimpleInterface.class);
	asyncTest.doNothing(5);
	asyncTest.doSomething(10);
	Future<String> future = asyncTest.returnSomething(20);
	assertEquals("20", future.get());
	Future<String> future2 = asyncTest.returnSomething2(30);
	assertEquals("30", future2.get());
}
 
Example 20
Source Project: java-technology-stack   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncMethodListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodListener.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 1);
	context.close();
}
 
Example 21
Source Project: java-technology-stack   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncClassListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	listenerConstructed = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassListener.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	context.close();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 2);
	assertEquals(1, listenerConstructed);
}
 
Example 22
Source Project: java-technology-stack   Source File: AsyncExecutionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void asyncPrototypeClassListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	listenerConstructed = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class);
	listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("asyncTest", listenerDef);
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	context.close();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 2);
	assertEquals(2, listenerConstructed);
}
 
Example 23
@Test
public void genericsBasedInjectionWithEarlyGenericsMatchingOnCglibProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setProxyTargetClass(true);
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));

	String[] beanNames = beanFactory.getBeanNamesForType(Repository.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo")));
}
 
Example 24
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnCglibProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setProxyTargetClass(true);
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(Repository.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo")));
}
 
Example 25
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnCglibProxyAndRawFactoryMethod() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawFactoryMethodRepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setProxyTargetClass(true);
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(Repository.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(Repository.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isCglibProxy(beanFactory.getBean("stringRepo")));
}
 
Example 26
@Test
@SuppressWarnings("serial")
public void proxiedStandardHandleMethod() throws Exception {
	DispatcherServlet servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(MyController.class));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("test", response.getContentAsString());
}
 
Example 27
@Test
public void genericsBasedInjectionWithEarlyGenericsMatchingOnJdkProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}
 
Example 28
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxy() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}
 
Example 29
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxyAndRawFactoryMethod() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawFactoryMethodRepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}
 
Example 30
@Test
public void genericsBasedInjectionWithLateGenericsMatchingOnJdkProxyAndRawInstance() {
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RawInstanceRepositoryConfiguration.class));
	new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory);
	DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
	autoProxyCreator.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(autoProxyCreator);
	beanFactory.registerSingleton("traceInterceptor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
	beanFactory.preInstantiateSingletons();

	String[] beanNames = beanFactory.getBeanNamesForType(RepositoryInterface.class);
	assertTrue(ObjectUtils.containsElement(beanNames, "stringRepo"));

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(RepositoryInterface.class, String.class));
	assertEquals(1, beanNames.length);
	assertEquals("stringRepo", beanNames[0]);

	assertTrue(AopUtils.isJdkDynamicProxy(beanFactory.getBean("stringRepo")));
}