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 e727b34e20
commit aa4f50ca09
15 changed files with 10 additions and 15 deletions

View File

@@ -1,35 +0,0 @@
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");
}
}

View File

@@ -1,21 +0,0 @@
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++;
}
}

View File

@@ -1,24 +0,0 @@
package com.baeldung.startup;
import java.util.Arrays;
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;
@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()));
}
}

View File

@@ -1,26 +0,0 @@
package com.baeldung.startup;
import java.util.Arrays;
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;
@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()));
}
}

View File

@@ -1,18 +0,0 @@
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();
}
}

View File

@@ -1,22 +0,0 @@
package com.baeldung.startup;
import java.util.Arrays;
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;
@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()));
}
}

View File

@@ -1,27 +0,0 @@
package com.baeldung.startup;
import java.util.Arrays;
import javax.annotation.PostConstruct;
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;
@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()));
}
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.startup;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.startup")
public class SpringStartupConfig {
}

View File

@@ -1,22 +0,0 @@
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++;
}
}

View File

@@ -1,16 +0,0 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="initMethodExampleBean"
class="com.baeldung.startup.InitMethodExampleBean"
scope="prototype"
init-method="init">
</bean>
<bean id="allStrategiesExampleBean"
class="com.baeldung.startup.AllStrategiesExampleBean"
init-method="init">
</bean>
</beans>

View File

@@ -1,44 +0,0 @@
package com.baeldung.startup;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringStartupConfig.class }, loader = AnnotationConfigContextLoader.class)
public class SpringStartupIntegrationTest {
@Autowired
private ApplicationContext ctx;
@Test(expected = BeanCreationException.class)
public void whenInstantiating_shouldThrowBCE() throws Exception {
ctx.getBean(InvalidInitExampleBean.class);
}
@Test
public void whenPostConstruct_shouldLogEnv() throws Exception {
ctx.getBean(PostConstructExampleBean.class);
}
@Test
public void whenConstructorInjection_shouldLogEnv() throws Exception {
ctx.getBean(LogicInConstructorExampleBean.class);
}
@Test
public void whenInitializingBean_shouldLogEnv() throws Exception {
ctx.getBean(InitializingBeanExampleBean.class);
}
@Test
public void whenApplicationListener_shouldRunOnce() throws Exception {
Assertions.assertThat(StartupApplicationListenerExample.counter).isEqualTo(1);
}
}

View File

@@ -1,26 +0,0 @@
package com.baeldung.startup;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:startupConfig.xml")
public class SpringStartupXMLConfigIntegrationTest {
@Autowired
private ApplicationContext ctx;
@Test
public void whenPostConstruct_shouldLogEnv() throws Exception {
ctx.getBean(InitMethodExampleBean.class);
}
@Test
public void whenAllStrategies_shouldLogOrder() throws Exception {
ctx.getBean(AllStrategiesExampleBean.class);
}
}