Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Boot for the Java EE Developer

Phil Webb
November 06, 2015
360

Spring Boot for the Java EE Developer

Phil Webb

November 06, 2015
Tweet

Transcript

  1. Overview • What is Spring Boot? • Spring Boot in

    a Java EE App Server • Java EE in Spring Boot
  2. 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>
  3. 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
  4. Application Server Support • JNDI Support • DataSource / JMS

    / JTA / Java MAIL • Environment properties • Websphere Hibernate support • Wildfly & TomEE integration tests
  5. Convention & Configuration • Add a *-starter POM • Inject

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

    Spring Beans • Servlet or filter directly • Using *registration beans • or ServletContext Initializer
  7. 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
  8. JMS • ActiveMQ / Artemis / HornetQ • Sending •

    Receiving // inject JmsMessagingTemplate template.convertAndSend(“dest”, “message”); @JmsListener(destination = "someQueue") public void processMessage(String content) { // ... } spring-boot-starter-artemis
  9. 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(); } }
  10. 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(); }
  11. Bean Validation • JSR-303 & JSR-349 support • Spring MVC

    DataBinder • and @ConfigurationProperties spring-boot-starter-validation
  12. 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
  13. 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) { // … } }
  14. Java mail • Auto-Configuration for java mail • MailSender interface

    spring-boot-starter-mail SimpleMailMessage message = new SimpleMailMessage(); message.setTo("[email protected]"); message.setText("Hello World!"); mailSender.send(message); spring.mail.host=mymailserver spring.mail.port=8025
  15. Conclusion • Great way to consume Java EE • inside

    or outside of an App Server • cloud friendly • Go to spring.io for more information