visit
GitHub Repository
[Spring Boot]
@SpringBootApplication
Well, this might be surprising. @SpringBootApplication is actually a combination of three features or annotations. In other words, it has the effect of 3 annotations together : @ComponentScan, @Configuration, @EnableAutoConfiguration
package xyz.sumithpuri.spring.boot.annotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;
@SpringBootApplication
public class SpringBootAnnotationApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAnnotationApplication.class, args);
}
@Bean
public SBASampleInterface getSBAService() {
return new SBASampleImpl();
}
}
@EnableAutoConfiguration
So, Spring allows the automatic configuration of the application, by creating and registering the spring beans in the classpath. The @EnableAutoConfiguration allows to define the base search package. By default, the base package for searching of beans will be the same package as of the class that declares this annotation.
package xyz.sumithpuri.spring.boot.annotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;
//@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackages = "xyz.sumithpuri.spring.boot.annotation")
public class SpringBootAnnotationApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAnnotationApplication.class, args);
}
@Bean
public SBASampleInterface getSBAService() {
return new SBASampleImpl();
}
}
@SpringBootTest
This one is straightforward, @SpringBootTest is used to create an application context object that supports testing.
package xyz.sumithpuri.spring.boot.annotation;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringBootAnnotationApplicationTests {
@Test
void contextLoads() {
}
}
@SpringBootConfiguration
Even though I have not used it much in my applications, from what I could gather I have found it is already part of the @SpringBootApplication. The only difference between @Configuration and @SpringBootConfiguration is that latter allows to automatically locate the configuration. This will be useful for unit and integration tests.@ConditionalOnClass
Will match only when the specified classes are in the classpath.
package xyz.sumithpuri.spring.boot.annotation.configuration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;
/**
* @author sumith.puri
*
*/
@Configuration
@ConditionalOnClass(SBASampleImpl.class)
public class SBASampleConfiguration {
@Bean
public SBASampleInterface getSBAService() {
return new SBASampleImpl();
}
}
@ConditionalOnProperty
Will match only when the specified environment property is present and it has a specific value.
package xyz.sumithpuri.spring.boot.annotation.configuration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleImpl;
import xyz.sumithpuri.spring.boot.annotation.service.SBASampleInterface;
/**
* @author sumith.puri
*
*/
@Configuration
//@ConditionalOnClass(SBASampleImpl.class)
@ConditionalOnProperty(name="mode", havingValue="false")
public class SBASampleConfiguration {
@Bean
public SBASampleInterface getSBAService() {
return new SBASampleImpl();
}
}
With Spring DevTools Enabled, you will see one such log on the console that are the debug statements showing the matches or evaluations against the conditions.
@ConfigurationProperties
@ConfigurationPropertiesScan
It marks a class as a configuration properties source (mapping it from a properties or yaml file), which can then be used to control and also to validate properties. ConfigurationPropertiesScan can be used to scan locations for property files. The location can be specified as the parameter to the annotation.
package xyz.sumithpuri.spring.boot.annotation.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.stereotype.Component;
/**
* @author sumith.puri
*
*/
@Component
@ConfigurationProperties(prefix = "proptest")
@ConfigurationPropertiesScan
public class SBASampleConfigurationProperties {
private String name;
private String pass;
private String mail;
private String year;
private long uuid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
..... // Getter and Setter Methods
[Typical Properties File to be Read By ConfigurationProperties]
**[Debug Print Messages on Invocation of a Controller Endpoint] \