您的位置:首页 > 编程语言 > Java开发

SSH与SSM学习之Spring03——Spring创建对象的方式

2017-10-21 12:29 531 查看
SSH与SSM学习之Spring03Spring创建对象的方式
一说明

二Spring创建对象方式之一空餐构造函数方式
1 applicationContextxml

2 代码示例

3 运行结果

三Spring创建对象方式之二静态工厂方式
1 UserFactory

2 applicationContextxml

3 代码示例

4 运行结果

四Spring创建对象方式之三实例工厂方式
1 UserFactory

2 applicationContextxml

3 代码示例

4 运行结果

五Spring的分模块配置

SSH与SSM学习之Spring03——Spring创建对象的方式

一、说明

Spring 是对象的容器,它会根据配置,创建对象。其实这些都需要在 bean 中配置。

创建对象有三种创建方式

空参数构造方式

静态工厂方式

实例工厂方式

二、Spring创建对象方式之一空餐构造函数方式

其实前面我们使用的就是这种方式,要求就是创建的对象的类必须有空构造函数。

2.1 applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

<!-- 将User对象交给spring容器管理 -->
<!-- Bean元素:使用该元素描述需要spring容器管理的对象
class属性:被管理对象的完整类名.
name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.
可以重复.可以使用特殊字符.
id属性: 与name属性一模一样.
名称不可重复.不能使用特殊字符.
结论: 尽量使用name属性.
-->
<!--第一种创建方式:空参构造函数-->
<bean name="user1" class="com.qwm.spring1.bean.User"/>

</beans>


2.2 代码示例

/**
* 第一种创建方式:空参构造函数方式
*/
@Test
public void createWay1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) ac.getBean("user1");
System.out.println(user1);
}


2.3 运行结果

User实例化了----com.qwm.spring1.bean.User@3f49dace
com.qwm.spring1.bean.User@3f49dace


三、Spring创建对象方式之二静态工厂方式

这种方式其实就是使用一个工厂类,它会创建对象的静态方法。

3.1 UserFactory

package com.qwm.spring1.b_beans;
import com.qwm.spring1.bean.User;

/**
* @author: wiming
* @date: 2017-09-25 16:48:14  星期一
* @decription:
* 创建User的工厂
*/
public class UserFactory {
public static User createUser(){
System.out.println("静态工厂创建User");
return new User();
}
}


3.2 applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!--第二种创建方式:静态工厂-->
<bean name="user2" class="com.qwm.spring1.b_beans.UserFactory" factory-method="createUser"/>
</beans>


3.3 代码示例

/**
* 第二种创建方式:静态工厂
*/
@Test
public void createWay2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user2 = (User) ac.getBean("user2");
System.out.println(user2);
}


3.4 运行结果

静态工厂创建User
User实例化了----com.qwm.spring1.bean.User@67b92f0a
com.qwm.spring1.bean.User@67b92f0a


四、Spring创建对象方式之三实例工厂方式

这种方式和上面方式的区别是,这种方式工厂的方法是示例的方法,不在是静态方法。配置也会不同

4.1 UserFactory

package com.qwm.spring1.b_beans;
import com.qwm.spring1.bean.User;

/**
* @author: wiming
* @date: 2017-09-25 16:48:14  星期一
* @decription:
* 创建User的工厂
*/
public class UserFactory {
public User createUser2(){
System.out.println("实例工厂创建User");
return new User();
}
}


4.2 applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

<!--第三种创建方式:实例工厂-->

<bean name="userFactory" class="com.qwm.spring1.b_beans.UserFactory"/>

<bean name="user3" factory-bean="userFactory" factory-method="createUser2"/>

</beans>


4.3 代码示例

/**
* 第三种创建方式:实例工厂
*/
@Test
public void createWay3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user3 = (User) ac.getBean("user3");
System.out.println(user3);
}


4.4 运行结果

实例工厂创建User
User实例化了----com.qwm.spring1.bean.User@61f8bee4
com.qwm.spring1.bean.User@61f8bee4


五、Spring的分模块配置

分模块配置可以我们的主配置文件中到导入模块的配置。模块的配置和主模块的配置是一样。

导入配置使用 import

<import resource="文件地址"/>


例如

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

<import resource="com/qwm/spring1/bean/applicationContext.xml"/>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: