Spring-Bean的3種例項化
阿新 • • 發佈:2019-01-04
構造器例項化
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"));
}
}