So let me show by example how to use Spring Framework together with JSR 330: Dependency Injection for Java. First, we need to reference JSR 330 annotations, f.e. from atinjet project. As always, I'm using Apache Maven 2/3 so here is my POM file:
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 | < 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 >com.example</ groupid > < artifactid >cdi-spring</ artifactid > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < properties > < project.build.sourceencoding >UTF-8</ project.build.sourceencoding > < spring.version >3.0.5.RELEASE</ spring.version > </ properties > < dependencies > < dependency > < groupid >javax.inject</ groupid > < artifactid >javax.inject</ artifactid > < version >1</ version > </ dependency > < dependency > < groupid >org.springframework</ groupid > < artifactid >spring-context</ artifactid > < version >${spring.version}</ version > </ dependency > </ dependencies > < repositories > < repository > < id >nexus.xwiki.org</ id > </ repository > </ repositories > </ project > |
1 2 3 4 5 6 7 8 9 10 11 | <!--xml version="1.0" encoding="UTF-8"?--> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemalocation=" < context:component-scan base-package = "com.spring.cdi" ></ context:component-scan > < context:annotation-config ></ context:annotation-config > </ beans > |
1 2 3 4 5 6 7 8 9 10 | package com.spring.cdi; import javax.inject.Named; @Named public class OneBean { public void doWork() { System.out.println( "Work is done" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.spring.cdi; import javax.inject.Inject; import javax.inject.Named; @Named public class AnotherBean { @Inject private OneBean oneBean; public void doWork() { oneBean.doWork(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.cdi.AnotherBean; public class Starter { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); AnotherBean bean = context.getBean( AnotherBean. class ); bean.doWork(); } } |
Jun 11, 2011 1:08:03 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2626d4f1: startup date [Sat Jun 11 13:08:03 EDT 2011]; root of context hierarchy Jun 11, 2011 1:08:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] Jun 11, 2011 1:08:03 PM org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning Jun 11, 2011 1:08:04 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorSo those beans could be easily used without modifications by any Java framework which supports JSR 330 (f.e., JBoss Weld). Cool stuff.INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring Jun 11, 2011 1:08:04 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4979935d: defining beans [anotherBean,oneBean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy Work is done