Bean的实例化
1. 构造器实例化:Spring容器通过Bean对应类中默认的无参构造方法来实例化Bean
package com.itheima.instance.constructor; public class Bean1 { }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bean1" class="com.itheima.instance.constructor.Bean1"></bean> </beans>
在beans1.xml文件中,定义了一个id为bean1的Bean,并通过class属性指定其对应的实现类Bean1
package com.itheima.instance.constructor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest1 { public static void main(String[] args) { //定义配置文件路径 String xmlPath = "com/itheima/instance/constructor/beans1.xml"; //ApplicationContext在加载配置文件时,对Bean进行实例化 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Bean1 bean = (Bean1) applicationContext.getBean("bean1"); System.out.println(bean); } }
在InstanceTest1类中,首先定义了配置文件的路径,然后Spring容器ApplicationContext会加载配置文件。在加载时,Spring容器会通过id为bean1的实现类Bean1中默认的无参构造方法对Bean进行实例化。
2. 静态工厂方法实例化
package com.itheima.instance.static_factory; public class Bean2 { }
package com.itheima.instance.static_factory; public class MyBean2Factory { //使用自己的方法创建Bean2实例 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean"></bean> </beans>
定义id为bean2的Bean,通过class属性指定其对应的工厂实现类(MyBean2Factory.java),需要增加factory-method属性来告诉Spring容器其方法名称为createBean。
package com.itheima.instance.static_factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest2 { public static void main(String[] args) { //定义配置文件路径 String xmlPath = "com/itheima/instance/static_factory/beans2.xml"; //ApplicationContext在加载配置文件时,对Bean进行实例化 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean2")); } }
3. 实例工厂方式实例化
package com.itheima.instance.factory; public class Bean3 { }
package com.itheima.instance.factory; public class MyBean3Factory { public MyBean3Factory(){ System.out.println("bean3工厂实例化中"); } //创建Bean3实例的方法 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory"></bean> <!-- 使用 factory-bean属性指向配置的实例工厂 使用factory-method属性确定使用工厂中的哪个方法 --> <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"></bean> </beans>
首先配置了一个工厂Bean,然后配置了需要实例化的Bean。在id为bean3的Bean中,使用factory-bean属性指向配置的实例工厂,使用factory-method属性来确定使用工厂中的createBean()方法
package com.itheima.instance.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest3 { public static void main(String[] args) { //指定配置文件路径 String xmlPath = "com/itheima/instance/factory/beans3.xml"; //ApplicationContext加载配置文件时,对Bean进行实例化 @SuppressWarnings("resource") ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean3")); } }
原文地址:https://www.cnblogs.com/tongdaeer/p/12264199.html
时间: 2024-10-16 15:11:45