Spring 3 - An Introduction

Preview:

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.

Citation preview

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

Von und mit Thorsten Kamann

22.02.2010

jUnit 3 Klassen

Commons Attributes

Struts 1.x Support

-

100% API

95% Extension

Points

Spring 3

Modules OSGi

Maven Enterprise Repository

#{...} @Value

XML ExpressionParser

Expression Language

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

</bean> !

@Repository !public class MyDatabase { !

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

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

} !

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);!

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

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

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

@Configuration @Bean @DependsOn

@Primary @Lazy @Import

@ImportResource @Value

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

!private String jdbcUrl;! !!

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

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

}!

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

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

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

}!

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

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

@Bean! public SessionFactory sessionFactory() {!

!...! }!}!

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

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

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

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

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

fooService.doStuff();!}!

Marshalling

JAXB Castor

Unmarshalling

JiBX Xstream XMLBeans

<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>!

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

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

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

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

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

<beans>!

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

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

</beans>!

Spring MVC

@Controller @RequestMapping @PathVariable

@Controller

The C of MVC

<context:component-scan/>

Mapping to an URI (optional)

@RequestMapping

Mapping a Controller or Methods to an URI

URI-Pattern

Mapping to a HTTP-Method

Works with @PathVariable

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

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

}!

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

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

}!

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

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

}!

@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:...“;!!}!

}!

RestTemplate

Delete Get Head Options Post Put

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!

public class VistorForm(!!@NotNull!

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

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

}!

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

JSR-303

HSQL H2 Derby ...

<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();!

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();! }!}!

@Async (out of EJB 3.1)

JSR-303

JSF 2.0

JPA 2

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

Grails for Java

Spring Best Practices

AOP-driven

Test-driven Nice Console

Command Line Shell

@Roo* Annotations

(Source-Level)

AspectJ Intertype

Declarations Metadata RoundTrip

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

Roo for Groovy

Best Practices

Groovy-driven

Test-driven

Nice Console

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.

Project Wizard Support

different Grails Versions

Grails Project Converter

Grails Tooling Full Groovy Support

Grails Command

Prompt

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

>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!

•  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

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

Recommended