Home / Class/ AopAutoConfigurationTests Class — spring-boot Architecture

AopAutoConfigurationTests Class — spring-boot Architecture

Architecture documentation for the AopAutoConfigurationTests class in AopAutoConfigurationTests.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/aop/AopAutoConfigurationTests.java lines 46–190

class AopAutoConfigurationTests {

	private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
		.withConfiguration(AutoConfigurations.of(AopAutoConfiguration.class));

	@Test
	void aopDisabled() {
		this.contextRunner.withUserConfiguration(TestConfiguration.class)
			.withPropertyValues("spring.aop.auto:false")
			.run((context) -> {
				TestAspect aspect = context.getBean(TestAspect.class);
				assertThat(aspect.isCalled()).isFalse();
				TestBean bean = context.getBean(TestBean.class);
				bean.foo();
				assertThat(aspect.isCalled()).isFalse();
			});
	}

	@Test
	void aopWithDefaultSettings() {
		this.contextRunner.withUserConfiguration(TestConfiguration.class).run(proxyTargetClassEnabled());
	}

	@Test
	void aopWithEnabledProxyTargetClass() {
		this.contextRunner.withUserConfiguration(TestConfiguration.class)
			.withPropertyValues("spring.aop.proxy-target-class:true")
			.run(proxyTargetClassEnabled());
	}

	@Test
	void aopWithDisabledProxyTargetClass() {
		this.contextRunner.withUserConfiguration(TestConfiguration.class)
			.withPropertyValues("spring.aop.proxy-target-class:false")
			.run(proxyTargetClassDisabled());
	}

	@Test
	void customConfigurationWithProxyTargetClassDefaultDoesNotDisableProxying() {
		this.contextRunner.withUserConfiguration(CustomTestConfiguration.class).run(proxyTargetClassEnabled());

	}

	@Test
	void whenGlobalMethodSecurityIsEnabledAndAspectJIsNotAvailableThenClassProxyingIsStillUsedByDefault() {
		this.contextRunner.withClassLoader(new FilteredClassLoader(Advice.class))
			.withUserConfiguration(ExampleController.class, EnableGlobalMethodSecurityConfiguration.class)
			.run((context) -> assertThat(context).getBean(ExampleController.class).matches(AopUtils::isCglibProxy));
	}

	private ContextConsumer<AssertableApplicationContext> proxyTargetClassEnabled() {
		return (context) -> {
			TestAspect aspect = context.getBean(TestAspect.class);
			assertThat(aspect.isCalled()).isFalse();
			TestBean bean = context.getBean(TestBean.class);
			bean.foo();
			assertThat(aspect.isCalled()).isTrue();
		};
	}

	private ContextConsumer<AssertableApplicationContext> proxyTargetClassDisabled() {
		return (context) -> {
			TestAspect aspect = context.getBean(TestAspect.class);
			assertThat(aspect.isCalled()).isFalse();
			TestInterface bean = context.getBean(TestInterface.class);
			bean.foo();
			assertThat(aspect.isCalled()).isTrue();
			assertThat(context).doesNotHaveBean(TestBean.class);
		};
	}

	@EnableAspectJAutoProxy
	@Configuration(proxyBeanMethods = false)
	@Import(TestConfiguration.class)
	static class CustomTestConfiguration {

	}

	@Configuration(proxyBeanMethods = false)
	static class TestConfiguration {

		@Bean
		TestAspect aspect() {
			return new TestAspect();
		}

		@Bean
		TestInterface bean() {
			return new TestBean();
		}

	}

	static class TestBean implements TestInterface {

		@Override
		public void foo() {
		}

	}

	@Aspect
	static class TestAspect {

		private boolean called;

		boolean isCalled() {
			return this.called;
		}

		@Before("execution(* foo(..))")
		void before() {
			this.called = true;
		}

	}

	interface TestInterface {

		void foo();

	}

	@EnableMethodSecurity(prePostEnabled = true)
	@Configuration(proxyBeanMethods = false)
	static class EnableGlobalMethodSecurityConfiguration {

	}

	public static class ExampleController implements TestInterface {

		@RequestMapping("/test")
		@PreAuthorize("true")
		String demo() {
			return "test";
		}

		@Override
		public void foo() {

		}

	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free