Thursday, October 24, 2013

I am using Spring 3.1 and its configuration via @ Configuration. This you can read more about the S


This post shows how the Dependency Injection (DI) can be used by CDI, Guice and Spring. Depencency Injection described including Robert cage Article Spring DI frameworks. The example in this post is heavily influenced by an article I read on DZone.
Spring and Guice are two of the most common frameworks for DI. These frameworks can be used in stand-alone, web and enterprise applications. CDI is a standardized way for DI that comes with Java EE 6 and can be suspended only be used in an enterprise environment. Spring, however, offers support for some of the features defined by the CDI.
The example I use (from DZone) is an ATM or ATM - Automated Teller Machine. On this ATM machine, you can make withdrawals or deposits (with draw, deposit). I have provided an implementation border lineups of this ATM for respective DI technology. Furthermore, injected an implementation of AtmTransport to each ATM.
The interface of the ATM is as follows: package se.cygni.stacktrace.di; import java.math.BigDecimal; public interface atm {void deposit (BigDecimal bd); void with draw (BigDecimal bd);}
I am using Spring 3.1 and its configuration via @ Configuration. This you can read more about the SpringSource blog but through border lineups this eliminates I totally need XML. First I set up the component scan using the @ Component Scan. This completely replaces XML: one component-scan. package se.cygni.stacktrace.di.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import se.cygni.stacktrace.di.spring.SpringAtm; border lineups / ** Spring 3.1 configuration with component scanning * / @ @ Configuration Component Scan (base package classes = SpringAtm.class) public class SpringConfig {/ / No impl. needed - only annotations are used}
As you can see, this is done magic with the help of @ Component Scan (base package classes = SpringAtm.class). This tells Spring to the package where the class is SpringAtm be scanned. You can replace this with regular string values containing package name but by specifying a class above, the configuration typesafe border lineups and manage refaktoreringar.
The implementation of the ATM looks like this: package border lineups se.cygni.stacktrace.di.spring; import java.math.BigDecimal; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import see . cygni.stacktrace.di.Atm; import se.cygni.stacktrace.di.AtmTransport: @ Component public class SpringAtm implements ATM {private final AtmTransport transportation; @ Auto Wired public SpringAtm (final AtmTransport transport) {this.transport = transport;} private byte [] data, create () {return "test border lineups data". getBytes ();} public void deposit (final BigDecimal bd) {System.out.println ("Spring: deposit called"); transport.communicateWithBank (create data ()); with} public void draw (final BigDecimal bd) {System.out.println ("Spring: with drawings called"); transport.communicateWithBank (create data ());}}
@ Component annotation is used to mark the class as a component border lineups to be created via a Spring context. @ Auto Wired used to inject a AtmTransport via constructor. Transport object, which is also annotated with @ Component looks like this: package se.cygni.stacktrace.di.spring; import org.springframework.stereotype.Component; import se.cygni.stacktrace.di.AtmTransport: @ Component public class Transport implements Spring AtmTransport {public void communicateWithBank (final byte [] data packet) {System.out.println ("Spring transportation - yey!");}}
Tada! That is all that is needed to "bootstrap" the application via a AnnotationConfigApplicationContext as shown below: final Atm springAtm = new AnnotationConfigApplicationContext (SpringConfig.class). GetBean (Atm.class); springAtm.deposit (new BigDecimal (3000));
To achieve the above via Guice can be done as follows. First we created an implementation of ATM and AtmTransport with Guice-annotations. Then a module used to configure the application (also without XML). Our Module units GuiceConfig.
Cash dispenser looks like this: package se.cygni.stacktrace.di.guice; import java.math.BigDecimal; import se.cygni.stacktrace.di.Atm; border lineups import se.cygni.stacktrace.di.AtmTransport; import com.google . inject.Inject; public class GuiceAtm implements ATM {private final AtmTransport transportation; @ Inject public GuiceAtm (final AtmTransport transport) {this.transport = transport;} private byte [] data, create () {return "test data". getBytes (); } public void deposit (final BigDecimal bd) {System.out.println ("Guice: deposit called"); transport.communicateWithBank (create data ());} public void with draw (final BigDecimal bd) {System.out.println (" Guice: with drawings called "); transport.communicateWithBank (creat

No comments:

Post a Comment