Spring笔记(2)——Spring中创建对象的三种方式

    Spring配置中,有三种配置方式。第一种是Spring中最常见的,由Spring容器来创建对象,会调用配置类的无参构造方法;第二种是静态工厂方式;第三种实例工厂模式。

①无参构造方式:

  初始Java项目结构图:

实体类HelloWorld.java:

package com.shw;
public class HelloWorld {
	private String userName;
	
	public HelloWorld() {
		System.out.println("HelloWorld无参方法被调用!");
	}
	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public void show() {
		System.out.println("Welcome " + userName + " to study Spring!");
	}
}

Spring配置文件applicationContext.xml:

<?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">
		<!-- Configuring a bean -->
	<bean name="helloWorld" class="com.shw.HelloWorld">
		<!-- Assessments for Properties -->
		<property name="userName" value="Richard"></property>
	</bean>
</beans>

 测试类JunitTest.java:

package com.rr.test;

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

import com.shw.HelloWorld;

public class JunitTest {
	// 第一种方式,无参构造创建,会调用类的无参构造方法
	@Test 
	public void fun1() {
		// Loading applicationContext.xml
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		// Getting  instance of configuration
		HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
		
		// Invoke method
		helloWorld.show();
	}
	
	
}

 运行结果:

可以看到在配置文件中添加的bean类,其无参构造函数会被调用。

②静态工厂方式:

在com.shw包下创建一个HelloWorldFactory类:

package com.shw;

public class HelloWorldFactory {
	public static HelloWorld createHelloWorld() {
		System.out.println("静态工厂方式创建!");
		return new HelloWorld();
	}
}

applicationContext.xml中添加bean元素:

<bean name="hw2" class="com.shw.HelloWorldFactory" factory-method="createHelloWorld"></bean>

JunitTest.java添加如下单元测试:

	@Test 
	public void fun2() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorld helloWorld = (HelloWorld)ctx.getBean("hw2");
		helloWorld.show();
	}

运行结果:

第一行中是applicationContext.xml被加载进来,其中的bean对象就会被创建好,默认为单例(之后会谈到的scope属性),不同于过去BeanFactory那种用的时候再创建的类。第二、三行执行的是Factory类中静态方法。

③实例工厂:

HelloWorldFactory类中添加如下方法:

	public HelloWorld rcreateHelloWorld() {
		System.out.println("实例工厂方式创建对象");
		return new HelloWorld();
	}

applicationContext.xml添加如下bean元素:

<bean name="helloworldFactory" class="com.shw.HelloWorldFactory"></bean>
	<bean name="hw3" factory-bean="helloworldFactory" factory-method="rcreateHelloWorld"></bean>

最后在JunitTest.java中添加新的单元测试方法:

	@Test 
	public void fun3() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorld helloWorld = (HelloWorld)ctx.getBean("hw3");
		helloWorld.show();
	}

运行结果:

思考一下,结果为什么是这样?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值