Spring入门(4),装配Bean,基于xml
来源:互联网 发布:最全双色球缩水软件 编辑:程序博客网 时间:2024/05/18 00:01
注意:spring3不支持jdk1.8,如果要用jdk1.8请使用spring4。本例子使用的spring3。
bean的实例化方式:默认构造,静态工厂,实例化工厂。
默认构造: <bean id = "useradd" class = "com.cn.user.UserAdd">
就会提供默认构造。
静态工厂:
常用于spring 整合其他框架(工具)。用于生成实例对象,所有方法都是static。
<bean id = "useradd" class = "com.cn.user.UserAdd" factory-metod="add'>
实例工厂:
必须现有实例化对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。
下面我们来看几个小例子:
1.静态工厂
UserService.java
package com.cn.inject.static_factory;public interface UserService { public void addUse();}
UserServiceImpl.java
package com.cn.inject.static_factory;public class UserServiceImpl implements UserService { @Override public void addUse() { System.out.println("static factory"); }}
MyBeanFactory.java
package com.cn.inject.static_factory;/** * 静态工厂 * */public class MyBeanFactory { /** * 创建实例 * @return */ public static UserService createService(){ return new UserServiceImpl(); }}
beans.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"> <!-- 将我们静态工厂创建的实例交于spring class 确定静态工厂的全限定类名 factory-method 确定静态方法名 --> <bean id="userServiceId" class="com.cn.inject.static_factory.MyBeanFactory" factory-method="createService"></bean></beans>
TestStaticFactory.java
package com.cn.inject.static_factory;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestStaticFactory { @Test //自定义工厂 public void demo1(){ UserService userService = MyBeanFactory.createService(); userService.addUse(); } @Test public void demo2(){ //spring工厂 String xmlPath = "com/cn/inject/static_factory/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserService userService = applicationContext.getBean("userServiceId",UserService.class); userService.addUse(); }}
2.实例工厂
UserService.java
package com.cn.inject.factory;public interface UserService { public void addUse();}
UserServiceImpl.java
package com.cn.inject.factory;public class UserServiceImpl implements UserService { @Override public void addUse() { System.out.println("factory"); }}
MyBeanFactory.java
package com.cn.inject.factory;/** * 实例工厂 * */public class MyBeanFactory { /** * 创建实例 * @return */ public UserService createService(){ return new UserServiceImpl(); }}
beans.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="myBeanFactoryId" class="com.cn.inject.factory.MyBeanFactory"></bean> <!-- 创建对象 --> <bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean></beans>
TestFactory.java
package com.cn.inject.factory;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactory { @Test //自定义工厂 public void demo1(){ //自定义实例工厂 //1.创建工厂 MyBeanFactory myBeanFactory = new MyBeanFactory(); //2.通过工厂实例来获取对象 UserService userService = myBeanFactory.createService(); userService.addUse(); } @Test public void demo2(){ //spring工厂 String xmlPath = "com/cn/inject/factory/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserService userService = applicationContext.getBean("userServiceId",UserService.class); userService.addUse(); }}
1 0
- Spring入门(4),装配Bean,基于xml
- spring装配Bean(基于xml)
- Spring 装配Bean 基于XML
- spring装配Bean(基于xml)
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring In Action(二):基于XML配置装配bean
- Spring(1-1、基于xml装配Bean)
- Spring(1-2、基于xml装配Bean)
- 装配Bean基于XML
- spring基础-Bean的装配方式(一)基于xml的装配
- 4、spring入门—Spring Bean的装配(下)
- Spring(3、基于注解装配Bean)
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring入门(Bean自动装配)
- Spring入门(Bean装配,Autowired注解)
- Spring入门(Bean装配,@Qualifier)
- Spring(4)装配Bean
- spring笔记(二)---基于XML的Bean装配
- Java线程锁(一)——wait(),notify(),synchnorized使用
- Android 运行时权限勾选不再询问后的处理
- RAII理念 应用于MFC 按钮使能控制
- 从Java视角理解系统结构(一)CPU上下文切换
- 如何高效的使用postman
- Spring入门(4),装配Bean,基于xml
- 利用JQuery实现左侧菜单栏可折叠功能
- 树莓派3 opencv调用摄像头(C++和Python)
- hdu3605 二分图多重匹配(hungary算法)
- ABI(Application Binary Interface)
- LeetCode #415: Add Strings
- 最新稳定版本:
- 深度学习框架Keras使用心得
- unity 生成assetbundle文件