Spring Boot

Spring Boot:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". 
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. 
Most Spring Boot applications need very little Spring configuration.

Features:-
  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated 'starter' POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration
Advantages of using Spring Boot:-
  • It is very easy to develop Spring Based applications with Java
  • It avoids writing lots of boilerplate Code, Annotations and XML Configuration. It reduces lots of development time and increases productivity
  • It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
  • It follows "Opinionated Defaults Configuration" Approach to reduce Developer effort
  • It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  • It provides CLI (Command Line Interface) tool to develop and test Spring Boot (Java or Groovy) Applications from command prompt very easily and quickly.
  • It provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle
  • It provides lots of plugins to work with embedded and in-memory Databases very easily.

Opinionated Defaults Configuration:-
Opinionated frameworks provide a "golden path", 
Which is supposed to be the best practice for most people and most scenarios (in the eyes of the authors).
This however doesn't necessarily mean lock-in. It means that it may require some extra effort to do things differently.
Less opinionated frameworks provide a number of different options and leave it up to you to decide.
Opinionated frameworks usually remove the burden from developer to reinvent the wheel or rethink the same problem again and again and thus help focus on the real problem at hand.
In the open-source world you can find many opinionated yet competing frameworks, 
So you still have a choice. You just have to choose your own golden path.
Configuration file:-
The configuration file used in spring boot projects is application.properties. 
This file is very important where we would over write all the default configurations

@SpringBootApplication was available from Spring Boot 1.2:-
It is very common to use @EnableAutoConfiguration, @Configuration, and @ComponentScan together.
insted of @EnableAutoConfiguration, @Configuration and @ComponentScan we can use @SpringBootApplication

Spring Boot can control the logging level:-
– Just set it in application.properties
• Works with most logging frameworks
– Java Util Logging, Logback, Log4J, Log4J2
logging.level.org.springframework=DEBUG
logging.level.com.acme.your.code=INFO
Custom port:-
In application.properties, add following property.
server.port = 8181

datasource using Spring boot:-
• Use either spring-boot-starter-jdbc or spring-boot-starterdata-jpa and include a JDBC driver on classpath
• Declare properties
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Implement Spring web using Spring boot:-
Web Application Convenience 
• Boot automatically configures
– A DispatcherServlet & ContextLoaderListener
– Spring MVC using same defaults as @EnableWebMvc
• Plus many useful extra features:
– Static resources served from classpath
• /static, /public, /resources or /META-INF/resources
– Templates served from /templates
• If Velocity, Freemarker, Thymeleaf, or Groovy on classpath
– Provides default /error mapping
• Easily overridden
– Default MessageSource for I18N

YAML:-
Yaml Ain't a Markup Language
– Recursive acronym
• Created in 2001
• Alternative to .properties files
– Allows hierarchical configuration
• Java parser for YAML is called SnakeYAML
– Must be in the classpath
– Provided by spring-boot-starters

application.properties
database.host = localhost
database.user = admin

application.yml
database:
    host: localhost
    user: admin



//@EnableEurekaClient
//@EnableCircuitBreaker
@SpringBootApplication
@ComponentScan(basePackages = {"com.chh"})
@EnableSwagger2
public class SampleApplication {
    public static void main(String... args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

@RestController
@RequestMapping("/test")
public class TestController{
    @Autowired
    private EmpService          empService;

 @RequestMapping(value = "/emp/{id}", method = RequestMethod.GET, produces =    MediaType.APPLICATION_JSON_VALUE)
    public GenericResponse<Object> getPatientMeddocHcpc(@PathVariable String id)    {
        return empService.getEmp(id);
    }

    @RequestMapping(value = "/emp", method = RequestMethod.POST, produces =  MediaType.APPLICATION_JSON_VALUE)
    public GenericResponse<String> createEmp(         @RequestBody Emp emp)  {
        return new GenericResponse();
    }
}

@Repository
public interface EmpRepository  extends JpaRepository<Emp, UUID>{
}


Questions:
1. diffrance between @Controler and @ControlerAdvice


Comments

Popular posts from this blog

Spring Boot + Maven + RestFul + JPA + Sqlite

View & Materialized View

JDBC with Sqlite