3.装配Bean 基于XML
一、实例化方式
3种bean实例化方式:默认构造、静态工厂、实例工厂
1.默认构造
<bean id="" class=""> 必须提供默认构造
2. 静态工厂
l 常用与spring整合其他框架(工具)
l 静态工厂:用于生成实例对象,所有的方法必须是static
<bean id="" class="工厂全限定类名" factory-method="静态方法">
UserService.java
package com.jd.inject.static_factory;
public interface UserService {
public void addUser();
}
UserServiceImpl.java
package com.jd.inject.static_factory.impl;
import com.jd.inject.static_factory.UserService;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("b_static_factory add user");
}
}
MyBeanFactory.java
package com.jd.inject.static_factory; import com.jd.inject.static_factory.impl.UserServiceImpl; /**
* @author weihu
* @date 2018/8/12/012 18:54
*/
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 确定静态方法名,还是创建UserServiceImpl对象
-->
<bean id="userServiceId" class="com.jd.inject.static_factory.MyBeanFactory" factory-method="createService"></bean>
</beans>
测试类 TestStaticFactory.java
package com.jd.test; import com.jd.inject.static_factory.MyBeanFactory;
import com.jd.inject.static_factory.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author weihu
* @date 2018/8/12/012 18:57
*/
public class TestStaticFactory { @Test
public void testStaticFactory(){
//自定义工厂
UserService userService = MyBeanFactory.createService();
userService.addUser(); } @Test
public void testSpringStaticFactory(){
//spring工厂
String xmlPath="com/jd/inject/static_factory/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userServiceId = applicationContext.getBean("userServiceId", UserService.class);
userServiceId.addUser();
}
}
3. 实例工厂
实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。
UserService.java
package com.jd.inject.factory;
public interface UserService {
public void addUser();
}
UserServiceImpl.java
package com.jd.inject.factory.impl;
import com.jd.inject.factory.UserService;
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("factory add user");
}
}
MyBeanFactory.java
package com.jd.inject.factory; import com.jd.inject.factory.impl.UserServiceImpl; /**
* @author weihu
* @date 2018/8/12/012 18:54
*/
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.jd.inject.factory.MyBeanFactory"></bean>
<!--获得userservice
factory-bean确定工厂实例
factory-method 确定普通方法
-->
<bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>
</beans>
测试类
TestFactory.java
package com.jd.test; import com.jd.inject.factory.MyBeanFactory;
import com.jd.inject.factory.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author weihu
* @date 2018/8/12/012 19:16
*/
public class TestFactory { @Test
public void testFactory(){
MyBeanFactory myBeanFactory=new MyBeanFactory();
UserService userService = myBeanFactory.createService();
userService.addUser(); } @Test
public void testSpringFactory(){
String xmlPath="com/jd/inject/factory/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userServiceId = applicationContext.getBean("userServiceId", UserService.class);
userServiceId.addUser(); }
}
3.装配Bean 基于XML的更多相关文章
- Spring装配Bean之XML装配bean
在Spring刚出现的时候,XML是描述配置的主要方式,在Spring的名义下,我们创建了无数行XML代码.在一定程度上,Spring成为了XML的同义词. 现在随着强大的自动化配置和Java代码的配 ...
- 第2章—装配Bean—通过XML装配Bean
通过XML装配Bean 尽管我们在生成Bean的过程中可以用到很多方法,但我们依然需要Spring的XML配置来完善更多的需求,下面就来介绍下XML装配Bean的过程是怎样的. 3.1创建XML配 ...
- Spring IOC容器装配Bean_基于XML配置方式
开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...
- IOC装配Bean(XML方式)
Spring框架Bean实例化的方式 提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 无参数构造方法的实例化 <!-- 默认情况下使用的就是 ...
- IoC容器装配Bean(xml配置方式)(Bean的生命周期)
1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...
- 6.装配Bean基于注解
1.注解:就是一个类,使用@注解名称 开发中:使用注解 取代 xml配置文件. @Component取代<bean class=""> @Component(" ...
- Spring基础篇——通过Java注解和XML配置装配bean(转载)
作者:陈本布衣 出处:http://www.cnblogs.com/chenbenbuyi 本文版权归作者和博客园共有,欢迎转载分享,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留 ...
- Spring实战2:装配bean—依赖注入的本质
主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...
- SpringInAction读书笔记--第2章装配Bean
实现一个业务需要多个组件相互协作,创建组件之间关联关系的传统方法通常会导致结构复杂的代码,这些代码很难被复用和单元测试.在Spring中,对象不需要自己寻找或创建与其所关联的其它对象,Spring容器 ...
随机推荐
- !!!常用CSS代码块
图片排满一行.左右两端无间隙. <style type="text/css"> .img_abc{float:left;width:30%;margin-left:5% ...
- centos6.5部署redmine3.2
ruby 2.1 + rails 4.2+ mysql 5.6 +centos6.5 + rvm 1.29 1.基本的软件环境 yum -y install libyaml-devel zlib-de ...
- leetcode438
public class Solution {;public IList<int> FindAnagrams(string s, string p) { List<int> l ...
- 编织织物的knit course direction and knit wale direction
来自:http://www.definetextile.com/2013/04/course-wale.html
- jschDemo
jsch是java的sftp实现 import com.jcraft.jsch.*; import java.io.OutputStream; public class JschStart { pub ...
- React页面插入script
项目中遇到插入广告的需要,而广告的信息只是一个url链接,这个链接返回的时一个js,和以前插入广告有点不同.所有找了很多方式. 先来展示广告链接返回的信息: 假设广告链接为:http://192.16 ...
- 在IDEA中停止和关闭SonarLint自动检查,手动运行SonarLint检查代码
关闭SonarLint自动检查代码 有时敲一行代码SonarLint插件就会自动检查,让人感觉很不舒服,还会使电脑卡顿: 依次点击:File -> Settings 或直接Ctrl+Alt+S ...
- R语言-地图
1.maps包的map()函数 >map('world', fill = TRUE,col=heat.colors(10)) #世界地图 >map("state", i ...
- 导出word文档 通过DocX组件
根据DocX官方描述如下: In the application development process, it uses COM libraries and requires MS Word or ...
- python实现FTP服务器
https://www.cnblogs.com/huangxm/p/6274645.html