Difference between revisions of "Spring tips"
From DarkWiki
(→@Value) |
|||
| (13 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | ==@Value== | + | ==Configuration== |
| + | |||
| + | ===@Value=== | ||
| + | |||
| + | Simple setting of values. | ||
| + | |||
| + | <source lang="java"> | ||
| + | @Value("${prop.strName:default value}}") | ||
| + | private String localDir; | ||
| + | |||
| + | @Value("${prop.intName:14}}") | ||
| + | private int localDir; | ||
| + | </source> | ||
Apply a setting, defaulting to a subdirectory of the temporary folder. | Apply a setting, defaulting to a subdirectory of the temporary folder. | ||
| Line 13: | Line 25: | ||
@Value("${prop.name:#{null}}") | @Value("${prop.name:#{null}}") | ||
private String remoteUrl; | private String remoteUrl; | ||
| + | </source> | ||
| + | |||
| + | ===Noteworthy classes=== | ||
| + | |||
| + | {| class="wikitable" | ||
| + | !Class!!Notes | ||
| + | |- | ||
| + | ||ClassPathScanningCandidateComponentProvider||Used for scanning for annotated classes etc. See: https://gist.github.com/skempken/dbb2ad55d213cd6a1f50 | ||
| + | |- | ||
| + | ||SpringDataWebProperties||A good example of how Spring configuration properties are bound from YML (etc.) | ||
| + | |} | ||
| + | |||
| + | ===Further reading=== | ||
| + | |||
| + | A list of configuration properties can be found here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html | ||
| + | |||
| + | ==Data== | ||
| + | |||
| + | ===Pageable=== | ||
| + | |||
| + | Given an endpoint: | ||
| + | |||
| + | <source lang="java"> | ||
| + | @GetMapping("/api/test/pager") | ||
| + | public Page<String> pager(@PageableDefault(sort = "name",direction = Sort.Direction.ASC) Pageable pageable) { | ||
| + | return new PageImpl<>(List.of("My string"),pageable,1); | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | It can be called using: | ||
| + | |||
| + | <pre> | ||
| + | http://localhost:8080/api/test/pager?sort=andy,desc&sort=age,asc | ||
| + | </pre> | ||
| + | |||
| + | It will output something like: | ||
| + | |||
| + | <source lang="json"> | ||
| + | { | ||
| + | "content": [ | ||
| + | "My string" | ||
| + | ], | ||
| + | "pageable": { | ||
| + | "sort": { | ||
| + | "sorted": true, | ||
| + | "unsorted": false, | ||
| + | "empty": false | ||
| + | }, | ||
| + | "offset": 0, | ||
| + | "pageNumber": 0, | ||
| + | "pageSize": 10, | ||
| + | "paged": true, | ||
| + | "unpaged": false | ||
| + | }, | ||
| + | "last": true, | ||
| + | "totalPages": 1, | ||
| + | "totalElements": 1, | ||
| + | "number": 0, | ||
| + | "sort": { | ||
| + | "sorted": true, | ||
| + | "unsorted": false, | ||
| + | "empty": false | ||
| + | }, | ||
| + | "size": 10, | ||
| + | "first": true, | ||
| + | "numberOfElements": 1, | ||
| + | "empty": false | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | ==Useful classes== | ||
| + | |||
| + | ===ApplicationListener=== | ||
| + | |||
| + | You can execute code once the application becomes ready (e.g. as part of start-up): | ||
| + | |||
| + | <source lang="java"> | ||
| + | ... | ||
| + | @Component | ||
| + | class MyClass implements ApplicationListener<ApplicationReadyEvent> { | ||
| + | ... | ||
| + | @Override | ||
| + | public void onApplicationEvent(ApplicationReadyEvent event) { | ||
| + | // Do something | ||
| + | } | ||
| + | ... | ||
| + | } | ||
</source> | </source> | ||
Latest revision as of 09:33, 16 April 2020
Contents
Configuration
@Value
Simple setting of values.
@Value("${prop.strName:default value}}")
private String localDir;
@Value("${prop.intName:14}}")
private int localDir;
Apply a setting, defaulting to a subdirectory of the temporary folder.
@Value("${prop.name:#{systemProperties['java.io.tmpdir']+'/subdir'}}")
private String localDir;
When a value is not present in the application.properties file, assume null.
@Value("${prop.name:#{null}}")
private String remoteUrl;
Noteworthy classes
| Class | Notes |
|---|---|
| ClassPathScanningCandidateComponentProvider | Used for scanning for annotated classes etc. See: https://gist.github.com/skempken/dbb2ad55d213cd6a1f50 |
| SpringDataWebProperties | A good example of how Spring configuration properties are bound from YML (etc.) |
Further reading
A list of configuration properties can be found here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Data
Pageable
Given an endpoint:
@GetMapping("/api/test/pager")
public Page<String> pager(@PageableDefault(sort = "name",direction = Sort.Direction.ASC) Pageable pageable) {
return new PageImpl<>(List.of("My string"),pageable,1);
}
It can be called using:
http://localhost:8080/api/test/pager?sort=andy,desc&sort=age,asc
It will output something like:
{
"content": [
"My string"
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 10,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 1,
"number": 0,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"size": 10,
"first": true,
"numberOfElements": 1,
"empty": false
}
Useful classes
ApplicationListener
You can execute code once the application becomes ready (e.g. as part of start-up):
...
@Component
class MyClass implements ApplicationListener<ApplicationReadyEvent> {
...
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// Do something
}
...
}