scope of configuring bean s

Keywords: Spring Junit log4j xml

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
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springioc.domain;

public class User {

}
<?xml version="1.0" encoding="UTF-8"?>
<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">

	<!--
		Scope of bean s
		In the context of spring containers, the default mode for bean creation is singleton.

		Common scopes
		Singleton
		org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON
		Prototype (multiple cases)
		org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE

		Usage mode
		a, set the scope attribute value in the bean tag;
		b, or use the annotation @Scope directly;
		At the class level, it is often used in combination with @Component, and at the method level, it is used in combination with @Bean.

		The way xmlConfig works
		<bean id="" class="" scope=""/>

		Customize the scope of the bean and refer to the interface.
		org.springframework.beans.factory.config.Scope
		There are several built-in implementation classes whose domains are:
		request,session,globalSession,application,websocket

		The scope of singletons and multiple cases is commonly used.
		Other scope types, to be determined.

	-->
	<bean class="ocn.site.springioc.domain.User" scope="prototype"></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 context;

	@Test
	public void run() throws Exception {
		User auser = context.getBean(User.class);
		logger.info(auser);
		User buser = context.getBean(User.class);
		logger.info(buser);
		Assert.assertNotEquals(auser, buser);
	}

}
19-09-08 15:38:03 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:38:04 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:38:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 15:38:04 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@643b1d11: startup date [Sun Sep 08 15:38:04 CST 2019]; root of context hierarchy
19-09-08 15:38:04 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@45f45fa1
19-09-08 15:38:04 ocn.site.springioc.domain.Runtest  =====>>> ocn.site.springioc.domain.User@4c6e276e
19-09-08 15:38:04 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@643b1d11: startup date [Sun Sep 08 15:38:04 CST 2019]; root of context hierarchy

Posted by ebm on Fri, 04 Oct 2019 03:48:44 -0700