@@ -24,6 +24,22 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--Test -->
|
||||
<dependency>
|
||||
@@ -32,4 +48,5 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ConditionalBeanConfiguration {
|
||||
|
||||
@Conditional(HibernateCondition.class)
|
||||
Properties additionalProperties() {
|
||||
// application specific properties
|
||||
return new Properties();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class HealthCheckController {
|
||||
|
||||
private static final String STATUS = "UP";
|
||||
|
||||
@GetMapping("/health")
|
||||
public ResponseEntity<String> healthCheck() {
|
||||
return ResponseEntity.ok(STATUS);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class HibernateCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
// application specific condition check
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(DataSource.class)
|
||||
public class MySQLAutoconfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(name = "dataSource")
|
||||
LocalContainerEntityManagerFactoryBean entityManagerFactory() throws IOException {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||
|
||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "usemysql", havingValue = "local")
|
||||
DataSource dataSource() {
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
|
||||
dataSource.setUrl("jdbc:hsqldb:mem:testdb");
|
||||
dataSource.setUsername( "sa" );
|
||||
dataSource.setPassword( "" );
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@ConditionalOnResource(resources = "classpath:mysql.properties")
|
||||
Properties additionalProperties() throws IOException {
|
||||
final Properties additionalProperties = new Properties();
|
||||
|
||||
try (InputStream inputStream = new ClassPathResource("classpath:mysql.properties").getInputStream()) {
|
||||
additionalProperties.load(inputStream);
|
||||
}
|
||||
|
||||
return additionalProperties;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class WebApplicationSpecificConfiguration {
|
||||
|
||||
@ConditionalOnWebApplication
|
||||
HealthCheckController healthCheckController() {
|
||||
return new HealthCheckController();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.springframework.context.annotation.ComponentScan;
|
||||
import com.baeldung.configurationproperties.ConfigProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class, Database.class })
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "database")
|
||||
public class Database {
|
||||
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+5
-1
@@ -1,4 +1,8 @@
|
||||
management.endpoints.web.exposure.include=refresh
|
||||
spring.properties.refreshDelay=1000
|
||||
spring.config.location=file:extra.properties
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
database.url=jdbc:postgresql:/localhost:5432/instance
|
||||
database.username=foo
|
||||
database.password=bar
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.configurationproperties;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
import com.baeldung.properties.Database;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
|
||||
@TestPropertySource("classpath:application.properties")
|
||||
public class DatabasePropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Database database;
|
||||
|
||||
@Test
|
||||
public void testDatabaseProperties() {
|
||||
Assert.assertNotNull(database);
|
||||
Assert.assertEquals("jdbc:postgresql:/localhost:5432/instance", database.getUrl());
|
||||
Assert.assertEquals("foo", database.getUsername());
|
||||
Assert.assertEquals("bar", database.getPassword());
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -1,4 +1,8 @@
|
||||
management.endpoints.web.exposure.include=refresh
|
||||
spring.properties.refreshDelay=1000
|
||||
spring.config.location=file:extra.properties
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
database.url=jdbc:postgresql:/localhost:5432/instance
|
||||
database.username=foo
|
||||
database.password=bar
|
||||
Reference in New Issue
Block a user