xml-配置bean之3种初始化方式

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;

public class User {

	public User() {
		super();
	}

	public static User newInstance() {
		return new User();
	}

	public User getInstance() {
		return this;
	}

}

<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">

	
	<bean id="auser" class="ocn.site.springioc.domain.User">bean>
	<bean id="buser" class="ocn.site.springioc.domain.User" factory-method="newInstance">bean>
	<bean id="cuser" class="ocn.site.springioc.domain.User" factory-bean="auser" factory-method="getInstance">bean>


beans>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;
import org.junit.Assert;
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.SpringRunner;

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

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired ApplicationContext ac;

	@Test
	public void run() throws Exception {
		User auser = (User) ac.getBean("auser");
		logger.info(auser);
		User buser = (User) ac.getBean("buser");
		logger.info(buser);
		User cuser = (User) ac.getBean("cuser");
		logger.info(cuser);
		Assert.assertEquals(auser, cuser);
	}

}
19-09-08 15:11:09 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 15:11:09 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@13fee20c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4e04a765, org.springframework.test.context.support.DirtiesContextTestExecutionListener@783e6358]
19-09-08 15:11:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 15:11:09 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@643b1d11: startup date [Sun Sep 08 15:11:09 CST 2019]; root of context hierarchy
19-09-08 15:11:09 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@2145433b
19-09-08 15:11:09 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@2890c451
19-09-08 15:11:09 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@2145433b
19-09-08 15:11:09 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@643b1d11: startup date [Sun Sep 08 15:11:09 CST 2019]; root of context hierarchy

你可能感兴趣的:(springioc)