Bean实例化的三种方式

-------------------siwuxie095

  

  

  

  

  

  

  

  

Bean 实例化的三种方式

  

  

1、Bean 实例化,即 在 Spring 的核心配置文件中创建对象

  

  

  

编写一个普通类(即要实例化的类):

  

User.java:

  

package com.siwuxie095.instance;

  

public class User {

 

publicvoid add() {

System.out.println("----- add -----");

}

  

}

  

  

  

  

  

  

  

  

2、Bean 实例化的三种方式

  

1第一种方式:使用无参的构造器实例化

  

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

  

  

  

applicationContext.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

  

  

<!-- 使用无参的构造器进行实例化 -->

<beanid="user"class="com.siwuxie095.instance.User"></bean>

 

  

</beans>

  

  

  

编写一个测试类:

  

TestUser.java:

  

package com.siwuxie095.instance;

  

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

  

public class TestUser {

  

/**

* 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 jar 包)

*

* 选中方法名,右键->Run As->JUint Test

*/

@Test

publicvoid testUser(){

 

// (1) 加载 Spring 的核心配置文件

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

 

// (2) 得到核心配置文件中创建的对象(获取 Bean 实例)

User user=(User) context.getBean("user");

 

user.add();

}

}

  

  

  

使用第一种方式需要注意

  

如果类中存在有参的构造器,一定要手动创建一个无参的构造器,

否则将出现异常(因为此时不存在默认的无参的构造器)

  

  

  

  

  

2第二种方式:使用静态工厂方法实例化

  

编写一个静态工厂类:

  

UserFactory.java:

  

package com.siwuxie095.instance;

  

// 静态工厂类

public class UserFactory {

  

// 静态工厂方法

public static User getUser() {

return new User();

}

 

}

  

  

  

id 是要实例化的对象,class 是静态工厂类,factory-method 是静态工厂方法

  

  

  

applicationContext.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

  

 

<!-- 使用静态工厂方法进行实例化 -->

<beanid="user"class="com.siwuxie095.instance.UserFactory"

factory-method="getUser"></bean>

 

  

</beans>

  

  

  

编写一个测试类(同上TestUser.java)

  

  

  

  

  

3第三种方式:使用实例工厂方法实例化

  

编写一个实例工厂类:

  

UserFactory.java:

  

package com.siwuxie095.instance;

  

// 实例工厂类

public class UserFactory {

  

// 实例工厂方法

public User getUser() {

return new User();

}

 

}

  

  

  

第一个Bean:id 是实例工厂类的对象,class 是实例工厂类,

  

第二个Bean:id 是要实例化的对象,factory-bean 是实例

工厂类的对象,factory-method 是实例工厂方法

  

  

  

applicationContext.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

  

 

<!-- 使用实例工厂方法进行实例化 -->

 

<!-- 先创建实例工厂类的对象 -->

<beanid="factory"class="com.siwuxie095.instance.UserFactory"></bean>

 

<!-- 再通过实例工厂方法创建要实例化的对象 -->

<beanid="user"factory-bean="factory"factory-method="getUser"></bean>

 

  

</beans>

  

  

  

编写一个测试类(同上TestUser.java)

  

  

  

  

  

3、总结

  

1)一般主要使用无参构造器的方式进行Bean 的实例化

  

2)静态工厂方法和实例工厂方法的主要区别就在于是否有static 关键字

  

  

  

  

  

  

  

  

【made by siwuxie095】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值