Add examples

This commit is contained in:
Grzegorz Piwowarek 2016-08-13 13:32:04 +02:00
parent cd2ee40c06
commit bec20baad0
7 changed files with 62 additions and 11 deletions

View File

@ -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) {
}

View File

@ -1,14 +1,23 @@
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;
@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(environment);
}
}

View File

@ -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();
}
}

View File

@ -1,5 +1,6 @@
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;
@ -9,10 +10,14 @@ import org.springframework.stereotype.Component;
@Scope(value = "prototype")
public class LogicInConstructorExampleBean {
@Autowired
private Environment environment;
private static final Logger LOG = Logger.getLogger(LogicInConstructorExampleBean.class);
public LogicInConstructorExampleBean() {
environment.getActiveProfiles();
private final Environment environment;
@Autowired
public LogicInConstructorExampleBean(Environment environment) {
this.environment = environment;
LOG.info(environment);
}
}

View File

@ -1,17 +1,24 @@
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;
@Component
@Scope(value = "prototype")
public class PostConstructExampleBean {
private static final Logger LOG = Logger.getLogger(PostConstructExampleBean.class);
@Autowired
private Environment environment;
public PostConstructExampleBean() {
@PostConstruct
public void init() {
LOG.info(environment);
}
}

View File

@ -6,5 +6,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.startup")
public class SpringStartupConfig {
}

View File

@ -17,9 +17,22 @@ public class SpringStartupTest {
private ApplicationContext ctx;
@Test(expected = BeanCreationException.class)
public void whenInstantiating_shouldThrowNPE() throws Exception {
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);
}
}