创建对象三种的方式
无参构造函数
无参构造函数
<bean id="helloWorld" class="com.hw.entity.HelloWorldFactory" >
</bean>
静态工厂
public class HelloWorld {
public void hello() {
System.out.println("哈哈");
}
}
--------------------------------------------
public class HelloWorldFactory {
public static HelloWorld getFactory() {
return new HelloWorld();
}
}
---------------------------------------------------
<bean id="helloWorld2" class="com.hw.entity.HelloWorldFactory" factory-method="getFactory"></bean>
@Test
public void test001() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) ac.getBean("helloWorld2");
hello.hello();
}
}
实例工厂
public class HelloWorld {
public void hello() {
System.out.println("哈哈");
}
}
public class HelloWorldFactory {
public HelloWorld getFactory() {
return new HelloWorld();
}
}
<bean id="helloWorld" class="com.hw.entity.HelloWorldFactory" ></bean>
<bean id="helloWorld3" factory-bean="helloWorld" factory-method="getFactory"></bean>
@Test
public void test001() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) ac.getBean("helloWorld2");
hello.hello();
}
}