Spring tips (1)

[Index of Spring blogs]

This blog contains a set of small Spring tips.


Tip: Use Spring Devtools:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

With these dependencies:

  • When running your application from the IDEA, Spring will reload when classes are recompiled
  • Local development properties can be stored in $/.spring-boot-devtools.properties and will have a higher precedence over the application.properties
  • You can connect to a remote Spring application for debugging en reloading classes by when they are recompiled. For this to work you need to:
    • Run the remote application with: -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n
    • For the same project, open the Launch configurations, choose the following options:
      Select main class: org.springframework.boot.devtools.RemoteSpringApplication
      In program arguments, add the URL for the application, e.g. http://localhost:8080
      Default port for debugger via spring-boot application is 8000 and can be overridden via: spring.devtools.remote.debug.local-port=8010
    • Now create a remote-debug configuration, setting up the port as 8010 as configured via properties or 8000, if sticking to defaults

Tip: Use Lombok (as provided in the Spring Boot BOM):

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

Tip: Use @ConfigurationProperties for grouping your properties:

@Data
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "myapp")
public class ConfigProperties {
    private String property1;
    private String property2;
}

Application properties:

myapp.property1=prop1
myapp.property2=prop2

Autowire the properties:

@RestController
public class MyResource {
    @Autowired
    ConfigProperties configProperties;

Tip: Use Spring Sessions:

You can just inject HttpSession to use the session

@GetMapping("session")
public String getSession(HttpSession session) {
    return session.getId();
}

Sessions can to be synchonized using Redis, JDBC, Gemfire, or MongoDB.

More information on baeldung or on the spring.io.


Tip: Use Spring Caching:

Spring can cache the result of your methods and return the cached result when the arguments are the same.

In a web application, this is provided by default, otherwise you need to add the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

To enable caching, add the following configuration:

@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("numbers");
    }
}

The caching can be used in the following way:

@GetMapping("number/{nr}")
@Cacheable("numbers")
public String getNumber(@PathVariable long nr) {
    log.info("Return nr "+nr);
    return "number "+nr;
}

When calling the endpoint multiple times with the same argument, only one log entry will be shown.

More information on baeldung.


Tip: Use Lombok to generate a logger using @Slf4j :

Just annotate your class with @Slf4j and Lombok will add a log object for you that you can use to log:

@RestController
@Slf4j
public class MyResource {
    @GetMapping("/sample")
    public String getSample() {
        log.info("sample called");
        return "sample";
    }

 

Een gedachte over “Spring tips (1)

Reacties zijn gesloten.