VS.
And the Winner is …
Java User Group Switzerland, Bern, 23.05.2012 Simon Martinelli / @simas_ch
simas GmbH - Moosentli 7 - 3235 Erlach - 032 544 88 88 - sm@simas.ch - www.simas.ch
VS. And the Winner is Java User Group Switzerland, Bern, 23.05.2012 - - PowerPoint PPT Presentation
VS. And the Winner is Java User Group Switzerland, Bern, 23.05.2012 Simon Martinelli / @simas_ch simas GmbH - Moosentli 7 - 3235 Erlach - 032 544 88 88 - sm@simas.ch - www.simas.ch Agenda Introduction Dependeny Injection
Java User Group Switzerland, Bern, 23.05.2012 Simon Martinelli / @simas_ch
simas GmbH - Moosentli 7 - 3235 Erlach - 032 544 88 88 - sm@simas.ch - www.simas.ch
1995 2012 2001 COBOL/HOST J2EE / Java EE 2010 2004 Spring 1995 2012 2001 JCA 2010 2004 AD DS 2007 Java Persistence API DB / DWH AD JEE
SBB ACS simas GmbH
Berner Fachhochschule Software Schule Schweiz Medical Technology Center
AD JEE = Architektur und Design Java EE / AD DS = Architektur und Design verteilter Systeme
1999 2011 2009 2003 1.2
Servlets JSP EJB JMS
2001 1.3
CMP JCA
1.4
Web Services Management Deployment Async JCA
2005 5
Ease of Dev Annotations JSF EJB 3 JPA WS
J2EE / Java EE Spring Framework 1.0
DI
2004 2006 6
Profiles Pruning Extensibility EJB Lite REST CDI
1.2
Java 5
2.0 2007 2.5
Annotations JEE5
3.0
JSR-330 REST
0.9
JSR 330: Dependency Injection for Java JSR 299: Contexts and Dependency Injection for the Java EE platform JSR 318: Enterprise JavaBeans 3.1
public class MyService { } public class AnotherService { @Inject private MyService myService; } The Bean The Injection Point Could also be a constructor or a setter
@Asynchronous public class AsyncService implements MyService { ... } public class AnotherService { @Inject @Synchronous private MyService myService; } @Synchronous public class SyncService implements MyService { ... } Two Implementations of the same interface Two implementations of the same interface Injection must be qualified
public class MyService { public MyService(A a) { // ... } } public class MyServiceProducer { @Produces public MyService createMyService(A a) { return new MyService(a); } } public class AnotherService { @Inject private MyService myService; } Producer Method to create instance A will be injected!
@Interceptor @Transactional public class TransactionInterceptor { @AroundInvoke public Object manageTransaction(InvocationContext ctx) { // ... } } @InterceptorBinding public @interface Transactional { } public class MyService { @Transactional public void foo() { // ... } } The Interceptor The Annotation A Transactional Method
public class MyService { @Inject private Event<LoggedInEvent> loggedInEvent; public void login(String username, String password) { loggedInEvent.fire(new LoggedInEvent(username)); // ... } } public class LoggedInObserver { public void afterLogin(@Observers LoggedInEvent event) { // ... } } The Event Object Fire the Event The Listener Called Synchronous!
@Stateless public class LoggedInObserver { @Asynchronous public void afterLogin(@Observers LoggedInEvent event) { // ... } } The Listener as EJB Called Asynchronous!
Source: http://planet.jboss.org/post/seam_next_announcement
<beans> <bean id="myService" class="service.MyService"/> <bean id="anotherService" class="service.AnotherService"> <property name="myService" ref="myService"/> </bean> </beans>
public class AnotherService { private MyService myService; public void setMyService(MyService myService) { this.myService = myService; } }
The Injection Point Could also be a constructor
public class AnotherService { @Autowired private MyService myService; }
public class AnotherService { @Inject private MyService myService; }
<beans> <context:annotation-config/> </beans> The Injection Point Could also be a constructor or a setter JSR-330 Spring Annotations
public class AnotherService { @Resource private MyService myService; }
JSR-250
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } <beans> <bean id="myService" class="service.MyService"/> </beans>
Application Session Conversation Request Java EE Spring EJB General Stateful = Prototype Stateless x Singleton = Singleton CDI Web RequestScoped = Request ConversationScoped (=) Spring Web Flow SessionScoped = Session ApplicationScoped = Singleton Dependant x x Global Session (only for Portlets)
Spring Framework Spring Security Spring Integration Spring Batch Spring Data Spring Web Flow Spring Web Services Spring Mobile Spring Social Spring Andorid Spring Roo Spring BlazeDS Integration Spring AMQP …
@Stateless public class MyService { public void foo() { ... } } @Service public class MyService { @Transactional public void foo() { ... } } Defaults to @TransactionAttribute(REQUIRED)
Java EE Applikation Spring Applikation Java EE App Server Tomcat TomEE Spring Framework Java EE App Server
CDI Apache OpenWebBeans EJB Apache OpenEJB Javamail Apache Geronimo JavaMail JPA Apache OpenJPA JSF Apache MyFaces JTA Apache Geronimo Transaction Connector Apache Geronimo Connector JMS Apache ActiveMQ Web Services Apache CXF
TomEE TomEE Plus
Source: Antonio Goncalves
Source: Antonio Goncalves
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class MyTest { @Autowired private MyService myService; } @RunWith(Arquillian.class) public class MyTest { @EJB private MyService myService; }
www.jboss.org/arquillian
+
Topic Java EE Spring Framework Framework Specification based Ecosystem Defaults Conventions over Configuration No Default Behavior Dependency Injection CDI Spring Container JSR 330 but not CDI! Transactions EJB AOP / Annotations Web Framework JSF Spring Web MVC AOP Interceptors XML AspectJ Integration Testing Arquillian (non standard) Spring Test Framework Deployment Part of the Platform Part of the Application Independency Several Vendors VMware