Posts

Conditional configuration

When you add Spring Boot to your application, there’s a JAR file named springboot-autoconfigure that contains several configuration classes. Every one of these configuration classes is available on the application’s classpath and has the opportunity to contribute to the configuration of your application. Conditional configuration allows for configuration to be available in an application, but to be ignored unless certain conditions are met.  It’s easy enough to write your own conditions in Spring. All you have to do is implement the Condition interface and override its matches() method. For example, the following simple condition class will only pass if JdbcTemplate is available on the classpath: import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class JdbcTemplateCondition implements Condition { @Override public boolean matches(ConditionContext c...

Spring Boot auto-configuration

    Spring Boot auto-configuration is a runtime (more accurately, application startup-time) process that considers several factors to decide what Spring configuration should and should not be applied. To illustrate, here are a few examples of the  kinds of things that Spring Boot auto-configuration might consider: Is Spring’s JdbcTemplate available on the classpath? If so and if there is a DataSource bean, then auto-configure a JdbcTemplate bean. Is Thymeleaf on the classpath? If so, then configure a Thymeleaf template resolver, view resolver, and template engine. Is Spring Security on the classpath? If so, then configure a very basic web security setup. There are nearly 200 such decisions that Spring Boot makes with regard to autoconfiguration every time an application starts up, covering such areas as security, integration, persistence, and web development. By extending JpaRepository, XyzRepository interface inherits 18 methods for performing common persistence operatio...

About dependency

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>{springBootVersion}</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <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> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactI...

Spring Boot configuration detail

  Spring Boot can automatically configure common configuration scenarios. If Spring Boot detects that you have the H2 database library in your application’s classpath, it will automatically configure an embedded H2 database. If JdbcTemplate is in the classpath, then it will also configure a JdbcTemplate bean for you. There’s no need for you to worry about configuring those beans. They’ll be configured for you, ready to inject into any of the beans you write. Spring Boot offers help with project dependency management by way of starter dependencies. Starter dependencies are really just special Maven (and Gradle) dependencies that take advantage of transitive dependency resolution to aggregate commonly used libraries under a handful of feature-defined dependencies. Spring Boot can take the burden of configuration off your hands, including auto-configuration for the Java Persistence API (JPA), Thymeleaf templates, security, and Spring MVC. In a typical Spring integration test, you’d an...

About @SpringBootApplication and more

    @SpringBootApplication is a convenience annotation that adds all of the following: @Configuration: Tags the class as a source of bean definitions for the application context. @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet. @ComponentScan:   Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers

About @RestController

@RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view.  It is shorthand for including both @Controller and @ResponseBody. A key difference between a traditional MVC controller and the RESTful web service controller shown earlier is the way that the HTTP response body is created. Rather than relying on a view technology to perform server-side rendering of the greeting data to HTML, this RESTful web service controller populates and returns a Greeting object. The object data will be written directly to the HTTP response as JSON

About @RequestParam

It binds the value of the query string parameter name into the name parameter of the method. If the name parameter is absent in the request, the defaultValue of the is used. by default required is true for the parameter. Example1 : request parameter name must be same is argument parameter name. (i.e. id) @GetMapping("/get/data") @ResponseBody public String getData ( @RequestParam String id) { return "ID is: " + id; } Example2- name : If request parameter is different than the argument parameter name then  we can specify the name of parameter in the name attribute. i.e. name="id" we can also write like this :  @RequestParam("id"). @GetMapping("/set/data") @ResponseBody public String setData ( @RequestParam(name = "id") String uid) { return "UID: " + uid ; } Example3- required : B y default required is true. So, if parameter will not be present in the request it will give below error. 400 Bad ...