Spring 3 - An Introduction

66

description

Since late 2009 there is Spring 3 published. Some things are new, something keep and something was removed.Thos talk discuss the changes of the 3rd edition of Spring and introduce Spring Roo, Grails and the SpringSource Toolsuite.

Transcript of Spring 3 - An Introduction

Page 1: Spring 3 - An Introduction
Page 2: Spring 3 - An Introduction

Spezialist für modellbasierte Entwicklungsverfahren

Gründung im Jahr 2003

Niederlassungen in Deutschland, Frankreich, Schweiz und Kanada

140 Mitarbeiter

Strategisches Mitglied der Eclipse Foundation

Intensive Verzahnung im Bereich der Forschung

Mitglied von ARTEMISIA

Embedded Software Development

Enterprise Application Development

Page 3: Spring 3 - An Introduction
Page 4: Spring 3 - An Introduction

Von und mit Thorsten Kamann

22.02.2010

Page 5: Spring 3 - An Introduction
Page 6: Spring 3 - An Introduction
Page 7: Spring 3 - An Introduction

jUnit 3 Klassen

Commons Attributes

Struts 1.x Support

-

Page 8: Spring 3 - An Introduction

100% API

95% Extension

Points

Spring 3

Page 9: Spring 3 - An Introduction

Modules OSGi

Maven Enterprise Repository

Page 10: Spring 3 - An Introduction

#{...} @Value

XML ExpressionParser

Expression Language

Page 11: Spring 3 - An Introduction

<bean class=“MyDatabase"> !!<property name="databaseName" !! value="#{systemProperties.databaseName}"/> !!<property name="keyGenerator" !! value="#{strategyBean.databaseKeyGenerator}"/> !

</bean> !

Page 12: Spring 3 - An Introduction

@Repository !public class MyDatabase { !

!@Value("#{systemProperties.databaseName}") !!public void setDatabaseName(String dbName) {…} !

!@Value("#{strategyBean.databaseKeyGenerator}") !!public void setKeyGenerator(KeyGenerator kg) {…} !

} !

Page 13: Spring 3 - An Introduction

Database db = new MyDataBase(„myDb“, „uid“, „pwd“);!

ExpressionParser parser = new SpelExpressionParser();!Expression exp = parser.parseExpression("name");!

EvaluationContext context = new ! ! !! ! ! !StandardEvaluationContext();!

context.setRootObject(db);!

String name = (String) exp.getValue(context);!

Page 14: Spring 3 - An Introduction

Database db = new MyDataBase(„myDb“, „uid“, „pwd“);!

ExpressionParser parser = new SpelExpressionParser();!Expression exp = parser.parseExpression("name");!

String name = (String) exp.getValue(db);!

Page 15: Spring 3 - An Introduction

@Configuration @Bean @DependsOn

@Primary @Lazy @Import

@ImportResource @Value

Page 16: Spring 3 - An Introduction

@Configuration!public class AppConfig {! !@Value("#{jdbcProperties.url}") !

!private String jdbcUrl;! !!

!@Value("#{jdbcProperties.username}") !!private String username;!

!@Value("#{jdbcProperties.password}") !!private String password;!

}!

Page 17: Spring 3 - An Introduction

@Configuration!public class AppConfig {! !@Bean! !public FooService fooService() {! return new FooServiceImpl(fooRepository());! !}!

!@Bean! !public DataSource dataSource() { ! return new DriverManagerDataSource(!

! !jdbcUrl, username, password);! !}!

}!

Page 18: Spring 3 - An Introduction

@Configuration!public class AppConfig {! !@Bean! !public FooRepository fooRepository() {! return new HibernateFooRepository! ! !

! ! ! ! !(sessionFactory());! }!

@Bean! public SessionFactory sessionFactory() {!

!...! }!}!

Page 19: Spring 3 - An Introduction

<context:component-scan !!base-package="org.example.config"/>

<util:properties id="jdbcProperties" ! !location="classpath:jdbc.properties"/>!

Page 20: Spring 3 - An Introduction

public static void main(String[] args) {! ApplicationContext ctx = !

!new AnnotationConfigApplicationContext(!! ! ! ! ! !AppConfig.class);!

FooService fooService = ctx.getBean(!! ! ! ! ! !FooService.class);!

fooService.doStuff();!}!

Page 21: Spring 3 - An Introduction

Marshalling

JAXB Castor

Unmarshalling

JiBX Xstream XMLBeans

Page 22: Spring 3 - An Introduction

<oxm:jaxb2-marshaller !!id="marshaller" !!contextPath=“my.packages.schema"/>!

<oxm:jaxb2-marshaller id="marshaller">! <oxm:class-to-be-bound name=“Customer"/>! <oxm:class-to-be-bound name=„Address"/>! ...!</oxm:jaxb2-marshaller>!

Page 23: Spring 3 - An Introduction

<beans>! <bean id="castorMarshaller" !

!class="org.springframework.oxm.castor.CastorMarshaller" >! <property name="mappingLocation"

! !value="classpath:mapping.xml" />! </bean>!</beans>

Page 24: Spring 3 - An Introduction

<oxm:xmlbeans-marshaller !!id="marshaller“!!options=„XMLOptionsFactoryBean“/>!

Page 25: Spring 3 - An Introduction

<oxm:jibx-marshaller !!id="marshaller" !!target-class=“mypackage.Customer"/>!

Page 26: Spring 3 - An Introduction

<beans>!

<bean id="xstreamMarshaller" !class="org.springframework.oxm.xstream.XStreamMarshaller">!

<property name="aliases">! <props>! <prop key=“Customer">mypackage.Customer</prop>! </props>! !</property>! </bean>! ...!

</beans>!

Page 27: Spring 3 - An Introduction

Spring MVC

@Controller @RequestMapping @PathVariable

Page 28: Spring 3 - An Introduction

@Controller

The C of MVC

<context:component-scan/>

Mapping to an URI (optional)

Page 29: Spring 3 - An Introduction

@RequestMapping

Mapping a Controller or Methods to an URI

URI-Pattern

Mapping to a HTTP-Method

Works with @PathVariable

Page 30: Spring 3 - An Introduction

@Controller!@RequestMapping(„/customer“)!public class CustomerController{!

!@RequestMapping(method=RequestMethod.GET)!!public List<Customer> list(){!! !return customerList;!!}!

}!

Page 31: Spring 3 - An Introduction

@Controller!@RequestMapping(„/customer“)!public class CustomerController{!

!@RequestMapping(value=„/list“,!! ! ! ! !method=RequestMethod.GET)!!public List<Customer> list(){!! !return customerList;!!}!

}!

Page 32: Spring 3 - An Introduction

@Controller!@RequestMapping(„/customer“)!public class CustomerController{!

!@RequestMapping(value=„/show/{customerId}“,!! ! ! ! !method=RequestMethod.GET)!!public Customer show(!! @PathVariable(„customerId“) long customerId){!! !return customer;!!}!

}!

Page 33: Spring 3 - An Introduction

@Controller!@RequestMapping(„/customer“)!public class CustomerController{!

!@RequestMapping(!! !value=„/show/{customerId}/edit/{addressId}“,!! ! ! ! !method=RequestMethod.GET)!!public String editAddressDetails(!! @PathVariable(„customerId“) long customerId,!! @PathVariable(„addressId“) long addressId){!! !return „redirect:...“;!!}!

}!

Page 34: Spring 3 - An Introduction

RestTemplate

Delete Get Head Options Post Put

Page 35: Spring 3 - An Introduction

public void displayHeaderInfo(! @RequestHeader("Accept-Encoding") String encoding,! @RequestHeader("Keep-Alive") long keepAlive) {!

!...!

}!

Host localhost:8080!Accept text/html, application/xml;q=0.9!Accept-Language fr,en-gb;q=0.7,en;q=0.3!Accept-Encoding gzip,deflate!Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7!Keep-Alive 300!

Page 36: Spring 3 - An Introduction

public class VistorForm(!!@NotNull!

!@Size(max=40)!!private String name;!

!@Min(18)!!private int age;!

}!

<bean id="validator" !!class=“...LocalValidatorFactoryBean" />!

JSR-303

Page 37: Spring 3 - An Introduction

HSQL H2 Derby ...

Page 38: Spring 3 - An Introduction

<jdbc:embedded-database id="dataSource">! <jdbc:script location="classpath:schema.sql"/>! <jdbc:script location="classpath:test-data.sql"/>!</jdbc:embedded-database>!

EmbeddedDatabaseBuilder builder = new ! !! ! ! !EmbeddedDatabaseBuilder();!

EmbeddedDatabase db = builder.setType(H2)!! ! ! !.addScript(“schema.sql")!! ! ! !.addScript(„test-data.sql")!! ! ! !.build();!

//Do somethings: db extends DataSource!db.shutdown();!

Page 39: Spring 3 - An Introduction

public class DataBaseTest {! private EmbeddedDatabase db;!

@Before! public void setUp() {!

!db = new EmbeddedDatabaseBuilder()!! !.addDefaultScripts().build();! !!

}!

@Test! public void testDataAccess() {! JdbcTemplate template = new JdbcTemplate(db);! template.query(...);! }!

@After! public void tearDown() {! db.shutdown();! }!}!

Page 40: Spring 3 - An Introduction

@Async (out of EJB 3.1)

JSR-303

JSF 2.0

JPA 2

Page 41: Spring 3 - An Introduction
Page 42: Spring 3 - An Introduction

Spri

ng A

pplic

atio

n T

ools

•  Spring project, bean and XML file wizards

•  Graphical Spring configuration editor

•  Spring 3.0 support including @Configuration and @Bean styles

•  Spring Web Flow and Spring Batch visual development tools

•  Spring Roo project wizard and development shell

•  Spring Application blue prints and best practice validation

OSG

i •  OSGi bundle overview and visual dependency graph

•  Classpath management based on OSGi meta data

•  Automatic generation of manifest dependency meta data

•  SpringSource Enterprise Bundle Repository browser

•  Manifest file validation and best practice recommendations

Fle

xibl

e D

eplo

ymen

ts

•  Support for all the most common Java EE application servers

•  Advanced support for SpringSource dm Server

•  Advanced support for SpringSource tc Server

•  Cloud Foundry targeting for dm Server and tc Server

•  VMware Lab Manager and Workstation integration and deployment

Page 43: Spring 3 - An Introduction
Page 44: Spring 3 - An Introduction
Page 45: Spring 3 - An Introduction
Page 46: Spring 3 - An Introduction
Page 47: Spring 3 - An Introduction
Page 48: Spring 3 - An Introduction

Grails for Java

Spring Best Practices

AOP-driven

Test-driven Nice Console

Page 49: Spring 3 - An Introduction

Command Line Shell

@Roo* Annotations

(Source-Level)

AspectJ Intertype

Declarations Metadata RoundTrip

Page 50: Spring 3 - An Introduction
Page 51: Spring 3 - An Introduction
Page 52: Spring 3 - An Introduction
Page 53: Spring 3 - An Introduction

roo> project --topLevelPackage com.tenminutes roo> persistence setup --provider HIBERNATE

--database HYPERSONIC_IN_MEMORY roo> entity --class ~.Timer --testAutomatically roo> field string --fieldName message --notNull roo> controller all --package ~.web roo> selenium test --controller ~.web.TimerController roo> perform tests roo> perform package roo> perform eclipse roo> quit $ mvn tomcat:run

Page 54: Spring 3 - An Introduction
Page 55: Spring 3 - An Introduction
Page 56: Spring 3 - An Introduction

Roo for Groovy

Best Practices

Groovy-driven

Test-driven

Nice Console

Page 57: Spring 3 - An Introduction

Rapid Dynamic Robust

Have your next Web 2.0 project done in weeks instead of months. Grails delivers a new age of Java web application productivity.

Get instant feedback, See instant results. Grails is the premier dynamic language web framework for the JVM.

Powered by Spring, Grails out performs the competition. Dynamic, agile web development without compromises.

Page 58: Spring 3 - An Introduction

Project Wizard Support

different Grails Versions

Grails Project Converter

Grails Tooling Full Groovy Support

Grails Command

Prompt

Page 59: Spring 3 - An Introduction
Page 60: Spring 3 - An Introduction

CTRL+ALT+G (or CMD+ALT+G on Macs)

Page 61: Spring 3 - An Introduction
Page 62: Spring 3 - An Introduction

>grails create-app tenminutes-grails!<grails create-domain-class tenminutes.domain.Timer!

Timer.groovy:!class Timer {!

!String message! !static constraints = {!

! !message(nullable: false)! !}!}!

>grails create-controller tenminutes.web.TimerController!

TimerController.groovy:!class TimerControllerController {!

!def scaffold = Timer!}!

>grails run-app!

Page 63: Spring 3 - An Introduction
Page 64: Spring 3 - An Introduction

•  http://www.springframework.org/ •  http://www.springsource.com/ Spring

•  http://www.grails.org/ •  http://groovy.codehaus.org/

Grails & Groovy

•  http://www.thorsten-kamann.de •  http://www.itemis.de Extras

Page 65: Spring 3 - An Introduction
Page 66: Spring 3 - An Introduction

Erfolg durch Agilität: Scrum-Kompakt mit Joseph Pelrine –

14:00 bis 18:00 25. Februar 2010

Joseph Pelrine, Jena, Veranstalter: itemis AG

Von »Exzellenten Wissensorganisationen« lernen

– 13:00 bis 18:30 25. Februar 2010

Lise-Meitner-Allee 6, 44801 Bochum, Veranstalter: ck 2

Embedded World 2010 – Halle 10 – Stand 529

02.-04. März 2010 Nürnberg - Messezentrum 1

Hands On Model-Based Development with Eclipse –

09:30 bis 16:30 04. März 2010

Axel Terfloth, Andreas Unger, embeddedworld Nürnberg

Zukunft der Wissensarbeit. Best Practice Sharing auf der CeBIT 2010 – 10:00 bis 13:00

04. März 2010 Exzellente Wissensorganisation,

Hannover, Veranstalter: ck 2, kostenlos