Posts

Application error pages

Spring Boot offers this “whitelabel” error page by default as part of auto-configuration. The default error handler that’s auto-configured by Spring Boot looks for a view whose name is “error”. If it can’t find one, it uses its default whitelabel error view. By default, Spring Boot makes the following error attributes available to the error view: timestamp —The time that the error occurred status —The HTTP status code error —The error reason exception —The class name of the exception message —The exception message (if the error was caused by an exception) errors —Any errors from a BindingResult exception (if the error was caused by an exception) trace —The exception stack trace (if the error was caused by an exception) path —The URL path requested when the error occurred SpringJUnit4ClassRunner , a JU nit class runner that loads a Spring application con text for use in a JU nit test and enables autowiring of beans into the test class.

Configuring with profiles

Based on the environment some configuration gets changed. A profile based configuration can be activated for the respective environment using the @profile. Example: @Profile("production") @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { } The @Profile annotation used here requires that the “production” profile be active at runtime for this SecurityConfig configuration to be applied. If the “production” profile isn’t active, this configuration will be ignored Profiles can be activated by setting the spring.profiles.active property using any of the means available for setting any other configuration property PROFILE-SPECIFIC PROPERTIES FILES you can provide profile-specific properties by creating additional properties files named with the pattern “application-{profile}.properties”. For the logging scenario, the development configuration would be in a file named application-development.properties and contain properties for ve...

Capturing configuration properties in a bean

    application.properties: amazon.associateId=habuma-20 Code: import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("amazon") public class AmazonProperties { private String associateId; public void setAssociateId(String associateId) { this.associateId = associateId; } public String getAssociateId() { return associateId; } }

Configuring a data source

MySQL database, your application.yml file: Spring: datasource: url: jdbc:mysql://localhost/readinglist username: dbuser password: dbpass No need to specify the JDBC driver; Spring Boot can figure it out from the database URL. But if there is a problem, you can try setting the spring.datasource .driver-class-name property:  driver-class-name: com.mysql.jdbc.Driver The DataSource bean will be pooled, using Tomcat’s pooling DataSource. it will look for and use one of these other connection If tomcat pooling lib not available then it will look for following libs for pool implementations on the classpath: ■ HikariCP ■ Commons DBCP ■ Commons DBCP 2 DataSource from JNDI spring: datasource: jndi-name: java:/comp/env/jdbc/readingListDS If you set the spring.datasource.jndi-name property, the other datasource connection properties (if set) will be ignored

Logging

Logging application.yml  Logging: path: /var/logs/ file: BookWorm.log Level: root: WARN Org: Springframework: security: DEBUG Assuming that the application has write permissions to /var/logs/, the log entries will be written to /var/logs/BookWorm.log. By default, the log files will rotate once they hit 10 megabytes in size. Similarly, all of these properties can be set in application.properties like this: logging.path=/var/logs/ logging.file=BookWorm.log logging.level.root=WARN logging.level.root.org.springframework.security=DEBUG

Properties for a Spring Boot App

There are, in fact, several ways to set properties for a Spring Boot application. Spring Boot will draw properties from several property sources, including the following: Command-line arguments JNDI attributes from java:comp/env JVM system properties Operating system environment variables Randomly generated values for properties prefixed with random.* (referenced when setting other  properties, such as `${random.long}) An application.properties or application.yml file outside of the application An application.properties or application.yml file packaged inside of the application Property sources specified by @PropertySource Default properties

About @Conditional

  @Conditional(JdbcTemplateCondition.class) public MyService myService() { } In this case, the MyService bean will only be created if the JdbcTemplateCondition passes. That is to say that the MyService bean will only be created if JdbcTemplate is available on the classpath. Otherwise, the bean declaration will be ignored. Spring Boot defines several more interesting conditions and applies them to the configuration classes that make up Spring Boot auto-configuration. Spring Boot applies conditional configuration by defining several special conditional annotations and using them in its configuration classes. @ConditionalOnBean : the specified bean has been configured @ConditionalOnMissingBean : the specified bean has not already been configured @ConditionalOnClass : the specified class is available on the classpath @ConditionalOnMissingClass :the specified class is not available on the classpath @ConditionalOnExpression : the given Spring Expression Language (SpEL) expression evaluat...