Merge branch 'master' of https://github.com/Maiklins/tutorials into BAEL-20858-move-spring-boot-camel
Conflicts: spring-boot-modules/pom.xml
This commit is contained in:
@@ -15,7 +15,10 @@
|
||||
|
||||
<modules>
|
||||
<module>spring-boot-camel</module>
|
||||
<module>spring-boot-data</module>
|
||||
<module>spring-boot-mvc-birt</module>
|
||||
<module>spring-boot-properties</module>
|
||||
<module>spring-boot-testing</module>
|
||||
<module>spring-boot-vue</module>
|
||||
</modules>
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Spring Boot Data
|
||||
|
||||
This module contains articles about Spring Boot with Spring Data
|
||||
|
||||
## Relevant Articles:
|
||||
|
||||
- [Formatting JSON Dates in Spring Boot](https://www.baeldung.com/spring-boot-formatting-json-dates)
|
||||
- [Rendering Exceptions in JSON with Spring](https://www.baeldung.com/spring-exceptions-json)
|
||||
- [Disable Spring Data Auto Configuration](https://www.baeldung.com/spring-data-disable-auto-config)
|
||||
- [Repositories with Multiple Spring Data Modules](https://www.baeldung.com/spring-multiple-data-modules)
|
||||
- [Spring Custom Property Editor](https://www.baeldung.com/spring-mvc-custom-property-editor)
|
||||
@@ -0,0 +1,175 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-data</artifactId>
|
||||
<name>spring-boot-data</name>
|
||||
<packaging>war</packaging>
|
||||
<description>Spring Boot Data Module</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javers</groupId>
|
||||
<artifactId>javers-spring-boot-starter-sql</artifactId>
|
||||
<version>${javers.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-boot-data</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>pl.project13.maven</groupId>
|
||||
<artifactId>git-commit-id-plugin</artifactId>
|
||||
<version>${git-commit-id-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>get-the-git-infos</id>
|
||||
<goals>
|
||||
<goal>revision</goal>
|
||||
</goals>
|
||||
<phase>initialize</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>validate-the-git-infos</id>
|
||||
<goals>
|
||||
<goal>validateRevision</goal>
|
||||
</goals>
|
||||
<phase>package</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<generateGitPropertiesFile>true</generateGitPropertiesFile>
|
||||
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
|
||||
</generateGitPropertiesFilename>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>autoconfiguration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.javers.SpringBootJaVersApplication</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/AutoconfigurationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<javers.version>5.6.3</javers.version>
|
||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootDataApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootDataApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.disableautoconfig;
|
||||
|
||||
import org.javers.spring.boot.sql.JaversSqlAutoConfiguration;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
|
||||
JaversSqlAutoConfiguration.class, SpringDataWebAutoConfiguration.class,
|
||||
DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
|
||||
public class SpringDataJPA {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataJPA.class, args);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.disableautoconfig;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
|
||||
SpringDataWebAutoConfiguration.class})
|
||||
public class SpringDataMongoDB {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataMongoDB.class, args);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.disableautoconfig;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = {RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class,
|
||||
SpringDataWebAutoConfiguration.class})
|
||||
public class SpringDataRedis {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataRedis.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles
|
||||
|
||||
- [Using JaVers for Data Model Auditing in Spring Data](https://www.baeldung.com/spring-data-javers-audit)
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.javers;
|
||||
|
||||
import com.baeldung.javers.domain.Address;
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import com.baeldung.javers.repo.StoreRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootJaVersApplication {
|
||||
@Autowired
|
||||
StoreRepository storeRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootJaVersApplication.class, args);
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void appReady(ApplicationReadyEvent event) {
|
||||
Store store = new Store("Baeldung store", new Address("Some street", 22222));
|
||||
for (int i = 1; i < 3; i++) {
|
||||
Product product = new Product("Product #" + i, 100 * i);
|
||||
store.addProduct(product);
|
||||
}
|
||||
storeRepository.save(store);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.javers.config;
|
||||
|
||||
import org.javers.spring.auditable.AuthorProvider;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JaversConfiguration {
|
||||
@Bean
|
||||
public AuthorProvider provideJaversAuthor() {
|
||||
return new SimpleAuthorProvider();
|
||||
}
|
||||
|
||||
private static class SimpleAuthorProvider implements AuthorProvider {
|
||||
@Override
|
||||
public String provide() {
|
||||
return "Baeldung Author";
|
||||
}
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.javers.domain;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
private String address;
|
||||
private Integer zipCode;
|
||||
|
||||
public Address(String address, Integer zipCode) {
|
||||
this.address = address;
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Integer getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
|
||||
public void setZipCode(Integer zipCode) {
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.javers.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "store_id")
|
||||
private Store store;
|
||||
|
||||
public Product(String name, double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Product() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Store getStore() {
|
||||
return store;
|
||||
}
|
||||
|
||||
public void setStore(Store store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public void setNamePrefix(String prefix) {
|
||||
this.name = prefix + this.name;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.javers.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Store {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
@Embedded
|
||||
private Address address;
|
||||
@OneToMany(
|
||||
mappedBy = "store",
|
||||
cascade = CascadeType.ALL,
|
||||
orphanRemoval = true
|
||||
)
|
||||
private List<Product> products = new ArrayList<>();
|
||||
|
||||
public Store(String name, Address address) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Store() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void addProduct(Product product) {
|
||||
product.setStore(this);
|
||||
this.products.add(product);
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return this.products;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.javers.repo;
|
||||
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import org.javers.spring.annotation.JaversAuditable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ProductRepository extends CrudRepository<Product, Integer> {
|
||||
@Override
|
||||
@JaversAuditable
|
||||
<S extends Product> S save(S s);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.javers.repo;
|
||||
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import org.javers.spring.annotation.JaversSpringDataAuditable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
@JaversSpringDataAuditable
|
||||
public interface StoreRepository extends CrudRepository<Store, Integer> {
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.javers.service;
|
||||
|
||||
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import com.baeldung.javers.repo.ProductRepository;
|
||||
import com.baeldung.javers.repo.StoreRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class StoreService {
|
||||
private final ProductRepository productRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
|
||||
public StoreService(ProductRepository productRepository, StoreRepository storeRepository) {
|
||||
this.productRepository = productRepository;
|
||||
this.storeRepository = storeRepository;
|
||||
}
|
||||
|
||||
public void updateProductPrice(Integer productId, Double price) {
|
||||
Optional<Product> productOpt = productRepository.findById(productId);
|
||||
productOpt.ifPresent(product -> {
|
||||
product.setPrice(price);
|
||||
productRepository.save(product);
|
||||
});
|
||||
}
|
||||
|
||||
public void rebrandStore(int storeId, String updatedName) {
|
||||
Optional<Store> storeOpt = storeRepository.findById(storeId);
|
||||
storeOpt.ifPresent(store -> {
|
||||
store.setName(updatedName);
|
||||
store.getProducts().forEach(product -> {
|
||||
product.setNamePrefix(updatedName);
|
||||
});
|
||||
storeRepository.save(store);
|
||||
});
|
||||
}
|
||||
|
||||
public void createRandomProduct(Integer storeId) {
|
||||
Optional<Store> storeOpt = this.storeRepository.findById(storeId);
|
||||
storeOpt.ifPresent(store -> {
|
||||
Random random = new Random();
|
||||
Product product = new Product("Product#" + random.nextInt(), random.nextDouble() * 100);
|
||||
store.addProduct(product);
|
||||
storeRepository.save(store);
|
||||
});
|
||||
}
|
||||
|
||||
public Store findStoreById(int storeId) {
|
||||
return storeRepository.findById(storeId).get();
|
||||
}
|
||||
|
||||
public Product findProductById(int id) {
|
||||
return this.productRepository.findById(id).get();
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.javers.web;
|
||||
|
||||
public class RebrandStoreDto {
|
||||
public String name;
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.javers.web;
|
||||
|
||||
import com.baeldung.javers.domain.Product;
|
||||
import com.baeldung.javers.domain.Store;
|
||||
import com.baeldung.javers.service.StoreService;
|
||||
import org.javers.core.Changes;
|
||||
import org.javers.core.Javers;
|
||||
import org.javers.core.metamodel.object.CdoSnapshot;
|
||||
import org.javers.repository.jql.JqlQuery;
|
||||
import org.javers.repository.jql.QueryBuilder;
|
||||
import org.javers.shadow.Shadow;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class StoreController {
|
||||
private final StoreService storeService;
|
||||
private final Javers javers;
|
||||
|
||||
public StoreController(StoreService customerService, Javers javers) {
|
||||
this.storeService = customerService;
|
||||
this.javers = javers;
|
||||
}
|
||||
|
||||
@PostMapping("/stores/{storeId}/products/random")
|
||||
public void createRandomProduct(@PathVariable final Integer storeId) {
|
||||
storeService.createRandomProduct(storeId);
|
||||
}
|
||||
|
||||
@PostMapping("/stores/{storeId}/rebrand")
|
||||
public void rebrandStore(@PathVariable final Integer storeId, @RequestBody RebrandStoreDto rebrandStoreDto) {
|
||||
storeService.rebrandStore(storeId, rebrandStoreDto.name);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/stores/{storeId}/products/{productId}/price", consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
public void updateProductPrice(@PathVariable final Integer productId, @PathVariable String storeId, @RequestBody UpdatePriceDto priceDto) {
|
||||
storeService.updateProductPrice(productId, priceDto.price);
|
||||
}
|
||||
|
||||
@GetMapping("/products/{productId}/changes")
|
||||
public String getProductChanges(@PathVariable int productId) {
|
||||
Product product = storeService.findProductById(productId);
|
||||
QueryBuilder jqlQuery = QueryBuilder.byInstance(product);
|
||||
Changes changes = javers.findChanges(jqlQuery.build());
|
||||
return javers.getJsonConverter().toJson(changes);
|
||||
}
|
||||
|
||||
@GetMapping("/products/snapshots")
|
||||
public String getProductSnapshots() {
|
||||
QueryBuilder jqlQuery = QueryBuilder.byClass(Product.class);
|
||||
List<CdoSnapshot> snapshots = javers.findSnapshots(jqlQuery.build());
|
||||
return javers.getJsonConverter().toJson(snapshots);
|
||||
}
|
||||
|
||||
@GetMapping("/stores/{storeId}/shadows")
|
||||
public String getStoreShadows(@PathVariable int storeId) {
|
||||
Store store = storeService.findStoreById(storeId);
|
||||
JqlQuery jqlQuery = QueryBuilder.byInstance(store)
|
||||
.withChildValueObjects().build();
|
||||
List<Shadow<Store>> shadows = javers.findShadows(jqlQuery);
|
||||
return javers.getJsonConverter().toJson(shadows.get(0));
|
||||
}
|
||||
|
||||
@GetMapping("/stores/snapshots")
|
||||
public String getStoresSnapshots() {
|
||||
QueryBuilder jqlQuery = QueryBuilder.byClass(Store.class);
|
||||
List<CdoSnapshot> snapshots = javers.findSnapshots(jqlQuery.build());
|
||||
return javers.getJsonConverter().toJson(snapshots);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.javers.web;
|
||||
|
||||
public class UpdatePriceDto {
|
||||
public double price;
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Contact {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd")
|
||||
private LocalDate birthday;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public LocalDate getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(LocalDate birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public LocalDateTime getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(LocalDateTime lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public Contact() {
|
||||
}
|
||||
|
||||
public Contact(String name, String address, String phone, LocalDate birthday, LocalDateTime lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ContactApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ContactApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Configuration
|
||||
public class ContactAppConfig {
|
||||
|
||||
private static final String dateFormat = "yyyy-MM-dd";
|
||||
|
||||
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "spring.jackson.date-format", matchIfMissing = true, havingValue = "none")
|
||||
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
|
||||
return new Jackson2ObjectMapperBuilderCustomizer() {
|
||||
@Override
|
||||
public void customize(Jackson2ObjectMapperBuilder builder) {
|
||||
builder.simpleDateFormat(dateTimeFormat);
|
||||
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
|
||||
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/contacts")
|
||||
public class ContactController {
|
||||
|
||||
@GetMapping
|
||||
public List<Contact> getContacts() {
|
||||
List<Contact> contacts = new ArrayList<>();
|
||||
|
||||
Contact contact1 = new Contact("John Doe", "123 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
Contact contact2 = new Contact("John Doe 2", "124 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
Contact contact3 = new Contact("John Doe 3", "125 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@GetMapping("/javaUtilDate")
|
||||
public List<ContactWithJavaUtilDate> getContactsWithJavaUtilDate() {
|
||||
List<ContactWithJavaUtilDate> contacts = new ArrayList<>();
|
||||
|
||||
ContactWithJavaUtilDate contact1 = new ContactWithJavaUtilDate("John Doe", "123 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
ContactWithJavaUtilDate contact2 = new ContactWithJavaUtilDate("John Doe 2", "124 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
ContactWithJavaUtilDate contact3 = new ContactWithJavaUtilDate("John Doe 3", "125 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@GetMapping("/plain")
|
||||
public List<PlainContact> getPlainContacts() {
|
||||
List<PlainContact> contacts = new ArrayList<>();
|
||||
|
||||
PlainContact contact1 = new PlainContact("John Doe", "123 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
PlainContact contact2 = new PlainContact("John Doe 2", "124 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
PlainContact contact3 = new PlainContact("John Doe 3", "125 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@GetMapping("/plainWithJavaUtilDate")
|
||||
public List<PlainContactWithJavaUtilDate> getPlainContactsWithJavaUtilDate() {
|
||||
List<PlainContactWithJavaUtilDate> contacts = new ArrayList<>();
|
||||
|
||||
PlainContactWithJavaUtilDate contact1 = new PlainContactWithJavaUtilDate("John Doe", "123 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
PlainContactWithJavaUtilDate contact2 = new PlainContactWithJavaUtilDate("John Doe 2", "124 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
PlainContactWithJavaUtilDate contact3 = new PlainContactWithJavaUtilDate("John Doe 3", "125 Sesame Street", "123-456-789", new Date(), new Date());
|
||||
|
||||
contacts.add(contact1);
|
||||
contacts.add(contact2);
|
||||
contacts.add(contact3);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class ContactWithJavaUtilDate {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd")
|
||||
private Date birthday;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public Date getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(Date lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public ContactWithJavaUtilDate() {
|
||||
}
|
||||
|
||||
public ContactWithJavaUtilDate(String name, String address, String phone, Date birthday, Date lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class PlainContact {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
private LocalDate birthday;
|
||||
|
||||
private LocalDateTime lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public LocalDate getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(LocalDate birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public LocalDateTime getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(LocalDateTime lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public PlainContact() {
|
||||
}
|
||||
|
||||
public PlainContact(String name, String address, String phone, LocalDate birthday, LocalDateTime lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class PlainContactWithJavaUtilDate {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
private String phone;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd")
|
||||
private Date birthday;
|
||||
|
||||
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date lastUpdate;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public Date getLastUpdate() {
|
||||
return lastUpdate;
|
||||
}
|
||||
|
||||
public void setLastUpdate(Date lastUpdate) {
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
public PlainContactWithJavaUtilDate() {
|
||||
}
|
||||
|
||||
public PlainContactWithJavaUtilDate(String name, String address, String phone, Date birthday, Date lastUpdate) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.phone = phone;
|
||||
this.birthday = birthday;
|
||||
this.lastUpdate = lastUpdate;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
public class CustomException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomException() {
|
||||
super("Custom exception message.");
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ErrorHandler {
|
||||
|
||||
@ExceptionHandler(CustomException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public CustomException handleCustomException(CustomException ce) {
|
||||
return ce;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
|
||||
@GetMapping("/")
|
||||
public void index() throws CustomException {
|
||||
throw new CustomException();
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.propertyeditor;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PropertyEditorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PropertyEditorApplication.class, args);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.propertyeditor;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.propertyeditor.creditcard.CreditCard;
|
||||
import com.baeldung.propertyeditor.exotictype.editor.CustomExoticTypeEditor;
|
||||
import com.baeldung.propertyeditor.exotictype.model.ExoticType;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/property-editor")
|
||||
public class PropertyEditorRestController {
|
||||
|
||||
@GetMapping(value = "/credit-card/{card-no}",
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public CreditCard parseCreditCardNumber(@PathVariable("card-no") CreditCard creditCard) {
|
||||
return creditCard;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/exotic-type/{value}",
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public ExoticType parseExoticType(@PathVariable("value") ExoticType exoticType) {
|
||||
return exoticType;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder binder) {
|
||||
binder.registerCustomEditor(ExoticType.class, new CustomExoticTypeEditor());
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.propertyeditor.creditcard;
|
||||
|
||||
public class CreditCard {
|
||||
|
||||
private String rawCardNumber;
|
||||
|
||||
private Integer bankIdNo;
|
||||
|
||||
private Integer accountNo;
|
||||
|
||||
private Integer checkCode;
|
||||
|
||||
public String getRawCardNumber() {
|
||||
return rawCardNumber;
|
||||
}
|
||||
public void setRawCardNumber(String rawCardNumber) {
|
||||
this.rawCardNumber = rawCardNumber;
|
||||
}
|
||||
|
||||
public Integer getBankIdNo() {
|
||||
return bankIdNo;
|
||||
}
|
||||
public void setBankIdNo(Integer bankIdNo) {
|
||||
this.bankIdNo = bankIdNo;
|
||||
}
|
||||
|
||||
public Integer getAccountNo() {
|
||||
return accountNo;
|
||||
}
|
||||
public void setAccountNo(Integer accountNo) {
|
||||
this.accountNo = accountNo;
|
||||
}
|
||||
|
||||
public Integer getCheckCode() {
|
||||
return checkCode;
|
||||
}
|
||||
public void setCheckCode(Integer checkCode) {
|
||||
this.checkCode = checkCode;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.propertyeditor.creditcard;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class CreditCardEditor extends PropertyEditorSupport {
|
||||
|
||||
@Override
|
||||
public String getAsText() {
|
||||
CreditCard creditCard = (CreditCard) getValue();
|
||||
|
||||
return creditCard == null ? "" : creditCard.getRawCardNumber();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
if (StringUtils.isEmpty(text)) {
|
||||
setValue(null);
|
||||
} else {
|
||||
CreditCard creditCard = new CreditCard();
|
||||
creditCard.setRawCardNumber(text);
|
||||
|
||||
String cardNo = text.replaceAll("-", "");
|
||||
if (cardNo.length() != 16)
|
||||
throw new IllegalArgumentException("Credit card format should be xxxx-xxxx-xxxx-xxxx");
|
||||
|
||||
try {
|
||||
creditCard.setBankIdNo( Integer.valueOf(cardNo.substring(0, 6)) );
|
||||
creditCard.setAccountNo( Integer.valueOf(cardNo.substring(6, cardNo.length() - 1)) );
|
||||
creditCard.setCheckCode( Integer.valueOf(cardNo.substring(cardNo.length() - 1)) );
|
||||
} catch (NumberFormatException nfe) {
|
||||
throw new IllegalArgumentException(nfe);
|
||||
}
|
||||
|
||||
setValue(creditCard);
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.propertyeditor.exotictype.editor;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
|
||||
import com.baeldung.propertyeditor.exotictype.model.ExoticType;
|
||||
|
||||
public class CustomExoticTypeEditor extends PropertyEditorSupport {
|
||||
|
||||
@Override
|
||||
public String getAsText() {
|
||||
ExoticType exoticType = (ExoticType) getValue();
|
||||
|
||||
return exoticType == null ? "" : exoticType.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
ExoticType exoticType = new ExoticType();
|
||||
exoticType.setName(text.toUpperCase());
|
||||
|
||||
setValue(exoticType);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.propertyeditor.exotictype.model;
|
||||
|
||||
public class ExoticType {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
|
||||
spring.jackson.time-zone=Europe/Zagreb
|
||||
spring.h2.console.path=/h2
|
||||
spring.h2.console.enabled=true
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
javers.mappingStyle=FIELD
|
||||
javers.algorithm=SIMPLE
|
||||
javers.commitIdGenerator=synchronized_sequence
|
||||
javers.prettyPrint=true
|
||||
javers.typeSafeValues=false
|
||||
javers.newObjectSnapshot=true
|
||||
javers.packagesToScan=
|
||||
javers.auditableAspectEnabled=true
|
||||
javers.springDataAuditableRepositoryAspectEnabled=true
|
||||
javers.sqlSchema=
|
||||
javers.sqlSchemaManagementEnabled=true
|
||||
javers.prettyPrintDateFormats.localDateTime=dd MMM yyyy, HH:mm:ss
|
||||
javers.prettyPrintDateFormats.zonedDateTime=dd MMM yyyy, HH:mm:ssZ
|
||||
javers.prettyPrintDateFormats.localDate=dd MMM yyyy
|
||||
javers.prettyPrintDateFormats.localTime=HH:mm:ss
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.disableautoconfig;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringDataJPA.class)
|
||||
public class SpringDataJPAIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
|
||||
context.getBean(DataSource.class);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.disableautoconfig;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringDataMongoDB.class)
|
||||
public class SpringDataMongoDBIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
|
||||
context.getBean(MongoTemplate.class);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.disableautoconfig;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringDataRedis.class)
|
||||
public class SpringDataRedisIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
|
||||
context.getBean(RedisTemplate.class);
|
||||
}
|
||||
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContactApp.class)
|
||||
@TestPropertySource(properties = {
|
||||
"spring.jackson.date-format=yyyy-MM-dd HH:mm:ss"
|
||||
})
|
||||
public class ContactAppIntegrationTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenJsonFormatAnnotationAndJava8DateType_whenGet_thenReturnExpectedDateFormat() throws IOException, ParseException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/contacts", String.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
|
||||
List<Map<String, String>> respMap = mapper.readValue(response.getBody(), new TypeReference<List<Map<String, String>>>(){});
|
||||
|
||||
LocalDate birthdayDate = LocalDate.parse(respMap.get(0).get("birthday"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
LocalDateTime lastUpdateTime = LocalDateTime.parse(respMap.get(0).get("lastUpdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
assertNotNull(birthdayDate);
|
||||
assertNotNull(lastUpdateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonFormatAnnotationAndLegacyDateType_whenGet_thenReturnExpectedDateFormat() throws IOException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/contacts/javaUtilDate", String.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
|
||||
List<Map<String, String>> respMap = mapper.readValue(response.getBody(), new TypeReference<List<Map<String, String>>>(){});
|
||||
|
||||
LocalDate birthdayDate = LocalDate.parse(respMap.get(0).get("birthday"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
LocalDateTime lastUpdateTime = LocalDateTime.parse(respMap.get(0).get("lastUpdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
assertNotNull(birthdayDate);
|
||||
assertNotNull(lastUpdateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDefaultDateFormatInAppPropertiesAndLegacyDateType_whenGet_thenReturnExpectedDateFormat() throws IOException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/contacts/plainWithJavaUtilDate", String.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
|
||||
List<Map<String, String>> respMap = mapper.readValue(response.getBody(), new TypeReference<List<Map<String, String>>>(){});
|
||||
|
||||
LocalDate birthdayDate = LocalDate.parse(respMap.get(0).get("birthday"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
LocalDateTime lastUpdateTime = LocalDateTime.parse(respMap.get(0).get("lastUpdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
assertNotNull(birthdayDate);
|
||||
assertNotNull(lastUpdateTime);
|
||||
}
|
||||
|
||||
@Test(expected = DateTimeParseException.class)
|
||||
public void givenDefaultDateFormatInAppPropertiesAndJava8DateType_whenGet_thenNotApplyFormat() throws IOException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/contacts/plain", String.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
|
||||
List<Map<String, String>> respMap = mapper.readValue(response.getBody(), new TypeReference<List<Map<String, String>>>(){});
|
||||
|
||||
LocalDate birthdayDate = LocalDate.parse(respMap.get(0).get("birthday"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
LocalDateTime lastUpdateTime = LocalDateTime.parse(respMap.get(0).get("lastUpdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.jsondateformat;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContactApp.class)
|
||||
public class ContactAppWithObjectMapperCustomizerIntegrationTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenDefaultDateFormatInAppPropertiesAndLegacyDateType_whenGet_thenReturnExpectedDateFormat() throws IOException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + this.port + "/contacts/plainWithJavaUtilDate", String.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
|
||||
List<Map<String, String>> respMap = mapper.readValue(response.getBody(), new TypeReference<List<Map<String, String>>>(){});
|
||||
|
||||
LocalDate birthdayDate = LocalDate.parse(respMap.get(0).get("birthday"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
LocalDateTime lastUpdateTime = LocalDateTime.parse(respMap.get(0).get("lastUpdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
assertNotNull(birthdayDate);
|
||||
assertNotNull(lastUpdateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDefaultDateFormatInAppPropertiesAndJava8DateType_whenGet_thenReturnExpectedDateFormat() throws IOException {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + this.port + "/contacts/plain", String.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
|
||||
List<Map<String, String>> respMap = mapper.readValue(response.getBody(), new TypeReference<List<Map<String, String>>>(){});
|
||||
|
||||
LocalDate birthdayDate = LocalDate.parse(respMap.get(0).get("birthday"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
LocalDateTime lastUpdateTime = LocalDateTime.parse(respMap.get(0).get("lastUpdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
assertNotNull(birthdayDate);
|
||||
assertNotNull(lastUpdateTime);
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jsonexception.CustomException;
|
||||
import com.baeldung.jsonexception.MainController;
|
||||
|
||||
public class MainControllerIntegrationTest {
|
||||
|
||||
@Test(expected = CustomException.class)
|
||||
public void givenIndex_thenCustomException() throws CustomException {
|
||||
|
||||
MainController mainController = new MainController();
|
||||
|
||||
mainController.index();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.propertyeditor.creditcard;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CreditCardEditorUnitTest {
|
||||
|
||||
private CreditCardEditor creditCardEditor;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
creditCardEditor = new CreditCardEditor();
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void whenInvalidCardNoWithLessDigits_thenThrowsException() {
|
||||
creditCardEditor.setAsText("123-123-123-123");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void whenInvalidCardNoWithNonDigits_thenThrowsException() {
|
||||
creditCardEditor.setAsText("1234-1234-xxxx-yyyy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCardNoWithNonDigits_parseCreditCard() {
|
||||
creditCardEditor.setAsText("1234-5678-9123-4560");
|
||||
|
||||
CreditCard creditCard = (CreditCard) creditCardEditor.getValue();
|
||||
Assert.assertNotNull(creditCard);
|
||||
|
||||
Assert.assertEquals(123456, creditCard.getBankIdNo().intValue());
|
||||
Assert.assertEquals(789123456, creditCard.getAccountNo().intValue());
|
||||
Assert.assertEquals(0, creditCard.getCheckCode().intValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
## Spring Boot Properties
|
||||
|
||||
This module contains articles about Properties in Spring Boot.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Reloading Properties Files in Spring](https://www.baeldung.com/spring-reloading-properties)
|
||||
- [Guide to @ConfigurationProperties in Spring Boot](https://www.baeldung.com/configuration-properties-in-spring-boot)
|
||||
- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties)
|
||||
- [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties)
|
||||
- [Properties with Spring and Spring Boot](https://www.baeldung.com/properties-with-spring) - checkout the `com.baeldung.properties` package for all scenarios of properties injection and usage
|
||||
- [A Quick Guide to Spring @Value](https://www.baeldung.com/spring-value-annotation)
|
||||
- [Spring YAML Configuration](https://www.baeldung.com/spring-yaml)
|
||||
- [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults)
|
||||
- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class)
|
||||
@@ -0,0 +1 @@
|
||||
application.theme.color=blue
|
||||
@@ -0,0 +1 @@
|
||||
application.theme.background=red
|
||||
@@ -0,0 +1,111 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-properties</artifactId>
|
||||
<name>spring-boot-properties</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring Boot Properties Module</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-configuration</groupId>
|
||||
<artifactId>commons-configuration</artifactId>
|
||||
<version>${commons-configuration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${httpcore.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<finalName>spring-boot-properties</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
|
||||
<commons-configuration.version>1.10</commons-configuration.version>
|
||||
<guava.version>20.0</guava.version>
|
||||
<httpcore.version>4.4.11</httpcore.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:configprops.properties")
|
||||
@ConfigurationProperties(prefix = "mail")
|
||||
@Validated
|
||||
public class ConfigProperties {
|
||||
|
||||
@Validated
|
||||
public static class Credentials {
|
||||
|
||||
@Length(max = 4, min = 1)
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getAuthMethod() {
|
||||
return authMethod;
|
||||
}
|
||||
|
||||
public void setAuthMethod(String authMethod) {
|
||||
this.authMethod = authMethod;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
@NotBlank
|
||||
private String hostName;
|
||||
|
||||
@Min(1025)
|
||||
@Max(65536)
|
||||
private int port;
|
||||
|
||||
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
|
||||
private String from;
|
||||
|
||||
private Credentials credentials;
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public Credentials getCredentials() {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void setCredentials(Credentials credentials) {
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
||||
public List<String> getDefaultRecipients() {
|
||||
return defaultRecipients;
|
||||
}
|
||||
|
||||
public void setDefaultRecipients(List<String> defaultRecipients) {
|
||||
this.defaultRecipients = defaultRecipients;
|
||||
}
|
||||
|
||||
public Map<String, String> getAdditionalHeaders() {
|
||||
return additionalHeaders;
|
||||
}
|
||||
|
||||
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
|
||||
this.additionalHeaders = additionalHeaders;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "item")
|
||||
public Item item(){
|
||||
return new Item();
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
private double salary;
|
||||
|
||||
public Employee(String name, double salary) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationPropertiesBinding
|
||||
public class EmployeeConverter implements Converter<String, Employee> {
|
||||
|
||||
@Override
|
||||
public Employee convert(String from) {
|
||||
String[] data = from.split(",");
|
||||
return new Employee(data[0], Double.parseDouble(data[1]));
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
public class Item {
|
||||
|
||||
private String name;
|
||||
private int size;
|
||||
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String name, int size) {
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { PropertyConversion.class, EmployeeConverter.class })
|
||||
public class PropertiesConversionApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PropertiesConversionApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.convert.DataSizeUnit;
|
||||
import org.springframework.boot.convert.DurationUnit;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:conversion.properties")
|
||||
@ConfigurationProperties(prefix = "conversion")
|
||||
public class PropertyConversion {
|
||||
private Duration timeInDefaultUnit;
|
||||
|
||||
private Duration timeInNano;
|
||||
|
||||
@DurationUnit(ChronoUnit.DAYS)
|
||||
private Duration timeInDays;
|
||||
|
||||
private DataSize sizeInDefaultUnit;
|
||||
|
||||
private DataSize sizeInGB;
|
||||
|
||||
@DataSizeUnit(DataUnit.TERABYTES)
|
||||
private DataSize sizeInTB;
|
||||
|
||||
private Employee employee;
|
||||
|
||||
// Getters and setters
|
||||
|
||||
public Duration getTimeInDefaultUnit() {
|
||||
return timeInDefaultUnit;
|
||||
}
|
||||
|
||||
public void setTimeInDefaultUnit(Duration timeInDefaultUnit) {
|
||||
this.timeInDefaultUnit = timeInDefaultUnit;
|
||||
}
|
||||
|
||||
public Duration getTimeInNano() {
|
||||
return timeInNano;
|
||||
}
|
||||
|
||||
public void setTimeInNano(Duration timeInNano) {
|
||||
this.timeInNano = timeInNano;
|
||||
}
|
||||
|
||||
public Duration getTimeInDays() {
|
||||
return timeInDays;
|
||||
}
|
||||
|
||||
public void setTimeInDays(Duration timeInDays) {
|
||||
this.timeInDays = timeInDays;
|
||||
}
|
||||
|
||||
public DataSize getSizeInDefaultUnit() {
|
||||
return sizeInDefaultUnit;
|
||||
}
|
||||
|
||||
public void setSizeInDefaultUnit(DataSize sizeInDefaultUnit) {
|
||||
this.sizeInDefaultUnit = sizeInDefaultUnit;
|
||||
}
|
||||
|
||||
public DataSize getSizeInGB() {
|
||||
return sizeInGB;
|
||||
}
|
||||
|
||||
public void setSizeInGB(DataSize sizeInGB) {
|
||||
this.sizeInGB = sizeInGB;
|
||||
}
|
||||
|
||||
public DataSize getSizeInTB() {
|
||||
return sizeInTB;
|
||||
}
|
||||
|
||||
public void setSizeInTB(DataSize sizeInTB) {
|
||||
this.sizeInTB = sizeInTB;
|
||||
}
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employee;
|
||||
}
|
||||
|
||||
public void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(AdditionalProperties.class)
|
||||
public class AdditionalConfiguration {
|
||||
|
||||
@Autowired
|
||||
private AdditionalProperties additionalProperties;
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "additional")
|
||||
public class AdditionalProperties {
|
||||
|
||||
private String unit;
|
||||
private int max;
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public int getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void setMax(int max) {
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import com.baeldung.configurationproperties.ConfigProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "custom")
|
||||
public class CustomJsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private Person sender;
|
||||
|
||||
public static class Person {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public Person getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(Person sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
||||
@ConfigurationProperties
|
||||
public class JsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private List<String> topics;
|
||||
|
||||
private LinkedHashMap<String, ?> sender;
|
||||
|
||||
public LinkedHashMap<String, ?> getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(LinkedHashMap<String, ?> sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public List<String> getTopics() {
|
||||
return topics;
|
||||
}
|
||||
|
||||
public void setTopics(List<String> topics) {
|
||||
this.topics = topics;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
private final static String CUSTOM_PREFIX = "custom.";
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
try {
|
||||
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
Set<Map.Entry> set = readValue.entrySet();
|
||||
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
||||
for (PropertySource propertySource : propertySources) {
|
||||
configurableApplicationContext.getEnvironment()
|
||||
.getPropertySources()
|
||||
.addFirst(propertySource);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertEntrySet(Set<Map.Entry> entrySet, Optional<String> parentKey) {
|
||||
return entrySet.stream()
|
||||
.map((Map.Entry e) -> convertToPropertySourceList(e, parentKey))
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertToPropertySourceList(Map.Entry e, Optional<String> parentKey) {
|
||||
String key = parentKey.map(s -> s + ".")
|
||||
.orElse("") + (String) e.getKey();
|
||||
Object value = e.getValue();
|
||||
return covertToPropertySourceList(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static List<MapPropertySource> covertToPropertySourceList(String key, Object value) {
|
||||
if (value instanceof LinkedHashMap) {
|
||||
LinkedHashMap map = (LinkedHashMap) value;
|
||||
Set<Map.Entry> entrySet = map.entrySet();
|
||||
return convertEntrySet(entrySet, Optional.ofNullable(key));
|
||||
}
|
||||
String finalKey = CUSTOM_PREFIX + key;
|
||||
return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value)));
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.core.io.support.PropertySourceFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertySourceFactory implements PropertySourceFactory {
|
||||
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
return new MapPropertySource("json-property", readValue);
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.properties.core;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
public class ComponentInXmlUsingProperties implements InitializingBean {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Value("${key.something}")
|
||||
private String injectedProperty;
|
||||
|
||||
public ComponentInXmlUsingProperties(final String propertyValue) {
|
||||
super();
|
||||
|
||||
System.out.println("Constructor Injection - Property Value resolved to: " + propertyValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
|
||||
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.properties.core;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ComponentUsingProperties implements InitializingBean {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Value("${key.something}")
|
||||
private String injectedProperty;
|
||||
|
||||
public ComponentUsingProperties() {
|
||||
super();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
|
||||
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.properties.external;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.properties.core")
|
||||
@PropertySource("classpath:foo.properties")
|
||||
public class ExternalPropertiesWithJavaConfig {
|
||||
|
||||
public ExternalPropertiesWithJavaConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
@Bean
|
||||
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.properties.external;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:configForProperties.xml")
|
||||
@ComponentScan("org.baeldung.core")
|
||||
public class ExternalPropertiesWithXmlConfig {
|
||||
|
||||
public ExternalPropertiesWithXmlConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.properties.external;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:configForPropertiesOne.xml")
|
||||
@ComponentScan("org.baeldung.core")
|
||||
public class ExternalPropertiesWithXmlConfigOne {
|
||||
|
||||
public ExternalPropertiesWithXmlConfigOne() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.properties.external;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:basicConfigForPropertiesTwo.xml")
|
||||
public class ExternalPropertiesWithXmlConfigTwo {
|
||||
|
||||
public ExternalPropertiesWithXmlConfigTwo() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.properties.reloading;
|
||||
|
||||
import com.baeldung.properties.reloading.configs.ReloadableProperties;
|
||||
import java.io.File;
|
||||
import java.util.Properties;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootPropertiesApplication {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.config.location", matchIfMissing = false)
|
||||
public PropertiesConfiguration propertiesConfiguration(
|
||||
@Value("${spring.config.location}") String path,
|
||||
@Value("${spring.properties.refreshDelay}") long refreshDelay) throws Exception {
|
||||
String filePath = path.substring("file:".length());
|
||||
PropertiesConfiguration configuration = new PropertiesConfiguration(new File(filePath).getCanonicalPath());
|
||||
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
|
||||
fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
|
||||
configuration.setReloadingStrategy(fileChangedReloadingStrategy);
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(PropertiesConfiguration.class)
|
||||
@Primary
|
||||
public Properties properties(PropertiesConfiguration propertiesConfiguration) throws Exception {
|
||||
ReloadableProperties properties = new ReloadableProperties(propertiesConfiguration);
|
||||
return properties;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootPropertiesApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
public class PropertiesException extends RuntimeException {
|
||||
public PropertiesException() {
|
||||
}
|
||||
|
||||
public PropertiesException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.util.Properties;
|
||||
import javax.naming.OperationNotSupportedException;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
|
||||
public class ReloadableProperties extends Properties {
|
||||
private PropertiesConfiguration propertiesConfiguration;
|
||||
|
||||
public ReloadableProperties(PropertiesConfiguration propertiesConfiguration) throws IOException {
|
||||
super.load(new FileReader(propertiesConfiguration.getFile()));
|
||||
this.propertiesConfiguration = propertiesConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Object setProperty(String key, String value) {
|
||||
propertiesConfiguration.setProperty(key, value);
|
||||
return super.setProperty(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
String val = propertiesConfiguration.getString(key);
|
||||
super.setProperty(key, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key, String defaultValue) {
|
||||
String val = propertiesConfiguration.getString(key, defaultValue);
|
||||
super.setProperty(key, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void load(Reader reader) throws IOException {
|
||||
throw new PropertiesException(new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void load(InputStream inStream) throws IOException {
|
||||
throw new PropertiesException(new OperationNotSupportedException());
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class ReloadablePropertySource extends PropertySource {
|
||||
|
||||
PropertiesConfiguration propertiesConfiguration;
|
||||
|
||||
public ReloadablePropertySource(String name, PropertiesConfiguration propertiesConfiguration) {
|
||||
super(name);
|
||||
this.propertiesConfiguration = propertiesConfiguration;
|
||||
}
|
||||
|
||||
public ReloadablePropertySource(String name, String path) {
|
||||
super(StringUtils.isEmpty(name) ? path : name);
|
||||
try {
|
||||
this.propertiesConfiguration = new PropertiesConfiguration(path);
|
||||
FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
|
||||
strategy.setRefreshDelay(1000);
|
||||
this.propertiesConfiguration.setReloadingStrategy(strategy);
|
||||
} catch (Exception e) {
|
||||
throw new PropertiesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getProperty(String s) {
|
||||
return propertiesConfiguration.getProperty(s);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
|
||||
@Configuration
|
||||
public class ReloadablePropertySourceConfig {
|
||||
|
||||
private ConfigurableEnvironment env;
|
||||
|
||||
public ReloadablePropertySourceConfig(@Autowired ConfigurableEnvironment env) {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.config.location", matchIfMissing = false)
|
||||
public ReloadablePropertySource reloadablePropertySource(PropertiesConfiguration properties) {
|
||||
ReloadablePropertySource ret = new ReloadablePropertySource("dynamic", properties);
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
sources.addFirst(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.properties.reloading.configs;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.FileUrlResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.DefaultPropertySourceFactory;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
|
||||
public class ReloadablePropertySourceFactory extends DefaultPropertySourceFactory {
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException {
|
||||
Resource internal = encodedResource.getResource();
|
||||
if (internal instanceof FileSystemResource) {
|
||||
return new ReloadablePropertySource(s, ((FileSystemResource) internal).getPath());
|
||||
}
|
||||
if (internal instanceof FileUrlResource) {
|
||||
return new ReloadablePropertySource(s, ((FileUrlResource) internal)
|
||||
.getURL()
|
||||
.getPath());
|
||||
}
|
||||
return super.createPropertySource(s, encodedResource);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.properties.spring;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:foo.properties")
|
||||
public class BasicPropertiesWithJavaConfig {
|
||||
|
||||
public BasicPropertiesWithJavaConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.properties.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:foo.properties")
|
||||
@PropertySource("classpath:bar.properties")
|
||||
public class PropertiesWithJavaConfig {
|
||||
|
||||
public PropertiesWithJavaConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
@Bean
|
||||
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.properties.spring;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:bar.properties")
|
||||
public class PropertiesWithJavaConfigOther {
|
||||
|
||||
public PropertiesWithJavaConfigOther() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.properties.spring;
|
||||
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class PropertiesWithPlaceHolderConfigurer {
|
||||
|
||||
public PropertiesWithPlaceHolderConfigurer() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PropertyPlaceholderConfigurer propertyConfigurer() {
|
||||
final PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
|
||||
props.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_FALLBACK);
|
||||
return props;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
public class ClassNotManagedBySpring {
|
||||
|
||||
private String customVariable;
|
||||
private String anotherCustomVariable;
|
||||
|
||||
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
|
||||
this.customVariable = someInitialValue;
|
||||
this.anotherCustomVariable = anotherManagedValue;
|
||||
}
|
||||
|
||||
public String getCustomVariable() {
|
||||
return customVariable;
|
||||
}
|
||||
|
||||
public void setCustomVariable(String customVariable) {
|
||||
this.customVariable = customVariable;
|
||||
}
|
||||
|
||||
public String getAnotherCustomVariable() {
|
||||
return anotherCustomVariable;
|
||||
}
|
||||
|
||||
public void setAnotherCustomVariable(String anotherCustomVariable) {
|
||||
this.anotherCustomVariable = anotherCustomVariable;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class InitializerBean {
|
||||
|
||||
private String someInitialValue;
|
||||
private String anotherManagedValue;
|
||||
|
||||
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
|
||||
this.someInitialValue = someInitialValue;
|
||||
this.anotherManagedValue = anotherManagedValue;
|
||||
}
|
||||
|
||||
public ClassNotManagedBySpring initClass() {
|
||||
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
public class SomeBean {
|
||||
private int someValue;
|
||||
|
||||
public SomeBean(int someValue) {
|
||||
this.someValue = someValue;
|
||||
}
|
||||
|
||||
public int getSomeValue() {
|
||||
return someValue;
|
||||
}
|
||||
|
||||
public void setSomeValue(int someValue) {
|
||||
this.someValue = someValue;
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package com.baeldung.value;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@Configuration
|
||||
@PropertySource(name = "myProperties", value = "values.properties")
|
||||
public class ValuesApp {
|
||||
|
||||
@Value("string value")
|
||||
private String stringValue;
|
||||
|
||||
@Value("${value.from.file}")
|
||||
private String valueFromFile;
|
||||
|
||||
@Value("${systemValue}")
|
||||
private String systemValue;
|
||||
|
||||
@Value("${unknown_param:some default}")
|
||||
private String someDefault;
|
||||
|
||||
@Value("${priority}")
|
||||
private String prioritySystemProperty;
|
||||
|
||||
@Value("${listOfValues}")
|
||||
private String[] valuesArray;
|
||||
|
||||
@Value("#{systemProperties['priority']}")
|
||||
private String spelValue;
|
||||
|
||||
@Value("#{systemProperties['unknown'] ?: 'some default'}")
|
||||
private String spelSomeDefault;
|
||||
|
||||
@Value("#{someBean.someValue}")
|
||||
private Integer someBeanValue;
|
||||
|
||||
@Value("#{'${listOfValues}'.split(',')}")
|
||||
private List<String> valuesList;
|
||||
|
||||
@Value("#{${valuesMap}}")
|
||||
private Map<String, Integer> valuesMap;
|
||||
|
||||
@Value("#{${valuesMap}.key1}")
|
||||
private Integer valuesMapKey1;
|
||||
|
||||
@Value("#{${valuesMap}['unknownKey']}")
|
||||
private Integer unknownMapKey;
|
||||
|
||||
@Value("#{${unknownMap : {key1:'1', key2 : '2'}}}")
|
||||
private Map<String, Integer> unknownMap;
|
||||
|
||||
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
|
||||
private Integer unknownMapKeyWithDefaultValue;
|
||||
|
||||
@Value("#{${valuesMap}.?[value>'1']}")
|
||||
private Map<String, Integer> valuesMapFiltered;
|
||||
|
||||
@Value("#{systemProperties}")
|
||||
private Map<String, String> systemPropertiesMap;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("systemValue", "Some system parameter value");
|
||||
System.setProperty("priority", "System property");
|
||||
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesApp.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SomeBean someBean() {
|
||||
return new SomeBean(10);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void afterInitialize() {
|
||||
System.out.println(stringValue);
|
||||
System.out.println(valueFromFile);
|
||||
System.out.println(systemValue);
|
||||
System.out.println(someDefault);
|
||||
System.out.println(prioritySystemProperty);
|
||||
System.out.println(Arrays.toString(valuesArray));
|
||||
System.out.println(spelValue);
|
||||
System.out.println(spelSomeDefault);
|
||||
System.out.println(someBeanValue);
|
||||
System.out.println(valuesList);
|
||||
System.out.println(valuesMap);
|
||||
System.out.println(valuesMapKey1);
|
||||
System.out.println(unknownMapKey);
|
||||
System.out.println(unknownMap);
|
||||
System.out.println(unknownMapKeyWithDefaultValue);
|
||||
System.out.println(valuesMapFiltered);
|
||||
System.out.println(systemPropertiesMap);
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.valuewithdefaults;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
/**
|
||||
* Demonstrates setting defaults for @Value annotation. Note that there are no properties
|
||||
* defined in the specified property source. We also assume that the user here
|
||||
* does not have a system property named some.key.
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@PropertySource(name = "myProperties", value = "valueswithdefaults.properties")
|
||||
public class ValuesWithDefaultsApp {
|
||||
|
||||
@Value("${some.key:my default value}")
|
||||
private String stringWithDefaultValue;
|
||||
|
||||
@Value("${some.key:}")
|
||||
private String stringWithBlankDefaultValue;
|
||||
|
||||
@Value("${some.key:true}")
|
||||
private boolean booleanWithDefaultValue;
|
||||
|
||||
@Value("${some.key:42}")
|
||||
private int intWithDefaultValue;
|
||||
|
||||
@Value("${some.key:one,two,three}")
|
||||
private String[] stringArrayWithDefaults;
|
||||
|
||||
@Value("${some.key:1,2,3}")
|
||||
private int[] intArrayWithDefaults;
|
||||
|
||||
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
|
||||
private String spelWithDefaultValue;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void afterInitialize() {
|
||||
// strings
|
||||
Assert.isTrue(stringWithDefaultValue.equals("my default value"));
|
||||
Assert.isTrue(stringWithBlankDefaultValue.equals(""));
|
||||
|
||||
// other primitives
|
||||
Assert.isTrue(booleanWithDefaultValue);
|
||||
Assert.isTrue(intWithDefaultValue == 42);
|
||||
|
||||
// arrays
|
||||
List<String> stringListValues = Lists.newArrayList("one", "two", "three");
|
||||
Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues));
|
||||
|
||||
List<Integer> intListValues = Lists.newArrayList(1, 2, 3);
|
||||
Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues));
|
||||
|
||||
// SpEL
|
||||
Assert.isTrue(spelWithDefaultValue.equals("my default system property value"));
|
||||
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.baeldung.yaml;
|
||||
|
||||
import java.util.Collections;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MyApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private YAMLConfig myConfig;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication app = new SpringApplication(MyApplication.class);
|
||||
app.run();
|
||||
}
|
||||
|
||||
public void run(String... args) throws Exception {
|
||||
System.out.println("using environment:" + myConfig.getEnvironment());
|
||||
System.out.println("name:" + myConfig.getName());
|
||||
System.out.println("servers:" + myConfig.getServers());
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.yaml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties
|
||||
public class YAMLConfig {
|
||||
private String name;
|
||||
private String environment;
|
||||
private List<String> servers = new ArrayList<String>();
|
||||
|
||||
public List<String> getServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
public void setServers(List<String> servers) {
|
||||
this.servers = servers;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEnvironment() {
|
||||
return environment;
|
||||
}
|
||||
|
||||
public void setEnvironment(String environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
management.endpoints.web.exposure.include=refresh
|
||||
spring.properties.refreshDelay=1000
|
||||
spring.config.location=file:extra.properties
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -0,0 +1,17 @@
|
||||
spring:
|
||||
profiles: test
|
||||
name: test-YAML
|
||||
environment: test
|
||||
servers:
|
||||
- www.abc.test.com
|
||||
- www.xyz.test.com
|
||||
|
||||
---
|
||||
|
||||
spring:
|
||||
profiles: prod
|
||||
name: prod-YAML
|
||||
environment: production
|
||||
servers:
|
||||
- www.abc.com
|
||||
- www.xyz.com
|
||||
@@ -0,0 +1 @@
|
||||
key.something2=val2
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
|
||||
>
|
||||
|
||||
<context:property-placeholder location="classpath:foo.properties"/>
|
||||
|
||||
</beans>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
|
||||
>
|
||||
|
||||
<context:property-placeholder location="classpath:foo.properties" ignore-unresolvable="true" order="1"/>
|
||||
|
||||
</beans>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:bar.properties"
|
||||
order="2" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1 @@
|
||||
child.name=child
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
|
||||
>
|
||||
|
||||
<context:property-placeholder location="classpath:foo.properties,classpath:bar.properties"/>
|
||||
|
||||
<bean id="componentInXmlUsingProperties" class="com.baeldung.properties.core.ComponentInXmlUsingProperties">
|
||||
<constructor-arg value="${key.something}"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
|
||||
>
|
||||
|
||||
<context:property-placeholder location="classpath:foo.properties" ignore-unresolvable="true" order="1"/>
|
||||
|
||||
<bean id="componentInXmlUsingProperties" class="com.baeldung.properties.core.ComponentInXmlUsingProperties">
|
||||
<constructor-arg value="${key.something2}"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"host" : "mailer@mail.com",
|
||||
"port" : 9090,
|
||||
"resend" : true,
|
||||
"topics" : ["spring", "boot"],
|
||||
"sender" : {
|
||||
"name": "sender",
|
||||
"address": "street"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#Simple properties
|
||||
mail.hostname=host@mail.com
|
||||
mail.port=9000
|
||||
mail.from=mailer@mail.com
|
||||
|
||||
#List properties
|
||||
mail.defaultRecipients[0]=admin@mail.com
|
||||
mail.defaultRecipients[1]=owner@mail.com
|
||||
|
||||
#Map Properties
|
||||
mail.additionalHeaders.redelivery=true
|
||||
mail.additionalHeaders.secure=true
|
||||
mail.additionalHeaders.p3=value
|
||||
|
||||
#Object properties
|
||||
mail.credentials.username=john
|
||||
mail.credentials.password=password
|
||||
mail.credentials.authMethod=SHA1
|
||||
|
||||
#Bean method properties
|
||||
item.name=Item name
|
||||
item.size=42
|
||||
|
||||
|
||||
#Additional properties
|
||||
additional.unit=km
|
||||
additional.max=100
|
||||
@@ -0,0 +1,2 @@
|
||||
conversion.timeInDefaultUnit=10
|
||||
conversion.timeInNano=9ns
|
||||
@@ -0,0 +1 @@
|
||||
key.something=val
|
||||
@@ -0,0 +1 @@
|
||||
parent.name=parent
|
||||
@@ -0,0 +1,4 @@
|
||||
value.from.file=Value got from the file
|
||||
priority=Properties file
|
||||
listOfValues=A,B,C
|
||||
valuesMap={key1:'1', key2 : '2', key3 : '3'}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user