Subtitle Text
Author
Contact info

David Gómez
@dgomezg
Spring 4
core improvements
Spring 4 core improvements
Generics in Qualifiers
Exposing attributes in Meta-annotations
Autowiring Lists and Arrays
@Des...
Generics in Qualifiers
En Spring 3.2….
public interface MessageService {	

!
!

public String getMessage();	

}	
public cl...
Generics in Qualifiers
En Spring 3.2….
<?xml version="1.0" encoding="UTF-8"?>	
<beans ...>	

!
!

	

!
!

<context:annotat...
Generics in Qualifiers
En Spring 4 also….
public interface MessageService<T> {	

!
!

public T getMessage();	

}	
public c...
Autowiring ordered Lists and Arrays
In Spring 3.2….
public interface MessageService {	

!
!

public String getMessage();	
...
Autowiring ordered Lists and Arrays
In Spring 4….
public interface MessageService {	

!
!

public String getMessage();	

p...
Exposing attributes in Meta-annotations
In Spring 3.2….
@Target({ElementType.TYPE})	
@Retention(RetentionPolicy.RUNTIME)	
...
Exposing attributes in Meta-annotations
In Spring 4….
@Target({ElementType.TYPE})	
@Retention(RetentionPolicy.RUNTIME)	
@S...
@Description on @Configuration classes
In Spring 4….
@Configuration	
@ComponentScan	
public class Application {	

!

@Bean...
@Profiles and @Conditional
In Spring 3.2….

!

<beans profile="dev">	
<jdbc:embedded-database id="dataSource">	
<jdbc:scri...
@Profiles and @Conditional
In Spring 4….
@Configuration	
@ComponentScan	
public class Application {	

!

@Bean	
@Descripti...
Web Container
improvements
Spring 4 web improvements
Support for Servlet 3.0
(Servlet 2.5 still supported for GAE compatibility)
(servlet 3.0 jar nee...
@RestController
In Spring 3.2….
@Controller	
public class WaverController {	

!

!

!

}	

@RequestMapping("/person")	
pub...
@RestController
In Spring 4…
@RestController = @Controller + @ResponseBody
@RestController	
public class WaverController {...
@AsyncRestTemplate (Non-blocking REST clients)
RestTemplate
public class RestTemplate {	
	
public <T> T getForObject(Strin...
@AsyncRestTemplate (Non-blocking REST clients)
AsyncRestTemplate
public class AsyncRestTemplate {	

!

	
	

public <T> Lis...
@AsyncRestTemplate (Non-blocking REST clients)
AsyncRestTemplate
ListenableFuture<ResponseEntity<Person>> futureEntity = t...
Spring 4
meets
Java 8
Support for lambdas on callbacks
In Spring 3.2
	
	

	
	
	

public Person findById(int id) {	
return jdbcTemplate.query("se...
Support for lambdas on callbacks
In Spring 4

public Person findById(int id) {	
return jdbcTemplate.queryForObject(	
"sele...
Support for lambdas on callbacks
In Spring 4

@Override	
@Transactional	
public Person getMessage() {	
final Person person...
JSR-310
package java.time
Distinction between Computer-times and Human-Times
Human-Times
TimeZone (ISO-8601)
LocalDateTime...
JSR-310
package java.time
Amounts of Time
Duration (nanosecond resolution)
Amounts of Days
Period (years, months, and days...
JSR-310 in Spring 4
In web handler Methods

import java.time.Clock;	
import java.time.ZoneId;	

!

@RestController	
public...
External Libraries
External Libraries Support
Hibernate 3.6+
Hibernate 4.3 (JPA 2.1)
EhCache 2.1
Quartz 1.8
JodaTime 2.0
Hibernate Validator ...
Other changes
Support for JEE7 (& JEE6)
Serlvet 3.0
JMS 2.0
JTA 1.2
JPA 2.1
Bean Validation 1.1
JSR-236 Concurrency (ThreadExecutors)
WebSockets
with
Spring 4
WebSocket Support
WebSocket server support via JSR-356 runtimes
(Tomcat 7.0.7 -Jetty 9)
Fallback option using SockJS
(Sock...
(de momento)

Subtitle Text
Author
Contact info

David Gómez
@dgomezg
Upcoming SlideShare
Loading in …5
×

Spring4 whats up doc?

4,712 views

Published on

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.

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,712
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
22
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Spring4 whats up doc?

  1. 1. Subtitle Text Author Contact info David Gómez @dgomezg
  2. 2. Spring 4 core improvements
  3. 3. 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
  4. 4. Generics in Qualifiers En Spring 3.2…. public 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() { ... } } 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()); } }
  5. 5. Generics in Qualifiers En Spring 3.2…. <?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>
  6. 6. Generics in Qualifiers En Spring 4 also…. public 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() { ... } } public class MultiMessagePrinter { ! ! ! } @Autowired private MessageService<Person> messageServices; public void printMessage() { System.out.println(messageService.getMessage().toString()); }
  7. 7. Autowiring ordered Lists and Arrays In Spring 3.2…. public 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() { ... } } 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!
  8. 8. Autowiring ordered Lists and Arrays In Spring 4…. public interface MessageService { ! ! public String getMessage(); public class GeneralWaver } implements MessageService, Ordered { ! ! ! } @Override public String getMessage() { return "Hello world!"; } @Override public int getOrder() { return Integer.MIN_VALUE; } public class MultiMessagePrinter { ! ! ! } public class PersonWaver implements MessageService { ! ! @Autowired public PersonRepository personRepository; @Override public int getOrder() { return 0; } } //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.
  9. 9. Exposing attributes in Meta-annotations In Spring 3.2…. @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service { String[] value(); } @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Service @Transactional(timeout=60) public @interface MyTransactionalService { String[] value(); } @MyTransactionalService public class PersonWaver implements MessageService { ! ! } @Autowired public PersonRepository personRepository; @Override public String getMessage() { ... }
  10. 10. Exposing attributes in Meta-annotations In Spring 4…. @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Service @Transactional public @interface MyTransactionalService { String[] value(); Propagation propagation() default Propagation.REQUIRED; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; ! } @MyTransactionalService(propagation=Propagation.REQUIRES_NEW) public class PersonWaver implements MessageService { ! ! } @Autowired public PersonRepository personRepository; @Override public String getMessage() { ... }
  11. 11. @Description on @Configuration classes In Spring 4…. @Configuration @ComponentScan public 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
  12. 12. @Profiles and @Conditional In 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" @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!"; } }; }
  13. 13. @Profiles and @Conditional In Spring 4…. @Configuration @ComponentScan public 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!"; } }; } } ! public class NoMessageServiceDefined implements Condition { ! } @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getBeanFactory().getBeansOfType(MessageService.class) .isEmpty(); }
  14. 14. Web Container improvements
  15. 15. Spring 4 web improvements Support 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) !
  16. 16. @RestController In Spring 3.2…. @Controller public class WaverController { ! ! ! } @RequestMapping("/person") public @ResponseBody Person showPersonalMessage() { return personWaver.getMessage(); } @RequestMapping("/message") public @ResponseBody String showMessage() { return genericWaver.getMessage(); }
  17. 17. @RestController In Spring 4… @RestController = @Controller + @ResponseBody @RestController public class WaverController { ! ! ! } @RequestMapping("/person") public Person showPersonalMessage() { return personWaver.getMessage(); } @RequestMapping("/message") public String showMessage() { return genericWaver.getMessage(); }
  18. 18. @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) {} } 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) {} }
  19. 19. @AsyncRestTemplate (Non-blocking REST clients) AsyncRestTemplate public 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);
  20. 20. @AsyncRestTemplate (Non-blocking REST clients) AsyncRestTemplate ListenableFuture<ResponseEntity<Person>> futureEntity = template.getForEntity( "http://localhost:8080/spring4/person/{personId}", Integer.class, 1); ! // register a callback futureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<Person>>() { @Override public void onSuccess(ResponseEntity<Person> entity) { //... } ! }); @Override public void onFailure(Throwable t) { //... }
  21. 21. Spring 4 meets Java 8
  22. 22. Support for lambdas on callbacks In 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); }
  23. 23. Support for lambdas on callbacks In 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); }
  24. 24. Support for lambdas on callbacks In Spring 4 @Override @Transactional public Person getMessage() { final Person person; ! ! } txTemplate.execute((txStatus) -> { person = messageRepository.findById(1); txStatus.setRollbackOnly(); return null; }); return person;
  25. 25. JSR-310 package java.time Distinction between Computer-times and Human-Times Human-Times TimeZone (ISO-8601) LocalDateTime LocalDate LocalTime
  26. 26. JSR-310 package java.time Amounts of Time Duration (nanosecond resolution) Amounts of Days Period (years, months, and days) TimeZones ZonedDateTime OffsetDateTime
  27. 27. JSR-310 in Spring 4 In web handler Methods
 import java.time.Clock; import java.time.ZoneId; ! @RestController public class WaverController { ! ! } @RequestMapping("/person") public Person showPersonalMessage(ZoneId zoneId) { Clock clock = Clock.of(zoneId) LocalTime time = LocalTime.now(clock); return personWaver.getMessageFor(time); }
  28. 28. External Libraries
  29. 29. External Libraries Support Hibernate 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)
  30. 30. Other changes
  31. 31. Support for JEE7 (& JEE6) Serlvet 3.0 JMS 2.0 JTA 1.2 JPA 2.1 Bean Validation 1.1 JSR-236 Concurrency (ThreadExecutors)
  32. 32. WebSockets with Spring 4
  33. 33. WebSocket Support WebSocket server support via JSR-356 runtimes (Tomcat 7.0.7 -Jetty 9) Fallback option using SockJS (SockJsHttpRequestHandler) k on tal g 4” ed prin icat ed ith S D ts w ocke ebS “W soon ming co
  34. 34. (de momento) Subtitle Text Author Contact info David Gómez @dgomezg

×