Enable Spring Dependency Injection for New Objects with @Configurable
This guide explains how to use Spring's @Configurable annotation and the AnnotationBeanConfigurerAspect to inject dependencies into objects created with new, covering environment setup, Maven configuration, AspectJ weaving, and advanced options like autowiring, constructor injection, and dependency checks.
1. Introduction
Spring's AnnotationBeanConfigurerAspect in spring-aspects.jar allows dependency injection for objects that are not managed by the Spring container, such as domain objects instantiated with new or returned from ORM queries.
<code>public class Account {
private Long id;
private String name;
@Resource
private BaseService baseService;
}</code>The Account class above is a plain domain object; @Resource injection will not work without additional configuration.
2. Practical Example
2.1 Environment Preparation
<code><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency></code>Define a service to be injected:
<code>@Component
public class BaseService {
public void save() {
System.out.println("BaseService save...");
}
}</code>2.2 Enable Configuration
<code>@Configuration
// Enable @Configurable processing
@EnableSpringConfigured
public class AppConfig {
}</code>The @EnableSpringConfigured annotation registers the AnnotationBeanConfigurerAspect with the container.
2.3 Define Configurable Class
<code>@Configurable
public class Account {
@Resource
private BaseService baseService;
public void s() {
this.baseService.save();
}
}</code>Although Account is not a Spring bean, the @Configurable annotation triggers AspectJ to enhance the class at compile time, enabling injection.
2.4 Configure AspectJ Maven Plugin
<code><plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.15.0</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<outxml>true</outxml>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin></code>If weaving does not take effect, recompile the project.
2.5 Test
<code>try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.pack.aop.configurable")) {
Account account = new Account();
account.s();
}</code>Output:
<code>BaseService save...</code>The Account instance created with new works correctly, and the generated Account.class shows the applied enhancement.
2.6 Additional Configurations
Autowiring You can specify the autowire mode on @Configurable : <code>@Configurable(autowire = Autowire.BY_TYPE) public class Account { private BaseService baseService; public void setBaseService(BaseService baseService) { this.baseService = baseService; } public void s() { this.baseService.save(); } }</code>
Constructor Injection By default, dependencies are not available in the constructor. Enable them with preConstruction = true : <code>@Configurable(autowire = Autowire.BY_TYPE, preConstruction = true) public class Account { @Resource private BaseService baseService; public Account() { System.out.println(this.baseService); } }</code>
Dependency Check Set dependencyCheck = true to enforce that required beans exist, otherwise an exception is thrown. <code>@Configurable(autowire = Autowire.BY_TYPE, preConstruction = true, dependencyCheck = true) public class Account {}</code>
2.7 Load‑time Weaving (optional)
Alternatively, enable weaving at runtime by adding the Spring Instrument agent:
<code>-javaagent:D:/java/maven/org/springframework/spring-instrument/6.1.7/spring-instrument-6.1.7.jar</code>Feel free to experiment with the above configurations.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.