spring学习(03)之bean实例化的三种方式

时间:2021-05-14 16:01:17

bean实体例化的三种方式

在spring中有三中实例化bean的方式:

一、使用构造器实例化;(通常使用的一个方法,重点)

二、使用静态工厂方法实例化;

三、使用实例化工厂方法实例化

第一种、使用构造器实例化;

这种实例化的方式可能在我们平时的开发中用到的是最多的,因为在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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="cn.lmn.ioc.User"></bean> </beans>

id是对象的名称,class是要实例化的类。

在这个类里面还要有个无参的构造,不然会出现异常

第二种 使用静态工厂创建

(1)创建静态的方法,返回类对象

package cn.lmn.bean;

public class Bean2Factory {
//静态方法,返回Bean2对象
public static Bean2 getBean2(){
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="user" class="cn.lmn.ioc.User"></bean> -->
<!-- 使用静态工厂创建对象 -->
<bean id="bean2" class="cn.lmn.bean.Bean2Factory" factory-method="getBean2"></bean>
</beans>

最后测试一下

 package cn.lmn.test;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.lmn.bean.Bean2;
import cn.lmn.ioc.User; public class TestIOC {
@Test
public void testUser() {
// 1加载spring的配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2得到配置创建的对象
// User user = (User) context.getBean("user");
Bean2 bean2 = (Bean2) context.getBean("bean2");
System.out.println(bean2); }
}

第三种 使用实例工厂创建

(1)创建不是静态的方法,返回类对象

package cn.lmn.bean;

public class Bean3Factory {
//普通的方法,返回的是Bean2的对象
public Bean3 getBean3(){
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="user" class="cn.lmn.ioc.User"></bean> -->
<!-- 使用静态工厂创建对象 -->
<!-- <bean id="bean2" class="cn.lmn.bean.Bean2Factory" factory-method="getBean2"></bean> -->
<!-- 使用实例工厂创建对象 -->
<!-- 创建工厂对象 -->
<bean id="bean3Factory" class="cn.lmn.bean.Bean3Factory"></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
</beans>

最后测试一下

 package cn.lmn.test;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.lmn.bean.Bean3; public class TestIOC {
@Test
public void testUser() {
// 1加载spring的配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2得到配置创建的对象
// User user = (User) context.getBean("user");
Bean3 bean3 = (Bean3) context.getBean("bean3");
System.out.println(bean3); }
}