JAVA-17: Move Running Setup Data on Startup in Spring into spring-boot-data
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Component
|
||||
@Scope(value = "prototype")
|
||||
public class AllStrategiesExampleBean implements InitializingBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AllStrategiesExampleBean.class);
|
||||
|
||||
public AllStrategiesExampleBean() {
|
||||
LOG.info("Constructor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
LOG.info("InitializingBean");
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
LOG.info("PostConstruct");
|
||||
}
|
||||
|
||||
public void init() {
|
||||
LOG.info("init-method");
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class EventListenerExampleBean {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EventListenerExampleBean.class);
|
||||
|
||||
public static int counter;
|
||||
|
||||
@EventListener
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
LOG.info("Increment counter");
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
@Scope(value = "prototype")
|
||||
public class InitMethodExampleBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(InitMethodExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public void init() {
|
||||
LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
@Scope(value = "prototype")
|
||||
public class InitializingBeanExampleBean implements InitializingBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(InitializingBeanExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Scope("prototype")
|
||||
public class InvalidInitExampleBean {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public InvalidInitExampleBean() {
|
||||
environment.getActiveProfiles();
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
@Scope(value = "prototype")
|
||||
public class LogicInConstructorExampleBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(LogicInConstructorExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
public LogicInConstructorExampleBean(Environment environment) {
|
||||
LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
@Scope(value = "prototype")
|
||||
public class PostConstructExampleBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PostConstructExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.startup")
|
||||
public class SpringStartupConfig {
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.startup;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class StartupApplicationListenerExample implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(StartupApplicationListenerExample.class);
|
||||
|
||||
public static int counter;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
LOG.info("Increment counter");
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user