Spring 框架实例化的方式:
构造方法实例化:(默认无参数)
public class bean1{}
@Test
public void demo1()
{
@SuppressWarnings("resource")
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
bean1 b1= (bean1) context.getBean("bean1");
b1.setName("1");
System.out.println(b1.getName());
return;
}
//配置:
<bean id="bean1" class="com.brook.spring.instancebean.bean1"></bean>
静态工厂实化bean
public class Bean2{
}
public class StaticBeanFactory {
public static Bean2 getBean()
{
return new Bean2();
}
}
//配置
<bean id="bean2" class="com.brook.spring.instancebean.StaticBeanFactory" factory-method="getBean"></bean>
实例工厂实例化bean
public clsass BeanFactory{}
public class BeanFactory {
public Bean3 getBean(){
return new Bean3();
}
}
//配置
<bean id ="bean3" factory-bean="beanFactory" factory-method="getBean"></bean>
<bean id="beanFactory" class="com.brook.spring.instancebean.BeanFactory"></bean>