JAVA-17: Move Running Setup Data on Startup in Spring into spring-boot-data

This commit is contained in:
Krzysiek
2020-07-31 22:02:44 +02:00
parent acf23c0a11
commit c748e81723
15 changed files with 10 additions and 15 deletions
@@ -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");
}
}
@@ -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++;
}
}
@@ -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()));
}
}
@@ -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()));
}
}
@@ -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();
}
}
@@ -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()));
}
}
@@ -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()));
}
}
@@ -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 {
}
@@ -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++;
}
}