在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象
org.springframework.beans.factory.BeanFactory 是Spring IoC容器的实际代表者,IoC容器负责容纳此前所描述的bean,并对bean进行管理。
package com.weixuan.testbean;
public class HelloWorld {
public HelloWorld() {
System.out.println("...Default Constructor ...");
}
public void hello() {
System.out.println("Hello World .");
}
}
@Test
public void test() {
applicationContext.getBean("helloWorld");
}
<bean id="helloWorld" class="com.weixuan.testbean.HelloWorld"></bean>
package com.weixuan.testbean;
/**
* 简单工厂模式
* @author Nicholas
*
*/
public class HelloWorldFactory {
public static HelloWorld CreateHelloWorld() {
return new HelloWorld();
}
}
@Test
public void test2() {
HelloWorld helloWorld = (HelloWorld) applicationContext
.getBean("helloWorldFactory");
helloWorld.hello();
}
<bean id="helloWorldFactory" class="com.weixuan.testbean.HelloWorldFactory"
factory-method="CreateHelloWorld">
</bean>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/weiyongxuan/article/details/46948449