实例化有三种方式:构造器,静态工厂,实例工厂。最常用的还是构造器实例化。
新建动态网站项目,命名为chapter02,项目结构:
ps:beans1.xml是在包下面不是在src下面
Bean1.java
1 package com.tiheima.instance.contructor; 2 3 public class Bean1 { 4 5 }
beans1.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> 6 <bean id="beans1" class="com.tiheima.instance.contructor.Bean1"></bean> 7 </beans>
InstanceTest.java
1 package com.tiheima.instance.contructor; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /*bean1是构造器实例化测试*/ 7 public class InstanceTest { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 String xmlpathString="com/tiheima/instance/contructor/beans1.xml"; 12 ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpathString); 13 Bean1 bean1=(Bean1)applicationContext.getBean("beans1"); 14 System.out.println(bean1); 15 } 16 17 }
截图:
上面是构造器实例化实例bean,从输出可以看出,创建了一个Bean1的对象。
Bean2.java
1 package com.tiheima.instance.static_factory; 2 3 public class Bean2 { 4 5 }
Bean2Factory.java
package com.tiheima.instance.static_factory; public class Bean2Factory { public static Bean2 creatBean2() { return new Bean2(); } }
StaticFactoryTest.java
1 package com.tiheima.instance.static_factory; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /*bean2是静态工厂实例化测试*/ 7 public class StaticFactoryTest { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 String xmlpathString="com/tiheima/instance/static_factory/beans2.xml"; 12 ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpathString); 13 Bean2 bean2=(Bean2)applicationContext.getBean("beans2"); 14 System.out.println(bean2); 15 } 16 17 }
bean2.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> 6 7 <bean id="beans2" class=" 8 com.tiheima.instance.static_factory.Bean2Factory" 9 factory-method="creatBean2"></bean> 10 </beans>
截图:
上面是静态工厂实例化bean,输出了一个Bean2的对象
Bean3.java
1 package com.tiheima.instance.factory; 2 3 public class Bean3 { 4 5 }
Bean3Factory.java
1 package com.tiheima.instance.factory; 2 3 public class Bean3Factory { 4 public Bean3 creatBean3() { 5 return new Bean3(); 6 } 7 public Bean3Factory() { 8 System.out.println("实例化Bean3中"); 9 } 10 }
FactoryBean3Test
1 package com.tiheima.instance.factory; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /*实例化工厂实例化bean*/ 7 public class FactoryBean3Test { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 String xmlpathString="com/tiheima/instance/factory/beans3.xml"; 12 ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpathString); 13 Bean3 bean3=(Bean3)applicationContext.getBean("beans3"); 14 System.out.println(bean3); 15 } 16 17 }
beans3.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> 6 <!-- 配置工厂 --> 7 <bean id="bean3Factory" class="com.tiheima.instance.factory.Bean3Factory"></bean> 8 <!-- 使用factory-bean标签指向配置好的实例工厂,使用factory-method确定使用工厂的方法 --> 9 <bean id="beans3" factory-bean="bean3Factory" factory-method="creatBean3" ></bean> 10 </beans>
截图:
上面是工厂实例化bean,输出了一个bean3对象以及factory中的构造函数。
来源:https://www.cnblogs.com/2312947032zyk/p/11188749.html