Add init-method example
This commit is contained in:
		
							parent
							
								
									b759bf7c59
								
							
						
					
					
						commit
						d27db567c1
					
				| @ -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())); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										10
									
								
								spring-all/src/main/resources/startupConfig.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								spring-all/src/main/resources/startupConfig.xml
									
									
									
									
									
										Normal 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> | ||||||
| @ -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); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user