Spring Boot

 

Spring boot profiles :


What is @SpringBootApplication Annotation


Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single @SpringBootApplication annotation can be used to enable those three features, that is:

The @SpringBootApplication annotation is equivalent to using @Configuration@EnableAutoConfiguration, and @ComponentScan with their default attributes,


Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It’s same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.


SpringBootApplication scanBasePackages

By default SpringApplication scans the configuration class package and all it’s sub-pacakges. So if our SpringBootRestApplication class is in com.journaldev.spring.main package, then it won’t scan com.journaldev.spring.controller package. We can fix this situation using SpringBootApplication scanBasePackages property.


@SpringBootApplication(scanBasePackages="com.journaldev.spring")
public class SpringBootRestApplication {
}
========================================================================
application.yaml

#Used for all profiles 
spring:
  profiles:
    active: "dev"

#==========='dev' profile only
---

server:
   port: 8094
spring:
   profiles: dev
   datasource:
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
   jpa:
      database-platform: org.hibernate.dialect.MySQL5Dialect
      hibernate:
         ddl-auto: update

#==========='test' profile only
---
server:
   port: 8094
spring:
   profiles: test
   datasource:
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
   jpa:
      database-platform: org.hibernate.dialect.MySQL5Dialect
      hibernate:
         ddl-auto: update         
         
#==========='prod' profile only
---
server:
   port: 8094
spring:
   profiles: prod
   datasource:
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
   jpa:
      database-platform: org.hibernate.dialect.MySQL5Dialect
      hibernate:
         ddl-auto: update         

===================================================

Running in Eclipse : 

Set Environment Variable - spring.profiles.active=dev


create Jar File :

mvn package -DskipTests


Running Jar file command -

java -jar -Dspring.profiles.active=dev app-0.0.1-SNAPSHOT.jar

No comments:

Post a Comment