Spring Boot Profiles
1.

2. DemoApplication Class
package com.example.profiles;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext ctx= SpringApplication.run(DemoApplication.class, args);
}
}
1.
2. DemoApplication Class
package com.example.profiles;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext ctx= SpringApplication.run(DemoApplication.class, args);
}
}
3.PageController class
package com.example.profiles;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PageController {
@Value("${spring.profiles.active}")
private String environment;
@RequestMapping("/")
public String home(){
return environment;
}
}
4. application.properties
server.port=9009
spring.profiles.active=development
5. application-production.properties
msg=Hello from production properties
6. application-development.properties
msg=Hello from development properties
7 Run program
Browse http://localhost:9009/
--------------------------------------
8. Now Modify pageController
package com.example.profiles;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PageController {
@Value("${spring.profiles.active}")
private String environment;
@Value("${msg}")
private String msg;
@RequestMapping("/")
public String home(){
return msg;
}
}
9, Run program
Browse http://localhost:9009/
----------------------------------------
8. Change profile from command line
9. Run program
Browse http://localhost:9009/
The output will change into production
---------------------------------
10. Now remove command line arguments from Spring.
11. Application.yml
environments:
development:
url: http://dev.bar.com
name: Developer site
production: https://foo.bar.com
name: My Cool App
===========================================
12. DataSource class File
package com.example.profiles;
public class DataSource {
private String server;
private int port;
public DataSource(String server, int port) {
super();
this.server = server;
this.port = port;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public String toString() {
return "DataSource Result====> [server=" + server + ", port=" + port + "]";
}
}
13. DataSourceConfig class File
package com.example.profiles;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class DataSourceConfig {
@Profile("development")
@Bean(name="dataSource")
public DataSource development(){
return new DataSource("my-development-url", 9999);
}
@Profile("production")
@Bean(name="dataSource")
public DataSource production(){
return new DataSource("my-production-url", 9999);
}
}
14. package com.example.profiles;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext ctx= SpringApplication.run(DemoApplication.class, args);
String [] names=ctx.getBeanDefinitionNames();
Arrays.sort(names);
for(String name:names)
{
System.out.println(name);
}
System.out.println( ctx.getBean("dataSource").toString());
}
}
15. Run the program
Now the Result in Console.
DataSource Result====> [server=my-development-url, port=9999]
16 Now Change in application properties
server.port=9009
spring.profiles.active=production
17. Run the program
Now the Result in Console.
DataSource Result====> [server=my-production-url, port=9999]
Comments
Post a Comment