SpringSource has published release candidate 2 of the upcoming 3.0 release of their Spring Framework. New Feature is the compliance with JSR-330 (“Dependency Injection for Java”). The JSR was developed together by Google (for their Guice Framework and SpringSource (for their Spring Framework) and is finally approved since 14.10.2009.
A little example shows how to develop services with interfaces and implementations without dependencies to Spring Framework or Google Guice:
The Maven pom.xml
4.0.0de.schaeftlein.devjsr330-samplejarjsr330-sample0.0.1-SNAPSHOTSample App with Spring 3.0 RC 2 and JSR3303.0.0.RC2org.springframeworkspring-core${springVersion}org.springframeworkspring-beans${springVersion}org.springframeworkspring-context${springVersion}org.springframeworkspring-asm${springVersion}org.springframeworkspring-expression${springVersion}junitjunit4.7testjavax.injectjavax.inject1jsr330-samplesrc/main/java${basedir}/src/test/javasrc/main/resources**/.svn**/.svn/****/_svn_svn**/_svn/**src/test/resources**/.svn**/.svn/****/_svn_svn**/_svn/**org.apache.maven.pluginsmaven-compiler-plugin1.61.6org.apache.maven.pluginsmaven-eclipse-plugintruetrue_svn.svnorg.apache.maven.pluginsmaven-jar-plugintrueorg.apache.maven.pluginsmaven-source-pluginattach-sourcesverifyjar
package de.schaeftlein.dev.spring;
public interface SecureUtil
{
void compareEncryption(String input);
}
a implementation for our service
package de.schaeftlein.dev.spring;
import javax.inject.Inject;
import javax.inject.Named;
@Named("SecureUtil")
public class SecureUtilImpl implements SecureUtil
{
@Inject // automatically set by DI framework
@Named("secure") // get the namend bean
private Encryption secureEncryption;
@Inject // automatically set by DI framework
@Named("URLEncoderEncyrption") // get the bean by its classname
private Encryption unsecureEncryption;
public void compareEncryption(String input){
String encodedSecure = secureEncryption.encode(input);
System.out.println("Secure encoded: "+encodedSecure);
String encodeUnsecure = unsecureEncryption.encode(input);
System.out.println("Unsecure encoded: "+encodeUnsecure);
System.out.println("Secure decoded: "+secureEncryption.decode(encodedSecure));
System.out.println("Unsecure decoded: "+unsecureEncryption.decode(encodeUnsecure));
}
}
finally a main class for testing
package de.schaeftlein.dev.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main
{
/**
* main method
*/
public static void main(String[] args)
{
ApplicationContext ctx = new AnnotationConfigApplicationContext(Main.class.getPackage().getName()); // new way to get Application context without applicationContext.xml available
SecureUtil util = ctx.getBean("SecureUtil", SecureUtil.class); // get bean with new generics method
util.compareEncryption("an sample input string 1234567890");
}
}
output of our Main class
19.11.2009 18:56:03 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@c1b531: startup date [Thu Nov 19 18:56:03 CET 2009]; root of context hierarchy
19.11.2009 18:56:03 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
19.11.2009 18:56:03 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a83b8a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,secure,SecureUtil,URLEncoderEncyrption]; root of factory hierarchy
Secure encoded: QDQvd4uK14YNUQ4uoqhqsZEMDDxUqJAMyisvZr2wsA2GyMC5tSEIiw==
Unsecure encoded: an+sample+input+string+1234567890
Secure decoded: an sample input string 1234567890
Unsecure decoded: an sample input string 1234567890