spring framework 2 - jugs › protokolle › 2008-03-03 › 02-spring-2.5.pdf · spring and osgi...

30
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring Framework 2.5 In the World of Java 6 and Java EE 5 Jürgen Höller VP & Distinguished Engineer SpringSource

Upload: others

Post on 27-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Spring Framework 2.5In the World of Java 6 and Java EE 5

Jürgen Höller

VP & Distinguished Engineer

SpringSource

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2

Agenda

• Part 1: Runtime Platforms

– A Brief History of Spring

– Java 6 & Java EE 5 Support

– WebSphere, AspectJ, OSGi

• Part 2: Configuration Annotations

– JSR-250: @Resource etc

– Spring's @Autowired

– Annotated MVC Controllers

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Part 1:Runtime Platforms

From Java EE to OSGi

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4

Spring & Runtime Platforms

• Spring's central value proposition

– providing a unified programming modelfor business components

– providing consistent configuration acrossdifferent runtime environments

• Foundation: plain Java objects

– transparently integrating with environmentservices through Spring's generic container

– rich annotation support on Java 5

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5

Java 6 (JDK 1.6) Support

• Spring 2.5 explicitly supports JDK 1.6

– while remaining backwards compatible withJDK 1.5 and even JDK 1.4

• no support for JDK 1.3 (EOL'ed) anymore

• Support for new APIs in JDK 1.6

– JDBC 4.0

• native connections, LOB handling

– JMX MXBeans

• exported through Spring's MBeanExporter

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6

Java EE 5 Support

• Spring 2.5 fully supports Java EE 5

– while remaining backwards compatible withJ2EE 1.4 and even J2EE 1.3

• e.g. WebLogic 8.1+, WebSphere 5.1+

• Support for JPA 1.0 and EJB 3.0

– JPA support available since Spring 2.0

• Support for Servlet 2.5, JSP 2.1, JSF 1.2

• Support for JTA 1.1, JAX-WS 2.0

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7

"jms" XML Namespace

<jms:listener-container connection-factory="myConnectionFactory" transaction-manager="myTransactionManager">

<jms:listener destination="myQueue" ref="myListener"/>

<jms:listener destination="myQueue2" ref="myListener2"/>

</jms:listener-container>

<jms:jca-listener-container resource-adapter="myResourceAdapter" transaction-manager="myTransactionManager">

<jms:listener destination="myQueue" ref="myListener"/>

<jms:listener destination="myQueue2" ref="myListener2"/>

</jms:jca-listener-container>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8

WebSphere Integration

• Support for WebSphere-specifictransaction management API

– including transaction suspension!

• avoiding the use of the raw JTATransactionManager on WebSphere

– on WebSphere 6.0.2.19+ and 6.1.9+

• officially supported by IBM!

• WebSphereUowTransactionManager

– instead of plain JtaTransactionManager

– <tx:jta-transaction-manager/>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9

Support for AspectJ LTW

• AspectJ Load-Time Weaving

– transforming byte code of application classes

– through Spring's LoadTimeWeaver abstraction

• Driven by AspectJ META-INF/aop.xml files

– standard AspectJ deployment descriptor

• Requires platform support

– generic Spring VM agent to be specified on startup

– or adapters for specific servers

• WebLogic 10, OC4J, GlassFish, Tomcat, Resin

• <context:load-time-weaver/>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10

Proxy-based or Weaving?

• Choice: proxies vs. weaving– proxy-based AOP shows 'isolated' effect

(specific instances)• simple; good enough for many purposes

– weaving-based AOP shows 'broad' effect(at the class level)• more advanced, but extremely powerful

• Load-Time Weaving only available inspecific runtime environments– alternative: use compile-time weaving

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11

Spring and OSGi

• Dynamic module system

– central notion: a 'bundle'

• a bundle is a versioned deployment unit

– bundles can be started, stopped, updated

• Spring Framework 2.5 jars arecompliant OSGi bundles

– can be used in an OSGi environment as-is

• Spring-OSGi platform integration

– in Spring Dynamic Modules project

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12

Spring on OSGi vs.Spring on J2EE

• Similar programming model

– services implemented as Spring beans

– no direct dependencies on environment

• Different deployment environments

– OSGi: bundle jars, service registry

– J2EE: WAR / EAR files, JNDI environment

• OSGi and J2EE are alternative runtimes

– hard to mix – an either/or choice

– Spring as the common ground!

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Part 2:Configuration Annotations

From JSR-250 to @Controller

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14

JSR-250 CommonAnnotations

• Spring 2.5 supports JSR-250 CommonAnnotations for Spring-managed beans

– @PostConstruct, @PreDestroy

• annotations for init & destroy method

– @Resource

• annotation-driven injection of beans

• on fields and/or setter methods

• Same annotations as supported byServlet 2.5 and JSF 1.2 managed beans

– seamless overall component model

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15

JSR-250 Annotation Example

public class MyBean implements MyServiceInterface {

private DataSource dataSource;

@Resource(name="myDataSource")

public void setDataSource(DataSource dataSource) {

this.dataSource = dataSource;

}

@PostConstruct

public void initialize() { ... }

@PreDestroy

public void shutdown() { ... }

}

// No specific configuration necessary!

<bean id="myService" class="mypackage.MyBean"/>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16

Further Java EE 5Annotations

• Java EE 5 includes further, more specificannotations– @WebServiceRef / @EJB

• injecting a JAX-WS / EJB 3 service proxy

– @TransactionAttribute• EJB 3 transaction demarcation

– @PersistenceContext /@PersistenceUnit• JPA resource injection

• supported since Spring 2.0 already!

• All consistently supported in Spring 2.5

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17

Autowiring Annotation

• New Spring @Autowired annotation

– for annotation-driven autowiring by type

– on fields, config methods, constructors

• Motivation

– autowiring by name is often too simplistic

– autowiring by type is often too extensive

– sweet spot inbetween:specific autowiring by type

• driven by explicit annotations

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18

Autowiring AnnotationExamples

public class MyBean implements MyServiceInterface {

@Autowired

public MyBean(DataSource dataSource, ServiceA a, ServiceB b) { ... }

}

public class MyBean implements MyServiceInterface {

@Autowired

public void setDataSource(DataSource ds) { ... }

@Autowired

public void injectServices(ServiceA a, ServiceB b) { ... }

}

// No specific configuration necessary!

<bean id="myService" class="mypackage.MyBean"/>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19

Autowiring Annotationwith Qualifiers

public class MyBean implements MyServiceInterface {

@Autowired @Qualifier("orderDataSource")

private DataSource dataSource;

@Autowired

public void injectServices(@Emea OrderService service) { ... }

}

<bean id="myService" class="mypackage.MyBean"/>

// Qualified through literal value (or through bean name)

<bean class="org.apache.commons.dbcp.BasicDataSource">

<qualifier value="orderDataSource"/>

</bean>

// Qualified through custom @Emea annotation.

<bean class="mypackage.EmeaOrderServiceImpl">

<qualifier type="Emea"/>

</bean>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20

Example for AutodetectableComponent

@Component("myService")

public class MyBean implements MyServiceInterface {

@Resource(name="myDataSource")

private DataSource dataSource;

@Autowired

public void inject(ServiceA a, ServiceB b) {...}

@PostConstruct

public void initialize() { ... }

@PreDestroy

public void shutdown() { ... }

}

// Not even a plain XML <bean> tag necessary!

<context:component-scan base-package="mypackage"/>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21

@Configurable with AspectJ

<!-- configures AspectJ bean configurer aspect --><!-- spring-aspects.jar required on the classpath --><!-- ClassLoader needs to be weaving-capable! -->

<context:load-time-weaver aspectj-weaving="on"/>

<context:spring-configured/>

@Configurable

public class MyDomainObject {

@Autowired

private MyService myService;

}

MyDomainObject obj = new MyDomainObject();

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22

@Transactional with AspectJ

<!-- configures AspectJ transaction aspect --><!-- spring-aspects.jar required on the classpath --><!-- ClassLoader needs to be weaving-capable! -->

<context:load-time-weaver aspectj-weaving="on"/>

<tx:annotation-driven mode="aspectj" transaction-manager="txManager"/>

<bean id="txManager" class="org.springframework.transaction.jta. JtaTransactionManager"/>

<!– weaved: allowed to use @Transactional on any methods -->

<bean id="accountService“ class="demo.AccountService"/>

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23

Annotated MVC Controllers

• Spring 2.5 introduces support forannotated MVC controllers

– @Controller

– @RequestMapping

– @RequestParam

– @ModelAttribute

• Java 5 variant of the traditional SpringMultiActionController approach

– also offering form handling capabilities

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24

Example for AnnotatedServlet Controller

@Controller

public class MyController {

private final MyService myService;

@Autowired

public MyController(MyService myService) {

this.myService = myService;

}

@RequestMapping("/myBooks")

public String showBooks(ModelMap model) {

model.addAttribute("books", myService.findAllBooks());

return "booksEdit";

}

@RequestMapping("/removeBook")

public String removeBook(@RequestParam("book") String bookId) {

myService.deleteBook(bookId);

return "redirect:myBooks";

}

}

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25

Example for AnnotatedPortlet Controller

@Controller

@RequestMapping("EDIT")

public class MyPortletController {

private final MyService myService;

@Autowired

public MyPortletController(MyService myService) {

this.myService = myService;

}

@RequestMapping(params = "action=list")

public String showBooks(ModelMap model) {

model.addAttribute("books", myService.findAllBooks());

return "booksEdit";

}

@RequestMapping(params = "action=delete")

public void removeBook(@RequestParam("book") String bookId, ActionResponse response) {

myService.deleteBook(bookId);

response.setRenderParameter("action", "list");

}

}

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26

Test Context Framework

• Spring 2.5 introduces a revised,annotation-based test framework

– @ContextConfiguration

– @TransactionConfiguration

• Supports JUnit 4.4 and TestNG

– as well as JUnit 3.8

• Supersedes Spring's traditionalJUnit 3.8 test base class hierarchy

– AbstractSpringContextTests etc

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27

Example for AnnotatedTest Class

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration

// defaults to MyTests-context.xml in same package

public class MyTests {

@Autowired

private MyService myService;

@Test

public void myTest() {...}

@Test

@Transactional

public void myOtherTest() { ... }

}

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28

Recommendations

• Option 1: XML-based bean definitions,selectively complemented with annotations

– <bean id="…" class="…"> for each component

– selective @Autowired / @PostConstruct etc usage

– <property> for non-trivial dependencies

– The typical service layer scenario!

• Option 2: Fully annotated components,detected through classpath scanning

– @Component / @Controller / etc stereotypes

– @Autowired used for all dependencies

– Viable option for web controllers, tests, etc

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29

Tradeoffs

• Externalized configuration

– XML <bean> as typical format for externalization

– contract for passing configuration in throughconstructor arguments & bean properties

– Configuration can change without recompilation!

• Embedded configuration

– configuration "hints" expressed through @Autowired(etc) annotations in component classes

– can be overridden through explicit <property> tags,if designed for it

– Changes may require recompilation!

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30

Spring 2.5 Summary

• Fully embraces Java 5 & Java EE 5– as well as Java 6

• Closely integrates AspectJ– explicit support for load-time weaving

• Embraces config annotations– annotation-driven autowiring

– annotation-driven component detection

– annotation-driven MVC controllers

– annotation-driven test context framework