How to switch spring profile at runtime - java

I have spring boot application with profiles. Now I want to switch profile at runtime, refresh spring context and continue application execution. How to switch active profile at runtime (switchEnvironment method)?
#SpringBootApplication
public class Application implements CommandLineRunner {
#Autowired
private Config config;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void run(String ... strings) throws Exception {
System.out.printf("Application is running in %s environment, service parameters below:\n",
getEnvProperty("spring.profiles.active").toUpperCase());
printServiceParameters();
switchEnvironment();
printServiceParameters();
}
private String getEnvProperty(String propertyName) {
return config.getEnv().getProperty(propertyName);
}
private void printServiceParameters() {
System.out.println(getEnvProperty("service.endpoint"));
}
private void switchEnvironment() {
//todo Switch active profile
}
}
Config.class
#Configuration
#ConfigurationProperties
public class Config{
#Autowired
private ConfigurableEnvironment env;
public ConfigurableEnvironment getEnv() {
return env;
}
public void setEnv(ConfigurableEnvironment env) {
this.env = env;
}
}

All what you need, it's add this method into your main class, and create Controller or Service for call this method.
#SpringBootApplication
public class Application {
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
context = SpringApplication.run(Application.class, args);
}
public static void restart() {
Thread thread = new Thread(() -> {
context.close();
context = SpringApplication.run(Application.class, "--spring.profiles.active=your_profile");
});
thread.setDaemon(false);
thread.start();
}
}
Controller:
#RestController
public class RestartController {
#PostMapping("/restart")
public void restart() {
Application.restart();
}
}

To elaborate on some of the other answers, this is what tools like Netflix Archaius (https://github.com/Netflix/archaius/wiki) attempt to solve (Dynamic Context Configurations). As far as I'm aware, the only way to accomplish this would be to refresh the contexts by restarting the application.

Related

Reading application.properties in static method

I have the below code when i put some properties statically in SpringApplicationBuilder
#SpringBootApplication(scanBasePackages = { "com" })
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
parentBuilder.child(RestConfiguration.class, SwaggerConfig.class)
.properties("server.port:9093")).web(WebApplicationType.SERVLET).run(args);
}
}
I want to move the properties to the file application.properties
application.port.query=9093
I used #value to read from the application file, but i get null. Is there another way to read data in a static method?
try something like this
#Configuration
#ConfigurationProperties("application")
class A {
public static int queryPort;
#Value("${port.query:9093}")
public void setQueryPort(final int portQuery){
A.queryPort = portQuery;
}
}
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
parentBuilder.child(RestConfiguration.class, SwaggerConfig.class)
.properties(A.queryPort)).web(WebApplicationType.SERVLET).run(args);
}
}
The preferable way to what you are trying to achieve is to use the spring bean itself which is the parent of your all configurations org.springframework.core.env.Environment
#SpringBootApplication(scanBasePackages = {"com"})
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(Application.class, args);
Environment environment = applicationContext.getBean(Environment.class);
String propertyValue = environment.getProperty("any.property.from.configuration")
}
}
In your use-case, just get the bean of Environment and from Environment object get any property value.

Initializa Bean with YAML configuration in Spring Boot

I would like to read some properties, like DB access configs, when initializing bean or service in spring boot.
Anyone knows good ways ?
This is my current code snippet.
public class SampleApplication implements ApplicationRunner
{
#Autowired
private YAMLConfig myConfig;
#Override
public void run(ApplicationArguments args) throws Exception
{
System.out.println(myConfig != null); //YAMLConfig has been intialized here
}
public SampleApplication()
{
System.out.println(myConfig == null); //myConfig is null
}
#Configuration
public static class Config
{
#Bean
#ConditionalOnProperty(value = {"batch.execute"}, havingValue = "SampleApplication")
public SampleApplication sampleApplication()
{
return new SampleApplication();
}
}
}
#Configuration
#EnableConfigurationProperties
#ConfigurationProperties
public class YAMLConfig
{
private String environment;
public String getEnvironment()
{
return environment;
}
public void setEnvironment(String environment)
{
this.environment = environment;
}
}
Thanks for taking a look at this!
create this method inside your SampleApplication class
#PostConstruct
public void init() {
// at this point, all the dependency injection has happened already
myConfig.doStuff()
}
it will be called by spring automatically after all bean initialization has been done.

Listing Aspect-proxied beans in Spring

Is there a way to get the list of beans proxied with a specific Aspect by Spring?
We have an aspect on some beans that stopped working, and we are trying to figure out what happened, so I created a class to scan the ApplicationContext after it has been loaded
#Component
public class AspectScanner implements ApplicationListener<ContextRefreshedEvent> {
public static final Logger LOGGER = LoggerFactory.getLogger(AspectScanner.class);
#Override
public void onApplicationEvent(ContextRefreshedEvent event) {
final ApplicationContext applicationContext = event.getApplicationContext();
applicationContext.[GET PROXIED BEANS CALL];
}
}
Any suggestions?
To identify advised bean you should check if it implements org.springframework.aop.framework.Advised and use org.springframework.aop.aspectj.AspectJPrecedenceInformation#getAspectName to get aspect name. Proof of concept code is presented below.
#Configuration
#ComponentScan
#EnableAspectJAutoProxy
public class AspectScanner implements ApplicationListener<ContextRefreshedEvent> {
public static final Logger LOGGER = LoggerFactory.getLogger(AspectScanner.class);
public void onApplicationEvent(ContextRefreshedEvent event) {
final ApplicationContext applicationContext = event.getApplicationContext();
Map<String, Object> beansOfType = applicationContext.getBeansOfType(Object.class);
for (Map.Entry<String, Object> entry : beansOfType.entrySet()) {
boolean advisedWith = isAdvisedWith(applicationContext, entry.getValue(), BeanAspect.class);
LOGGER.info(entry.getKey() + (advisedWith ? " is advised" : " isn't advised"));
}
}
private static boolean isAdvisedWith(ApplicationContext context, Object bean, Class<?> aspectClass) {
boolean advisedWith = false;
HashSet<String> names = new HashSet<>(Arrays.asList(context.getBeanNamesForType(aspectClass)));
if (bean instanceof Advised) {
Advisor[] advisors = ((Advised) bean).getAdvisors();
for (Advisor advisor : advisors) {
if (advisor instanceof AspectJPrecedenceInformation) {
if (names.contains(((AspectJPrecedenceInformation) advisor).getAspectName())) {
advisedWith = true;
}
}
}
}
return advisedWith;
}
#Aspect
#Component
public static class BeanAspect {
#Before("execution(* test.AspectScanner.Bean*.*(..))")
public void beforeAny(JoinPoint jp) {
}
}
#Component
public static class Bean1 {
public void m() {
}
}
public interface Bean2Intf {
void m();
}
#Component
public static class Bean2 implements Bean2Intf {
public void m() {
}
}
#Component
public static class NotAdvised {
public void n() {
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AspectScanner.class);
context.start();
context.registerShutdownHook();
}
}

Error creating bean with name 'application', No default constructor found; nested exception is java.lang.NoSuchMethodException

I don't quite understand why this code gives me 'no default constructor found' error. The constructor is #Autowired. Everything seems to be injected correctly. Can anybody please help? Thanks
#SpringBootApplication
public class Application {
private ApplicationContext applicationContext;
private MessagingService messagingService;
private Parser parser;
private static final Logger log = LoggerFactory.getLogger(Application.class);
#Autowired
public Application(ApplicationContext applicationContext,
MessagingService messagingService,
Parser parser)
{
this.applicationContext = applicationContext;
Assert.notNull(messagingService, "MessagingService must not be null");
this.messagingService = messagingService;
Assert.notNull(parser, "Parser must not be null");
this.parser = parser;
}
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner app() {
return args -> {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(defaultLocale);
log.info("Using MessagingService: " + messagingService.getMyMessageCode());
parser.parse();
};
}
}
Edit: updated Application.class
#SpringBootApplication
public class Application {
#Autowired
private MessagingService messagingService;
#Autowired
private Parser parser;
private static final Logger log = LoggerFactory.getLogger(Application.class);
public Application() {}
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner app() {
return args -> {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(defaultLocale);
log.info("Using MessagingService: " + messagingService.getMyMessageCode());
parser.parse();
};
}
}
Answer from luboskrnac is correct.
But if you really want to use Constructor Injection you can upgrade you SpringBoot version to 1.4.0.RELEASE which will use Spring 4.3.2.RELEASE
From Spring 4.3 Constructor Injection is supported on #Configuration class
New Features and Enhancements in Spring Framework 4.3
You can't autowire into main Spring Boot class. You can inject dependencies needed for CommandLineRunner as parameters of method annotated with #Bean and of course remove constructor injection for main class:
#SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner app(ApplicationContext applicationContext,
MessagingService messagingService,
Parser parser) {
return args -> {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(defaultLocale);
log.info("Using MessagingService: " + messagingService.getMyMessageCode());
parser.parse();
};
}
}
EDIT:
Correct context configuration after edit:
#SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public Application() {}
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner app(MessagingService messagingService, Parser parser) {
return args -> {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(defaultLocale);
log.info("Using MessagingService: " + messagingService.getMyMessageCode());
parser.parse();
};
}
}

#Autowired fails in spring boot web project

I have a simple Spring Boot web project, right from a template:
#SpringBootApplication
#RestController
public class HelloWorldRestApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldRestApplication.class, args);
Performer p = new Performer();
p.perform();
}
}
I have a test to ensure autowiring works, and in fact it does in this test class (examples come from Spring in Action, 4th):
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
#Autowired
private CDPlayer cdp;
#Test
public void cdShouldNotBeNull(){
assertNotNull(cdp);
}
}
and:
public class Performer {
#Autowired
private CDPlayer cdp;
public void perform(){
System.out.println(cdp);
cdp.play();
}
public CDPlayer getCdp() {
return cdp;
}
public void setCdp(CDPlayer cdp) {
this.cdp = cdp;
}
}
and:
#Component
public class CDPlayer{
public void play(){
System.out.println("play");
}
}
config:
#Configuration
#ComponentScan
public class CDPlayerConfig {
}
However, it doesnt work in HelloWorldRestApplication, I get null.
Adding #ContextConfiguration(classes=CDPlayerConfig.class) doesn't help.
What do I miss?
Try enabling #ComponentScan your packages in your main class and get Performer class instance from ApplicationContext as below:
#SpringBootApplication
#RestController
#ComponentScan({“package.name.1”,”package.name.2”})
public class HelloWorldRestApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(HelloWorldRestApplication.class, args);
Performer p = ctx.getBean(Performer.class);//get the bean by type
p.perform();
}
}

Categories

Resources