bean生命周期基于全局配置

siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springioc
│   │               └── domain
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springioc
    │               └── domain
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

15 directories, 4 files

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.3.22.RELEASEversion>
dependency>

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
    <scope>testscope>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-testartifactId>
    <version>4.3.22.RELEASEversion>
    <scope>testscope>
dependency>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;

public class User {

	private final Logger logger = Logger.getLogger(this.getClass());

	public void init() {
		logger.info("initialized");
	}

	public void destroy() {
		logger.info("destruct");
	}

}

<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.xsd"
	default-init-method="init" default-destroy-method="destroy">
	
	<bean class="ocn.site.springioc.domain.User">bean>

beans>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired User user;

	@Test
	public void run() throws Exception {
		logger.info(user);
	}

}
19-09-08 16:11:45 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-08 16:11:45 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4e04a765, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@783e6358, org.springframework.test.context.support.DirtiesContextTestExecutionListener@17550481]
19-09-08 16:11:45 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 16:11:45 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 16:11:45 CST 2019]; root of context hierarchy
19-09-08 16:11:45 ocn.site.springioc.domain.User  =====>>> initialized
19-09-08 16:11:45 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@589b3632
19-09-08 16:11:45 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 16:11:45 CST 2019]; root of context hierarchy
19-09-08 16:11:45 ocn.site.springioc.domain.User  =====>>> destruct

你可能感兴趣的:(springioc)