首页 > 代码库 > Spring4-作用域singleton和prototype

Spring4-作用域singleton和prototype

1.创建Maven项目,项目名称springdemo26,如图所示

技术分享


2.配置Maven,修改项目中的pom.xml文件,修改内容如下

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>1.0.0</modelVersion>
  <groupId>shequ</groupId>
  <artifactId>springdemo13</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
  	<java.version>1.7</java.version>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  
  <repositories>
  	<repository>
  		<id>codelds</id>
  		<url>https://code.lds.org/nexus/content/groups/main-repo</url>
  	</repository>
  </repositories>
  
  <dependencies>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.10</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-core</artifactId>
  		<version>4.1.4.RELEASE</version>
  	</dependency>
  	
  	<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
       
  </dependencies>
  <build/>
</project>


3.在src/main/java下创建实体Bean Forum,包名(com.mycompany.shequ.bean)如图所示

技术分享


4.实体Bean Forum的内容如下

package com.mycompany.shequ.bean;

public class Forum {
	private int fid;
	private String name;
	public int getFid() {
		return fid;
	}
	public void setFid(int fid) {
		this.fid = fid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "{fid=>"+this.fid+",name=>"+this.name+"}";
	}
}


5.在src/main/java下创建实体Bean ForumPost,包名(com.mycompany.shequ.bean)如图所示

技术分享


6.实体Bean ForumPost的内容如下

package com.mycompany.shequ.bean;

public class ForumPost {
	private int pid;
	private String name;
	private Forum forum;
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Forum getForum() {
		return forum;
	}
	public void setForum(Forum forum) {
		this.forum = forum;
	}
	@Override
	public String toString() {
		return "{pid=>"+this.pid+",name=>"+this.name+",["+this.forum+"]}";
	}
}


7.在src/main/resources下创建spring-bean.xml配置文件,如图所示

技术分享


8.spring-bean.xml配置文件的内容如下

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

	<!-- 1.scope="singleton"表示单例,如果不写scope,默认为单例 -->
	<bean id="forumpost1" class="com.mycompany.shequ.bean.ForumPost" scope="singleton">
		<property name="pid" value="http://www.mamicode.com/1"></property>
		<property name="name" value="http://www.mamicode.com/singleton"></property>
		<property name="forum" ref="forum1"></property>
	</bean>
	
	<bean id="forum1" class="com.mycompany.shequ.bean.Forum">
		<property name="fid" value="http://www.mamicode.com/1"></property>
		<property name="name" value="http://www.mamicode.com/singleton"></property>
	</bean>
	
	<!-- ******************************分开比较好************************************ -->
	
	<!-- 2.scope="prototype"表示原型,如果不写scope,默认为单例 -->
	<bean id="forumpost2" class="com.mycompany.shequ.bean.ForumPost" scope="prototype">
		<property name="pid" value="http://www.mamicode.com/2"></property>
		<property name="name" value="http://www.mamicode.com/prototype"></property>
		<property name="forum" ref="forum2"></property>
	</bean>
	
	<bean id="forum2" class="com.mycompany.shequ.bean.Forum">
		<property name="fid" value="http://www.mamicode.com/2"></property>
		<property name="name" value="http://www.mamicode.com/prototype"></property>
	</bean>

</beans>


9.在src/main/resources下创建核心配置文件applicationContext.xml,如图所示

技术分享


10.核心配置文件applicationContext.xml的内容如下

<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-4.0.xsd">

	<import resource="bean/spring-bean.xml"/>

</beans>


11.在src/test/java下创建测试文件AppTest,包名(com.mycompany.shequ.test)如图所示

技术分享


12.测试文件AppTest的内容如下

package com.mycompany.shequ.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mycompany.shequ.bean.ForumPost;

public class AppTest {
	
	@Test
	public void beanTest(){
	    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	    ForumPost forumPost1 = (ForumPost) context.getBean("forumpost1");
	    System.out.println(forumPost1.hashCode());
		
	    ForumPost forumPost2 = (ForumPost) context.getBean("forumpost1");
	    System.out.println(forumPost2.hashCode());
		
	    System.out.println("-------------------------分开比较好------------------------");
		
	    ForumPost forumPostA = (ForumPost) context.getBean("forumpost2");
	    System.out.println(forumPostA.hashCode());
		
	    ForumPost forumPostB = (ForumPost) context.getBean("forumpost2");
	    System.out.println(forumPostB.hashCode());
	}
}


13.在测试类AppTest的beanTest方法上右键运行,输出结果如图所示

技术分享

技术分享

本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1909881

Spring4-作用域singleton和prototype