Merge pull request #602 from eugenp/grzegorz_spring_startup
"Running Logic on Startup in Spring" Examples #1
This commit is contained in:
commit
623c2a0f63
|
@ -130,6 +130,13 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package org.baeldung.async;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Component
|
||||
public class AsyncComponent {
|
||||
|
||||
|
@ -19,7 +19,7 @@ public class AsyncComponent {
|
|||
System.out.println("Execute method asynchronously " + Thread.currentThread().getName());
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
return new AsyncResult<String>("hello world !!!!");
|
||||
return new AsyncResult<>("hello world !!!!");
|
||||
} catch (final InterruptedException e) {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package org.baeldung.profiles;
|
||||
|
||||
public interface DatasourceConfig {
|
||||
public void setup();
|
||||
void setup();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.startup;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
public class AllStrategiesExampleBean implements InitializingBean {
|
||||
|
||||
private static final Logger LOG = Logger.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 org.baeldung.startup;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class InitMethodExampleBean {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(InitMethodExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public void init() {
|
||||
LOG.info(Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package org.baeldung.startup;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
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 = Logger.getLogger(InitializingBeanExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
LOG.info(Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package org.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,25 @@
|
|||
package org.baeldung.startup;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
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 = Logger.getLogger(LogicInConstructorExampleBean.class);
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
@Autowired
|
||||
public LogicInConstructorExampleBean(Environment environment) {
|
||||
this.environment = environment;
|
||||
|
||||
LOG.info(Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.baeldung.startup;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
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 = Logger.getLogger(PostConstructExampleBean.class);
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
LOG.info(Arrays.asList(environment.getDefaultProfiles()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.baeldung.startup;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.startup")
|
||||
public class SpringStartupConfig {
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package org.baeldung.startup;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
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 = Logger.getLogger(StartupApplicationListenerExample.class);
|
||||
|
||||
public static int counter;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
LOG.info("Increment counter");
|
||||
counter++;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<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="org.baeldung.startup.InitMethodExampleBean"
|
||||
scope="prototype"
|
||||
init-method="init">
|
||||
</bean>
|
||||
|
||||
<bean id="allStrategiesExampleBean"
|
||||
class="org.baeldung.startup.AllStrategiesExampleBean"
|
||||
init-method="init">
|
||||
</bean>
|
||||
</beans>
|
|
@ -0,0 +1,44 @@
|
|||
package org.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 SpringStartupTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.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 SpringStartupXMLConfigTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue