现在的位置: 首页 > 综合 > 正文

Spring 通过单利模式,单利方法获取对象–factory-method=”getInstance”(二)

2013年07月17日 ⁄ 综合 ⁄ 共 467字 ⁄ 字号 评论关闭

1

	<bean id="a"
		class="com.ioc.A" factory-method="getInstance">
		<property name="b" ref="b" />
		<property name="c" ref="c" />
	</bean>

2

package com.ioc;

public class A {

	B b;
	C c;
	
	public A() {
	}
	public A(String title)
	{
		System.out.println("i am single");
	}
	
	public B getB() {
		return b;
	}
	public void setB(B b) {
		this.b = b;
	}
	public C getC() {
		return c;
	}
	public void setC(C c) {
		this.c = c;
	}
	@Override
	public String toString() {
		return "A [b=" + b + ", c=" + c + "]";
	}
	public static A a = null;
	public static A getInstance()
	{
		if(a == null)
		{
			a = new A("hello");
		}
		return a;
	}
}

抱歉!评论已关闭.