Spring Boot for the Java EE Developer

Bd3e6a603ad7608262e025fabe5343b6?s=47 Phil Webb
November 06, 2015
300

Spring Boot for the Java EE Developer

Bd3e6a603ad7608262e025fabe5343b6?s=128

Phil Webb

November 06, 2015
Tweet

Transcript

  1. Spring Boot for the Java EE Developer

  2. None
  3. None
  4. Overview • What is Spring Boot? • Spring Boot in

    a Java EE App Server • Java EE in Spring Boot
  5. WARning!! may contain traces of Spring boot 1.3

  6. What is Spring Boot? • Convention over configuration • Embedded

    Servlet Container • Zero XML
  7. “Spring Boot is like pair programming with the Spring Team”

  8. “We searched stackoverflow.com so you don’t have to”

  9. Demo Spring Boot in 5 minutEs

  10. Application Servers Deploying Spring Boot Apps to Java EE servers

  11. Your Code Spring Libs Java EE Libs Servlet Container .jar

    java -jar myapp.jar
  12. App Server Your Code Spring Libs Java EE Libs .war

    ./bin/asadmin start-domain
  13. Converting an app <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId>

    </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </exclusion> </exclusions> </dependency>
  14. Converting an app <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency>

    <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> or
  15. Converting an app @SpringBootApplication public class DemoApplication extends SpringBootServletInitializer {

    }
  16. Demo Spring Boot on payara

  17. Application Server Support • JNDI Support • DataSource / JMS

    / JTA / Java MAIL • Environment properties • Websphere Hibernate support • Wildfly & TomEE integration tests
  18. Java EE Consuming Java EE From Boot

  19. Convention & Configuration • Add a *-starter POM • Inject

    & use beans • Customize with application.properties • If it’s not enough: Define your own @Configuration
  20. Servlets & Filters • There’s no web.xml • Register using

    Spring Beans • Servlet or filter directly • Using *registration beans • or ServletContext Initializer
  21. Demo Filter

  22. JPA • Entity scanning from @SpringBootApplication • Auto-configured in-memory database

    • or use application.properties spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring-boot-starter-jpa
  23. Demo JPA

  24. JMS • ActiveMQ / Artemis / HornetQ • Sending •

    Receiving // inject JmsMessagingTemplate template.convertAndSend(“dest”, “message”); @JmsListener(destination = "someQueue") public void processMessage(String content) { // ... } spring-boot-starter-artemis
  25. Demo JMS

  26. Websockets • Spring’s Support (with sockjs fallback) spring-boot-starter-websocket @SpringBootApplication @EnableWebSocket

    public class Application implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(echoHandler(), "/echo").withSockJS(); } @Bean public WebSocketHandler echoWebSocketHandler() { return new EchoWebSocketHandler(); } }
  27. Websockets • JSR 356 Support spring-boot-starter-websocket @ServerEndpoint("/reverse") public class ReverseWebSocketEndpoint

    { @OnMessage public void handleMessage(Session session, String message) throws IOException { StringBuilder reverse = new StringBuilder(message).reverse(); session.getBasicRemote().sendText("Reversed: " + reverse); } } @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }
  28. Demo Websockets

  29. Bean Validation • JSR-303 & JSR-349 support • Spring MVC

    DataBinder • and @ConfigurationProperties spring-boot-starter-validation
  30. Bean Validation spring-boot-starter-validation @Component @ConfigurationProperties(prefix = "myapp") public class ExampleConfiguration

    { @Length(min = 8) private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } #application.properties myapp.password=short
  31. Demo Bean validation

  32. Caching • JSR-107 Auto-configuration (+ Others) • Spring’s @Cachable annotation

    spring-boot-starter-cache @Service public class MyService { @Cacheable("mycache") public String lengthy(int id) { // … } }
  33. Demo Caching

  34. Java mail • Auto-Configuration for java mail • MailSender interface

    spring-boot-starter-mail SimpleMailMessage message = new SimpleMailMessage(); message.setTo("example@example.com"); message.setText("Hello World!"); mailSender.send(message); spring.mail.host=mymailserver spring.mail.port=8025
  35. Demo Java mail

  36. Conclusion • Great way to consume Java EE • inside

    or outside of an App Server • cloud friendly • Go to spring.io for more information
  37. Thanks! Questions? @phillip_webb speakerdeck.com/philwebb github.com/philwebb/spring-boot-for-the-java-ee-developer spring.io/blog/2014/11/23/bootiful-java-ee-support-in-spring-boot-1-2