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的分模块配置
创建对象有三种创建方式
空参数构造方式
静态工厂方式
实例工厂方式
导入配置使用 import
例如
一说明
二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>
相关文章推荐
- SSH与SSM学习之Spring21——Spring事务之代码方式管理事务(基本不用)
- SSM框架之spring学习1——创建对象,处理依赖关系
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- Spring学习心得(2)-- 创建对象的方式
- Spring 4.0 学习日记(2) --IOC 创建对象方式小记
- spring学习(五)—通过注解方式创建对象和注入属性
- spring学习教程7-spring容器创建bean对象的方式以及如何处理多个spring配置文件
- spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象
- SSH与SSM学习之Struts204——结果跳转方式
- 学习javascript面向对象 掌握创建对象的9种方式
- java学习日志之四种创建对象方式概述
- SSH与SSM学习之Spring14——动态代理之自己的AOP框架
- Spring学习心得(3)-- Spring容器创建对象的时机
- SSH与SSM学习之Spring08——Spring属性注入之复杂类型注入
- Spring底层创建对象的几种方式
- Spring学习-03:ApplicationContext加载配置文件的两种方式
- SSH与SSM学习之SSH整合07——Spring整合c3p0连接池
- 使用Spring创建对象的方式
- SSH与SSM学习之Struts205——访问servletAPI方式