Add init-method example

This commit is contained in:
Grzegorz Piwowarek 2016-08-14 08:58:25 +02:00
parent b759bf7c59
commit d27db567c1
3 changed files with 55 additions and 0 deletions

View File

@ -0,0 +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 java.util.Arrays;
@Component
@Scope(value = "prototype")
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()));
}
}

View File

@ -0,0 +1,10 @@
<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"
init-method="init">
</bean>
</beans>

View File

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