34
Subtitle Text Author Contact info David Gómez @dgomezg

Spring4 whats up doc?

Embed Size (px)

DESCRIPTION

Spring 4.0 has been released on December 2013. If you want to review some of the Core and Web container improvements, check this out.

Citation preview

Page 1: Spring4 whats up doc?

Subtitle TextAuthorContact info

David Gómez@dgomezg

Page 2: Spring4 whats up doc?

Spring 4 core improvements

Page 3: Spring4 whats up doc?

Spring 4 core improvements

Generics in Qualifiers

Exposing attributes in Meta-annotations

Autowiring Lists and Arrays

@Description on @Configuration classes

@Conditional (user-defined @Profiles)

Time Zone support on Locale Context

Page 4: Spring4 whats up doc?

Generics in Qualifierspublic interface MessageService {! public String getMessage();!}

public class GeneralWaver implements MessageService{ @Override public String getMessage() { return "Hello world!"; }!}

public class PersonWaver implements MessageService {! @Autowired public PersonRepository personRepository;! @Override public String getMessage() { ... }}

En Spring 3.2….

public class MultiMessagePrinter {! //All Message Services are injected @Autowired private List<MessageService> messageServices;! public void printMessage() { for (MessageService messageService: messageServices) { System.out.println(messageService.getMessage()); } }!}

Page 5: Spring4 whats up doc?

Generics in Qualifiers

<?xml version="1.0" encoding="UTF-8"?><beans ...>! <context:annotation-config/>! <!-- Database config --> <jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:sql/schema.ddl"/> <jdbc:script location="classpath:sql/data.sql"/> </jdbc:embedded-database> <bean id=“personRepository" class="com.autentia.playground.spring4.helloWorld.db.JdbcPersonRepository"/>! <!-- Wavers (MessageService implementations) --> <bean id="personWaver" class="com.autentia.playground.spring4.helloWorld.messages.PersonWaver"/> <bean id="generalWaver" class="com.autentia.playground.spring4.helloWorld.messages.GeneralWaver"/>! <!-- Printer : waves to everybody using available MessageServices --> <bean id="messagePrinter" class="com.autentia.playground.spring4.helloWorld.messages.MultiMessagePrinter"/>!</beans>

En Spring 3.2….

Page 6: Spring4 whats up doc?

Generics in Qualifierspublic interface MessageService<T> {! public T getMessage();!}

public class GeneralWaver implements MessageService<String>{ @Override public String getMessage() { return "Hello world!"; }!}

public class PersonWaver implements MessageService<Person> {! @Autowired public PersonRepository personRepository;! @Override public Person getMessage() { ... }}

En Spring 4 also….

public class MultiMessagePrinter {! @Autowired private MessageService<Person> messageServices;! public void printMessage() { System.out.println(messageService.getMessage().toString()); }!}

Page 7: Spring4 whats up doc?

public interface MessageService {! public String getMessage();!}

Autowiring ordered Lists and ArraysIn Spring 3.2….

public class GeneralWaver implements MessageService{ @Override public String getMessage() { return "Hello world!"; }!}

public class PersonWaver implements MessageService {! @Autowired public PersonRepository personRepository;! @Override public String getMessage() { ... }}

public class MultiMessagePrinter {! //All Message Services are injected @Autowired private List<MessageService> messageServices;! public void printMessage() { for (MessageService messageService: messageServices) { System.out.println(messageService.getMessage()); } }!}

Hello Sr. David Gomez G.Hello world!

Page 8: Spring4 whats up doc?

public interface MessageService {! public String getMessage();!}

Autowiring ordered Lists and ArraysIn Spring 4….

public class GeneralWaver implements MessageService, Ordered {! @Override public String getMessage() { return "Hello world!"; }! @Override public int getOrder() { return Integer.MIN_VALUE; }!}

public class PersonWaver implements MessageService {! @Autowired public PersonRepository personRepository;! @Override public int getOrder() { return 0; }}

public class MultiMessagePrinter {! //All Message Services are injected @Autowired private List<MessageService> messageServices;! public void printMessage() { for (MessageService messageService: messageServices) { System.out.println(messageService.getMessage()); } }!}

Hello world!Hello Sr. David Gomez G.

Page 9: Spring4 whats up doc?

Exposing attributes in Meta-annotationsIn Spring 3.2….

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Service { String[] value();}

@MyTransactionalServicepublic class PersonWaver implements MessageService {! @Autowired public PersonRepository personRepository;! @Override public String getMessage() { ... }}

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Service@Transactional(timeout=60)public @interface MyTransactionalService { String[] value();}

Page 10: Spring4 whats up doc?

Exposing attributes in Meta-annotationsIn Spring 4….

@MyTransactionalService(propagation=Propagation.REQUIRES_NEW)public class PersonWaver implements MessageService {! @Autowired public PersonRepository personRepository;! @Override public String getMessage() { ... }}

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Service@Transactionalpublic @interface MyTransactionalService { String[] value(); Propagation propagation() default Propagation.REQUIRED; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;!}

Page 11: Spring4 whats up doc?

@Description on @Configuration classesIn Spring 4….

@Configuration@ComponentScanpublic class Application {! @Bean @Description("This is a mock implementation of MockService") MessageService mockMessageService() { return new MessageService() { @Override public String getMessage() { return "Hello world!"; } }; }}

Useful when beans are exposed, !for example, as JMX beans

Page 12: Spring4 whats up doc?

@Configuration@ComponentScan@Profile(“test”)public class Application {! @Bean @Description("This is a mock implementation of MockService") MessageService mockMessageService() { return new MessageService() { @Override public String getMessage() { return "Hello world!"; } }; }}

@Profiles and @ConditionalIn Spring 3.2….

<beans profile="dev"> <jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:sql/schema.ddl"/> <jdbc:script location="classpath:sql/data.sql"/> </jdbc:embedded-database> </beans>! <beans profile="prod"> <jee:jndi-lookup id="dataSource" jndi-name="jdbc/LiveDataSource"/> </beans>

-Dspring.profiles.active=“dev"

Page 13: Spring4 whats up doc?

@Configuration@ComponentScanpublic class Application {! @Bean @Description("This is a mock implementation of MockService”) @Conditional(NoMessageServiceDefined.class) MessageService mockMessageService() { return new MessageService() { @Override public String getMessage() { return "Hello world!"; } }; }}

@Profiles and @ConditionalIn Spring 4….

!public class NoMessageServiceDefined implements Condition {! @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getBeanFactory().getBeansOfType(MessageService.class) .isEmpty(); }}

Page 14: Spring4 whats up doc?

Web Container improvements

Page 15: Spring4 whats up doc?

Spring 4 web improvementsSupport for Servlet 3.0

(Servlet 2.5 still supported for GAE compatibility)

(servlet 3.0 jar needed for SPRING MVC Tests)

@RestController (@RequestMapping + @ResponseBody)

@AsyncRestTemplate (Non-blocking REST clients)

!

Page 16: Spring4 whats up doc?

@Controllerpublic class WaverController {! @RequestMapping("/person") public @ResponseBody Person showPersonalMessage() { return personWaver.getMessage(); }! @RequestMapping("/message") public @ResponseBody String showMessage() { return genericWaver.getMessage(); }!}

@RestControllerIn Spring 3.2….

Page 17: Spring4 whats up doc?

@RestControllerpublic class WaverController {! @RequestMapping("/person") public Person showPersonalMessage() { return personWaver.getMessage(); }! @RequestMapping("/message") public String showMessage() { return genericWaver.getMessage(); }!}

@RestControllerIn Spring 4…

@RestController = @Controller + @ResponseBody

Page 18: Spring4 whats up doc?

public class RestTemplate { public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) {} public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables) {} public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) {} public void put(String url, Object request, Object... urlVariables) {}}

@AsyncRestTemplate (Non-blocking REST clients)

RestTemplate

public class RestTemplate { public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) {} public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables) {} public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) {} public void put(String url, Object request, Object... urlVariables) {}}

Page 19: Spring4 whats up doc?

@AsyncRestTemplate (Non-blocking REST clients)

AsyncRestTemplatepublic class AsyncRestTemplate {! public <T> ListenableFuture<ResponseEntity<T>> getForEntity(String url, Class<T> responseType, Object... uriVariables) {}! public ListenableFuture<URI> postForLocation(String url, HttpEntity<?> request, Object... uriVariables) {}! public ListenableFuture<?> put(String url, HttpEntity<?> request, Object... uriVariables) {}}

public interface ListenableFuture<T> extends Future<T> {! void addCallback(ListenableFutureCallback<? super T> callback);!}

Page 20: Spring4 whats up doc?

@AsyncRestTemplate (Non-blocking REST clients)

AsyncRestTemplate

ListenableFuture<ResponseEntity<Person>> futureEntity = template.getForEntity( "http://localhost:8080/spring4/person/{personId}", Integer.class, 1);!// register a callbackfutureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<Person>>() { @Override public void onSuccess(ResponseEntity<Person> entity) { //... }! @Override public void onFailure(Throwable t) { //... }});

Page 21: Spring4 whats up doc?

Spring 4 meets Java 8

Page 22: Spring4 whats up doc?

Support for lambdas on callbacksIn Spring 3.2

public Person findById(int id) { return jdbcTemplate.query("select * from persons where id = ?", new RowMapper<Person>() { @Override public Person mapRow(ResultSet rs, int rowNum) throws SQLException { return new Person(rs.getInt("id"), rs.getString("treatment"), rs.getString("name"), rs.getString("surname"), new Date(rs.getDate("birthDate").getTime())); } }, id) .get(0); }

Page 23: Spring4 whats up doc?

Support for lambdas on callbacksIn Spring 4

public Person findById(int id) { return jdbcTemplate.queryForObject( "select * from persons where id = ?", (rs, rowNum) -> new Person(rs.getInt("id"), rs.getString("treatment"), rs.getString("name"), rs.getString("surname"), new Date(rs.getDate("birthDate").getTime())), id);}

Page 24: Spring4 whats up doc?

Support for lambdas on callbacksIn Spring 4

@Override@Transactionalpublic Person getMessage() { final Person person;! txTemplate.execute((txStatus) -> { person = messageRepository.findById(1); txStatus.setRollbackOnly(); return null; });! return person;}

Page 25: Spring4 whats up doc?

JSR-310package java.time

Distinction between Computer-times and Human-Times

Human-Times

TimeZone (ISO-8601)

LocalDateTime

LocalDate

LocalTime

Page 26: Spring4 whats up doc?

JSR-310package java.time

Amounts of Time

Duration (nanosecond resolution)

Amounts of Days

Period (years, months, and days)

TimeZones

ZonedDateTime

OffsetDateTime

Page 27: Spring4 whats up doc?

JSR-310 in Spring 4

In web handler Methodsimport java.time.Clock;import java.time.ZoneId;!@RestControllerpublic class WaverController {! @RequestMapping("/person") public Person showPersonalMessage(ZoneId zoneId) { Clock clock = Clock.of(zoneId) LocalTime time = LocalTime.now(clock); return personWaver.getMessageFor(time); }!}

Page 28: Spring4 whats up doc?

External Libraries

Page 29: Spring4 whats up doc?

External Libraries SupportHibernate 3.6+

Hibernate 4.3 (JPA 2.1)

EhCache 2.1

Quartz 1.8

JodaTime 2.0

Hibernate Validator 4.3 (Bean Validation 1.1)

Jackson 2.0 (1.8/1.9 deprecated)

Page 30: Spring4 whats up doc?

Other changes

Page 31: Spring4 whats up doc?

Support for JEE7 (& JEE6)Serlvet 3.0

JMS 2.0

JTA 1.2

JPA 2.1

Bean Validation 1.1

JSR-236 Concurrency (ThreadExecutors)

Page 32: Spring4 whats up doc?

WebSockets with

Spring 4

Page 33: Spring4 whats up doc?

WebSocket SupportWebSocket server support via JSR-356 runtimes

(Tomcat 7.0.7 -Jetty 9)

Fallback option using SockJS

(SockJsHttpRequestHandler)

Dedicated talk on

“WebSockets with Spring 4”

coming soon

Page 34: Spring4 whats up doc?

Subtitle TextAuthorContact info

David Gómez@dgomezg

(de momento)