Spring-Bean的3种实例化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwl755/article/details/85721265

构造器实例化

bean1.java

package cn.itcast.instance.constructor;

public class Bean1 {

}

InstanceTest1.java

package cn.itcast.instance.constructor;

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

public class InstanceTest1 {
	
	@Test
	public void demo01(){
		String xmlpath="cn/itcast/instance/constructor/beans1.xml";
		ApplicationContext context=new ClassPathXmlApplicationContext(xmlpath);
		System.out.println(context.getBean("bean1"));
	}
}

bean1.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
   <bean id="bean1" class="cn.itcast.instance.constructor.Bean1"></bean>
</beans>

 

静态工厂实例化

bean2.java

package cn.itcast.instance.static_factory;

public class Bean2 {

}
package cn.itcast.instance.static_factory;

public class MyBean2Factory {
	public static Bean2 createBean(){
		return new Bean2();
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
   <bean id="bean2" 
   		class="cn.itcast.instance.static_factory.MyBean2Factory"
   		factory-method="createBean">
   </bean>
</beans>

实例工厂实例化

package cn.itcast.instance.factory;

public class Bean3 {

}
package cn.itcast.instance.factory;

public class MyBean3Factory {
	public MyBean3Factory(){
		System.out.println("bean3 工厂实例化中");
	}
	//创建Bean的方法
	public Bean3 createBean(){
		return new Bean3();
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <!-- 配置工厂 -->
   <bean id="myBean3Factory" class="cn.itcast.instance.factory.MyBean3Factory"/>
   
   <!-- 使用factory-bean属性配置一个实例化工厂 
   		使用factory-method属性确定使用工厂的哪个函数-->
   <bean id="bean3" 
   		factory-bean="myBean3Factory"
   		factory-method="createBean">
   </bean>
</beans>
package cn.itcast.instance.factory;

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

public class InstanceTest3 {
	
	@Test
	public void demo03(){
		String xmlpath="cn/itcast/instance/factory/beans3.xml";
		ApplicationContext context=new ClassPathXmlApplicationContext(xmlpath);
		System.out.println(context.getBean("bean3"));
	}
}

猜你喜欢

转载自blog.csdn.net/qwl755/article/details/85721265