Let me show how it works by creating simple test project. Let's start with Maven's project descriptor (pom.xml):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelversion >4.0.0</ modelversion > < groupid >spring-configurable</ groupid > < artifactid >spring-configurable</ artifactid > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < name >spring-configurable</ name > < properties > < project.build.sourceencoding >UTF-8</ project.build.sourceencoding > < spring.framework.version >3.0.5.RELEASE</ spring.framework.version > </ properties > < dependencies > < dependency > < groupid >org.aspectj</ groupid > < artifactid >aspectjweaver</ artifactid > < version >1.6.5</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-beans</ artifactid > < version >${spring.framework.version}</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-aspects</ artifactid > < version >${spring.framework.version}</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-core</ artifactid > < version >${spring.framework.version}</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-context</ artifactid > < version >${spring.framework.version}</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-jdbc</ artifactid > < version >${spring.framework.version}</ version > </ dependency > </ dependencies > </ project > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!--xml version="1.0" encoding="UTF-8"?--> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation=" < context:annotation-config /> < context:spring-configured /> < context:load-time-weaver /> < context:component-scan base-package = "com.test.configurable" /> < bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" /> </ beans > |
instantiated outside of the Spring bean factory (typically classes annotated with the @Configurable annotation)..." and <context:load-time-weaver /> turns on runtime weaving.
Respective Java code which uses power of @Configurable annotation is located into Starter.java, which itself is placed inside src/main/java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.test.configurable; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.Assert; public class Starter { @Configurable private static class Inner { @Autowired private DataSource dataSource; } public static void main(String[] args) { final ApplicationContext context = new ClassPathXmlApplicationContext( "/applicationContext.xml" ); final DataSource dataSource = context.getBean( DataSource. class ); Assert.notNull( dataSource ); Assert.notNull( new Inner().dataSource ); } } |