Add examples
This commit is contained in:
parent
cd2ee40c06
commit
bec20baad0
|
@ -1,11 +1,11 @@
|
||||||
package org.baeldung.async;
|
package org.baeldung.async;
|
||||||
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.annotation.AsyncResult;
|
import org.springframework.scheduling.annotation.AsyncResult;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class AsyncComponent {
|
public class AsyncComponent {
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public class AsyncComponent {
|
||||||
System.out.println("Execute method asynchronously " + Thread.currentThread().getName());
|
System.out.println("Execute method asynchronously " + Thread.currentThread().getName());
|
||||||
try {
|
try {
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
return new AsyncResult<String>("hello world !!!!");
|
return new AsyncResult<>("hello world !!!!");
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,23 @@
|
||||||
package org.baeldung.startup;
|
package org.baeldung.startup;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Scope(value = "prototype")
|
@Scope(value = "prototype")
|
||||||
public class InitializingBeanExampleBean implements InitializingBean {
|
public class InitializingBeanExampleBean implements InitializingBean {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(InitializingBeanExampleBean.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment environment;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
LOG.info(environment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package org.baeldung.startup;
|
package org.baeldung.startup;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
@ -9,10 +10,14 @@ import org.springframework.stereotype.Component;
|
||||||
@Scope(value = "prototype")
|
@Scope(value = "prototype")
|
||||||
public class LogicInConstructorExampleBean {
|
public class LogicInConstructorExampleBean {
|
||||||
|
|
||||||
@Autowired
|
private static final Logger LOG = Logger.getLogger(LogicInConstructorExampleBean.class);
|
||||||
private Environment environment;
|
|
||||||
|
|
||||||
public LogicInConstructorExampleBean() {
|
private final Environment environment;
|
||||||
environment.getActiveProfiles();
|
|
||||||
|
@Autowired
|
||||||
|
public LogicInConstructorExampleBean(Environment environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
|
||||||
|
LOG.info(environment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,24 @@
|
||||||
package org.baeldung.startup;
|
package org.baeldung.startup;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Scope(value = "prototype")
|
@Scope(value = "prototype")
|
||||||
public class PostConstructExampleBean {
|
public class PostConstructExampleBean {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(PostConstructExampleBean.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment environment;
|
private Environment environment;
|
||||||
|
|
||||||
public PostConstructExampleBean() {
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
LOG.info(environment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,5 +6,4 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("org.baeldung.startup")
|
@ComponentScan("org.baeldung.startup")
|
||||||
public class SpringStartupConfig {
|
public class SpringStartupConfig {
|
||||||
|
|
||||||
}
|
}
|
|
@ -17,9 +17,22 @@ public class SpringStartupTest {
|
||||||
private ApplicationContext ctx;
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@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);
|
ctx.getBean(LogicInConstructorExampleBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInitializingBean_shouldLogEnv() throws Exception {
|
||||||
|
ctx.getBean(InitializingBeanExampleBean.class);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue