Fork me on GitHub

Bean实例化的三种方式

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

   

   

   

   

   

   

   

   

Bean 实例化的三种方式

   

   

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

   

   

   

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

   

User.java:

   

package com.siwuxie095.instance;

   

public class User {

 

public void add() {

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

}

   

}

   

   

   

   

   

   

   

   

2、Bean 实例化的三种方式

   

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

   

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

   

   

   

applicationContext.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="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

public void 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:

   

<?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="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:

   

<?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="factory" class="com.siwuxie095.instance.UserFactory"></bean>

 

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

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

 

   

</beans>

   

   

   

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

   

   

   

   

   

3、总结

   

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

   

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

   

   

   

   

   

   

   

   

【made by siwuxie095】

posted on 2017-08-20 22:05  siwuxie095  阅读(602)  评论(0编辑  收藏  举报

导航