Spring(四)基于XML装配bean(实例化方式)
来源:互联网 发布:飞利浦呼吸机读卡软件 编辑:程序博客网 时间:2024/04/30 22:37
基于xml装配bean 的实例化方式共有三种
1.默认构造
2.静态工厂
3.实例化工厂
1默认构造
1.1 说明
- 用于生成实例化对象,必须未重写bean的默认构造方法。
1.2 xml配置
<bean id="" class=""> 必须提供默认构造方法
id 为bean的别名,用于之后从spring容器获得实例时使用的
class 为需要创建实例的全限定类名
1.3 目标类
创建目标类的过程
UsersService接口和实现类UsersServiceImpl
使用junit测试
重写UsersServiceImpl的构造方法
使用junit测试
测试代码如下:
UsersService接口 仅仅为了测试 写了一个show方法
package com.scx.test;public interface UsersService { public void show();}
UsersService实现类UsersServiceImpl ,重写show方法,输出UsersService,使用的是默认的构造方法
package com.scx.test;public class UsersServiceImpl implements UsersService { @Override public void show() { System.out.println("默认构造创建实例"); }}
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="UsersServiceId" class="com.scx.test.UsersServiceImpl"></bean></beans>
使用junit测试
package com.scx.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test public void testConstract(){ String xmlPath="com/scx/test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UsersService usersService=applicationContext.getBean("UsersServiceId",UsersService.class); usersService.show(); }}
运行结果:
成功输出了结果
重写UsersServiceImpl的构造方法
package com.scx.test;public class UsersServiceImpl implements UsersService { @Override public void show() { System.out.println("UsersService"); } //重写UsersServiceImpl构造方法 public UsersServiceImpl(String str){ }}
再次使用junit测试
并未输出结果
比较可知:
若想使用默认构造实例化,必须未重写默认的构造方法
2静态工厂
2.1 说明
- 常用于Spring整合其它框架(工具)
- 用于生成实例化对象,所有的方法必须是static
2.2 XML配置
<bean id="" class="" factory-method="">
id 为bean的别名,用于之后从spring容器获得实例时使用的
class 为需要创建工厂的全限定类名
factory-method 为工厂的静态方法
2.3 目标类
创建目标类的过程
UsersService接口和实现类UsersServiceImpl
静态工厂MyStaticFactory
使用junit测试
UsersService接口和实现类UsersServiceImpl代码和上面的一样就不再贴了
静态工厂MyStaticFactory ,其中的静态方法createUsersService,返回一个UsersServiceImpl 实例
package com.scx.factory.static_test;/* * 静态工厂创建实例 */public class MyStaticFactory { //静态方法 public static UsersServiceImpl createUsersService(){ return new UsersServiceImpl(); }}
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"> <!-- >id 为bean的别名,用于之后从spring容器获得实例时使用的 >class 为需要创建工厂的全限定类名 >factory-method 为工厂的静态方法 --> <bean id="UsersServiceId" class="com.scx.factory.static_test.MyStaticFactory" factory-method="createUsersService"></bean></beans>
使用junit测试
package com.scx.factory.static_test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test public void testStaticFactory(){ String xmlPath="com/scx/factory/static_test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UsersService usersService=applicationContext.getBean("UsersServiceId",UsersService.class); usersService.show(); }}
由于在静态工厂中的静态方法中,我们自己new 的UsersServiceImpl 对象,所以重写构造方法与否无影响。
运行结果:
3实例工厂
3.1 说明
- 提供的所有方法都是非静态的
- 实例化之前必须先有工厂的实例化对象,通过实例化对象创建对象
3.2 XML配置
<bean name="" class=""></bean><!-- 创建工厂实例--><bean name="" factory-bean="" factory-method=""></bean><!-- factory-bean:确定工厂实例 factory-method:确定普通方法 -->
3.3 目标类
创建目标类的过程
UsersService接口和实现类UsersServiceImpl
实例工厂MyFactory
使用junit测试
UsersService接口和实现类UsersServiceImpl代码和上面的一样就不再贴了
实例工厂MyFactory
package com.scx.factory.static_test;/* * 实例工厂创建实例 */public class MyFactory { //普通方法 public UsersServiceImpl createUsersService(){ return new UsersServiceImpl(); }}
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 name="MyFactoryId" class="com.scx.factory.static_test.MyFactory"></bean> <bean name="UsersServiceId" factory-bean="MyFactoryId" factory-method="createUsersService"></bean></beans>
junit测试代码
package com.scx.factory.static_test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test public void testFactory(){ String xmlPath="com/scx/factory/static_test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UsersService usersService=applicationContext.getBean("UsersServiceId",UsersService.class); usersService.show(); }}
运行结果:
- Spring(四)基于XML装配bean(实例化方式)
- 基于XML方式的Bean装配实例
- spring装配Bean(基于xml)
- Spring 装配Bean 基于XML
- spring装配Bean(基于xml)
- spring基础-Bean的装配方式(一)基于xml的装配
- Spring实例解析之XML装配Bean
- Spring学习笔记(四) --- 装配Bean之通过XML装配
- 装配Bean基于XML
- Spring中Bean的基于xml的三种实例化方式
- Spring自动装配Bean属性的四种方式
- Spring学习(四)-Bean的三种装配方式
- spring笔记(二)---基于XML的Bean装配
- Spring入门(4),装配Bean,基于xml
- Spring学习历程---Bean基于xml的装配
- Spring(五)基于XML装配bean(作用域)
- Spring In Action(二):基于XML配置装配bean
- Spring(1-1、基于xml装配Bean)
- 通过HTML5 网页代码获取设备Sensor值
- arttemplate+ajax 转 vue
- Android开发之Fragment的加载方式
- eclipce创建maven项目
- EM-LPC1768开发板原理图
- Spring(四)基于XML装配bean(实例化方式)
- python爬虫简单的抓页面图片并保存到本地
- View滑动冲突处理方法(外部拦截法、内部拦截法)
- overridePendingTransition函数
- http://www.mat.univie.ac.at/~neum/glopt.html
- hdu 1158 Employment Planning
- Android静态安全检测 -> 强制类型转换本地拒绝服务漏洞
- json-server快速“伪造”后台接口
- 机器学习中的PR曲线和ROC曲线