注入bean有两种方式:

注入其他bean:
方式一
<bean id="orderDao" class="cn.itcast.service.OrderDaoBean"/>
<bean id="orderService" class="cn.itcast.service.OrderServiceBean">
<property name="orderDao" ref="orderDao"/>
</bean>
方式二(使用内部bean,但该bean不能被其他bean使用)
<bean id="orderService" class="cn.itcast.service.OrderServiceBean">
<property name="orderDao">
<bean class="cn.itcast.service.OrderDaoBean"/>
</property>
</bean>

  

一般我们的工程在service层依赖dao层的实现来实现业务逻辑。

service层:

public class PersonServiceImpl implements PersonService {

	private PersonDao personDao;

	@Override
public void save() {
personDao.save();
System.out.println("service : " + " save 方法");
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}

  

dao层:

public class PersonDaoImpl implements PersonDao {
@Override
public void save() {
System.out.println("dao层的save方法");
}
}

  

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-2.5.xsd"> <bean id="personDaoImpl" class="cn.gbx.dao.PersonDaoImpl"></bean>
<bean id="personServiceImpl" class="cn.gbx.serviceimpl.PersonServiceImpl" >
<property name="personDao" ref="personDaoImpl"></property>
</bean>
</beans>

  

测试方法:

public class SpringTest {
@Test
public void spring1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService ps = (PersonService)ctx.getBean("personServiceImpl");
ps.save();
}
}

  

然后我们就实现了Spring对bean对象的依赖注入。 service层所依赖的dao层的PersonDao对象不是由service本身去创建管理,而是交给了第三方容器Spring去管理。

那么Spring是如何管理的呢?

我们可想而知

1: 首先必须解析XML 将 <bean>标签和 <property>标签都解析出来

2: 利用反射实例话对象

3: 利用内省将实例化完成的对象注入到有依赖的对象里面

我们可以自己模拟实现:

package cn.gbx.myExample;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader; public class MyClassPathXmlApplicationContext { private List<DefBean> defBeans = new ArrayList<DefBean>(); //存储<bean>标签的信息
private Map<String, Object> singltons = new HashMap<String, Object>(); //存储读出来的对象 public MyClassPathXmlApplicationContext() {
super();
} public MyClassPathXmlApplicationContext(String filename) {
this.readXML(filename);
this.instanceBean();
this.injectProperty();
}
//利用内省将属性注入
private void injectProperty() {
for (DefBean defBean : defBeans) {
Object bean = singltons.get(defBean.getId());
if (bean != null) {
//要想将依赖的对象注入,我们就要通过内省来操作对象的属性
try {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] ps = beanInfo.getPropertyDescriptors(); for (DefProperty p : defBean.getDefPropertys()) {
for (PropertyDescriptor propertyDes : ps) {
if (propertyDes.getName().equals(p.getName())) {
Method setter = propertyDes.getWriteMethod();
if (setter != null) {
Object value = null;
if (p.getRef() != null && !"".equals(p.getRef())) {
value = singltons.get(p.getRef());
} else { //基本数据类型利用beanUtils实现转化
value = ConvertUtils.convert(p.getValue(),propertyDes.getPropertyType());
}
setter.setAccessible(true); //这样即使set方法是私有的都可以访问
setter.invoke(bean, value); //把引用对象注入到bean属性里面
}
break;
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
//利用反射实例化对象
private void instanceBean() {
for (DefBean bean : defBeans) {
//输出测试读取的xml
/*System.out.println(bean.getId() + " : " + bean.getClassName());
System.out.println("------------------------");
for (DefProperty p : bean.getDefPropertys()) {
System.out.println("Property : name = " + p.getName() + " : ref = " + p.getRef());
}
*/ if (bean.getClassName() != null && !"".equals(bean.getClassName())) {
try {
singltons.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
} private void readXML(String filename) {
SAXReader reader = new SAXReader();
Document document = null;
URL xmlPath = this.getClass().getClassLoader().getResource(filename);
try {
document = reader.read(xmlPath);
//创建命名空间
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans"); //创建查询路径
XPath xsub = document.createXPath("//ns:beans/ns:bean");
xsub.setNamespaceURIs(nsMap); // 设置命名空间 List<Element> elements = xsub.selectNodes(document); DefBean defBean = null;
for (Element e : elements) {
String id = e.attributeValue("id");
String className = e.attributeValue("class");
defBean = new DefBean(id, className); //为<bean>节点设置查询路径
XPath xPropertySub = e.createXPath("ns:property");
xPropertySub.setNamespaceURIs(nsMap);
List<Element> propertys = xPropertySub.selectNodes(e); DefProperty defProperty = null;
for (Element e2 : propertys) {
String name = e2.attributeValue("name");
String ref = e2.attributeValue("ref");
String value = e2.attributeValue("value");
defProperty = new DefProperty(name, ref, value);
defBean.getDefPropertys().add(defProperty);
} defBeans.add(defBean);
}
} catch (Exception e) {
e.printStackTrace();
} }
public Object getBean(String key) {
return singltons.get(key);
} }

  

<?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-2.5.xsd"> <bean id="personDaoImpl" class="cn.gbx.dao.PersonDaoImpl"></bean>
<bean id="personServiceImpl" class="cn.gbx.serviceimpl.PersonServiceImpl" >
<property name="personDao" ref="personDaoImpl"></property>
<property name="name" value="ok-gbx"></property>
<property name="id" value="22"></property>
</bean>
</beans>

  

Spring的DI(Ioc) - 注入bean 和 基本数据类型的更多相关文章

  1. Spring的DI(Ioc) - 注入集合类型

    1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...

  2. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

  3. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  4. Spring源码-IOC部分-Bean实例化过程【5】

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...

  5. spring的DI.IoC是什么

    最近要搞spring的单元测试,不得已啊啊啊啊啊啊啊啊啊又要开始搞spring…… 日目晶…… 搞这几个概念,先甩一部分代码: UserDao 接口 package com.itheima.ioc; ...

  6. 3、Spring的DI依赖注入

    一.DI介绍 1.DI介绍 依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入. Spring容器管理容器中Bean之间的依赖关系,Spring使用一种被称为&qu ...

  7. Spring框架(2)---IOC装配Bean(xml配置方式)

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  8. Spring的几种注入bean的方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式).   使用属性的se ...

  9. Spring(三)之Ioc、Bean、Scope讲解

    Spring容器是Spring Framework的核心.容器将创建对象,将它们连接在一起,配置它们,并管理从创建到销毁的整个生命周期.Spring容器使用DI来管理组成应用程序的组件.这些对象称为S ...

随机推荐

  1. java网络编程之TCP通讯

    java中的网络编程之TCP协议的详细介绍,以及如何使用,同时我在下面举2例说明如何搭配IO流进行操作, /* *TCP *建立连接,形成传输数据的通道: *在连接中进行大数据量传输: *通过三次握手 ...

  2. NLog

    C# 使用NLog记录日志 C#第三方日志库Nlog NLog类库使用探索——详解配置 使用Nlog记录日志到数据库 NLog文章系列——系列文章目录以及简要介绍 NLog日志框架简写用法(write ...

  3. Valid Anagram

    class Solution { public: bool isAnagram(string s, string t) { if(t=="") return s=="&q ...

  4. 身为运维工程师怎么用Nginx部署DokuWiki

    运维人员按区域组织,人员分散,集中培训成本比较高: 新入职运维人员除了培训手册,没有其它渠道可以持续深入了解公司产品: 运维人员的知识存在各自脑袋里,缺少有效的渠道来传播和分享: 运维知识体系需要积累 ...

  5. Windows驱动开发(中间层)

    Windows驱动开发 一.前言 依据<Windows内核安全与驱动开发>及MSDN等网络质料进行学习开发. 二.初步环境 1.下载安装WDK7.1.0(WinDDK\7600.16385 ...

  6. spring mvc+myBatis配置详解

    一.spring mvc Spring框架(框架即:编程注解+xml配置的方式)MVC是Spring框架的一大特征,Spring框架有三大特征(IOC(依赖注入),AOP(面向切面),MVC(建模M- ...

  7. javascript 中==和===的区别

        对于JavaScript中比较运算符,可能大家用的比较多的是“==”.对于“===”很多人可能很陌生.=== 表示恒等,首先比较两边的变量数据类型是否相等,其次比较两边的变量的数值是否相等:= ...

  8. python: shutil模块 -拷贝文件

    import shutil #拷贝文件 #存在文档1文件 shutil.copyfile('文档1','新文件') 随机验证码-4位 import random random_code='' for ...

  9. linux 时钟源初步分析linux kernel 时钟框架详细介绍

    初步概念: 看datasheet的关于时钟与定时器的部分, FCLK供给cpu, HCLK供给AHB总线设备(存储器控制器,中断控制器.LCD控制器.DMA.USB主机控制器等), PCLK供给APB ...

  10. Cookie案例

    实现的功能是:在页面上点击,各种书名,跳转到另一个页面,在点解return 回到原来的页面,并且最下面出现刚才点击的书名:进行多次操作,都会出现刚刚点击的书名: 类似于:在浏览器上显示你登录的记录: ...