- 浏览: 416109 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (71)
- spring-->>备忘笔记 (17)
- struts-->>备忘笔记 (4)
- hibernate-->>备忘笔记 (6)
- J2EE (10)
- linux (4)
- AOP (3)
- ibatis (1)
- bean (1)
- scop (1)
- IOC (2)
- Central Authentication Service (5)
- javascript-->>备忘笔记 (12)
- 程序人生 (2)
- MongoDB-->>备忘笔记 (2)
- java-->>静态与非静态语句块 (1)
- java-->>泛型 (1)
- java-->>线程 (4)
- java-->>堆栈 (1)
- java-->>jvm (1)
- java-->>工具类 (3)
- mysql-->>备忘笔记 (2)
- 设计模式-->>备忘笔记 (1)
- oracle-->>备忘笔记 (0)
- 互联网->>电子商务 (5)
最新评论
-
lihaiming:
shengouqiang 写道例如有两个线程同时执行(没有sy ...
java之yield(),sleep(),wait()区别详解-备忘笔记 -
zjxchase:
你的onlyMoney这个方法好像不太好用
js 之 Number 工具类 -
shengouqiang:
例如有两个线程同时执行(没有synchronized)一个线程 ...
java之yield(),sleep(),wait()区别详解-备忘笔记 -
u011028234:
楼主你这例子里边的SampleResource实体没有额?
spring 控制反转与依赖注入原理-学习笔记 -
yueerba:
<table class="bbcode&qu ...
spring 控制反转与依赖注入原理-学习笔记
在Spring中有两个非常重要的概念,控制反转和依赖注入;控制反转将依赖对象的创建和管理交由Spring容器,而依赖注入则是在控制反转的基础上将Spring容器管理的依赖对象注入到应用之中;
所谓依赖注入:在运行期,由外部容器动态将依赖对象注入到组件中。
XML文件解析 + Java反射技术;
首先是XML文件的解析(dom4j),Spring框架对于配置文件的选择是XML文件,根据Spring的规范,配置文件的命名是没有特殊要求的,只是在文件的放置位置上有两种选择;类路径下或者操作系统文件目录下(大多数情况是放到类路径下)。
对于Spring的控制反转和依赖注入来说,唯一使用的是配置文件中的<bean>标签,通过这个标签,Spring就完成了对象的创建和依赖对象的注入工作;
1、首先对于配置文件中的<bean>节点,在Spring框架中存在一个对用的定义接口,叫做BeanDefinition;子啊个类定义了获得<bean>节点中出现的所有属性的方法,例如classNam、scope、factory-method、lazy-init 等等属性;
2、对于<bean>节点的子节点property则完成了属性注入的功能;属性注入有三种方式,构造器注入、属性setter方法注入和注解方式注入;
3、如果是setter方法注入,对于类属性XML配置文件中有两种方法,一是使用property节点的ref属性,一是使用property几点的子节点bean进行内部bean配置;如果是对于基本数据类型进行配置,那么要是用property节点的value属性;
定义自己的关于bean节点、property节点的pojo类文件;
使用注入DOM4J等开源包讲配置文件解析读入;
使用Java的反射技术讲配置文件中的信息setter到我们需要的属性中去;common-beanutils.jar
<context:component-scan base-package="com.sample"/> <bean id="personService" class="com.spring.junit.test.impl.PersonServiceImpl"></bean> <bean id="stockService" class="com.spring.junit.test.impl.StockServiceImpl"></bean> <bean id="personServiceFactory" class="com.spring.junit.test.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBeanFactory"></bean> <bean id="personServiceFactory2" class="com.spring.junit.test.impl.PersonServiceBeanFactory"></bean> <bean id="stockServiceFactory" factory-bean="personServiceFactory2" factory-method="createStockServiceBeanFactory"></bean> <bean id="randomBean" class="com.spring.junit.bean.StaticFactoryBean" factory-method="createRandom" scope="prototype"></bean> <!-- 集合类型的注入 --> 通过setter方法注入 <bean id="user" class="com.sample.bean.User"></bean> <bean id="personDao" class="com.sample.dao.impl.PersonDaoBeanImpl"></bean> <bean id="personService" class="com.sample.service.impl.PersonServiceBeanImpl"> <property name="personDao" ref="personDao"></property> <property name="name" value="jackjson_xu_test"></property> <property name="id" value="108"></property> <property name="sets"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="lists"> <list> <value>第一個list元素</value> <value>第二個list元素</value> <value>第三個list元素</value> </list> </property> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> <entry key="key-4" value="value-4"></entry> </map> </property> <property name="users"> <map> <entry key="U_1001"> <ref bean="user"/> </entry> <entry key="U_1002"> <ref bean="user"/> </entry> </map> </property> </bean> <!-- 采用内部bean的方式注入 --> <bean id="personService" class="com.sample.service.impl.PersonServiceBeanImpl"> <property name="personDao"> <bean class="com.sample.dao.impl.PersonDaoBeanImpl"/> </property> <property name="name" value="jackjson_xu_test"></property> <property name="id" value="100"></property> </bean> <!-- 构造器注入方式 --> <bean id="personDao" class="com.sample.dao.impl.PersonDaoBeanImpl"></bean> <bean id="personService2" class="com.sample.service.impl.PersonServiceBeanImpl2" autowire="byType"> <constructor-arg index="0" type="com.sample.dao.IPersonDao" ref="personDao"></constructor-arg> <constructor-arg index="1" type="java.lang.String" value="http://www.woyo.com"></constructor-arg> </bean>
package com.sample.junit;
import java.util.ArrayList; import java.util.List; /** * Spring xml 属性的方法 * @author DY * */ public class BeanDefinition { private String id; private String className; private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>(); public BeanDefinition(String id, String className) { this.id = id; this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<PropertyDefinition> getPropertys() { return propertys; } public void setPropertys(List<PropertyDefinition> propertys) { this.propertys = propertys; } }
package com.sample.junit;
/** * Spring xml bean 子节点property属性方法 * * @author DY * */ public class PropertyDefinition { private String name; private String ref; private String value; public PropertyDefinition(String name, String ref, String value) { this.name = name; this.ref = ref; this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
package com.sample.junit;
import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; 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.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; /** * spring装配applicationContext.xml文件 * @author DY * */ public class SampleClassPathXMLApplicationContext { private Logger logger = Logger.getLogger(SampleClassPathXMLApplicationContext.class); private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons = new HashMap<String, Object>(); public SampleClassPathXMLApplicationContext(String filename) { this.readXML(filename); this.instanceBeans(); //bean的实例化 Class.forName().newInstance() this.annotationInject();//注解 this.injectObject(); //bean对象的属性注入值 } /** * 注解处理器 * 如果注解SampleResouce配置了name属性,则根据name所指定的名称获取要注入的实例引用 * 如果注解SampleResouce没有配置name属性,则根据属性所属类型来扫描配置文件获取要注入的实例引用 */ private void annotationInject() { for (String beanName : sigletons.keySet()) { Object bean = sigletons.get(beanName); if (bean != null) { this.propertyAnnotation(bean); this.fieldAnnotation(bean); } } } /** * 处理在所有set方法加入的注解 * @param bean 处理的bean对象 */ private void propertyAnnotation(Object bean) { try { //获取其属性的描述 PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for (PropertyDescriptor properdesc : ps) { //获取属性的setter方法 Method setter = properdesc.getWriteMethod(); //setter方法上是否存在注解 if (setter != null && setter.isAnnotationPresent(SampleResource.class)) { //获取当前注解,判断name属性是否为空 SampleResource resouce = setter.getAnnotation(SampleResource.class); Object value = null; if (resouce.name() != null && !"".equals(resouce.name())) { value = sigletons.get(resouce.name()); setter.setAccessible(true); setter.invoke(bean, value);//把引用对象注入到属性 } else {//如果当前属性没有指定name,则根据类型匹配 value = sigletons.get(resouce.name()); if (value == null) { for (String key : sigletons.keySet()) { //判断当前属性所属类型是否在配置文件中存在 if (properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())) { value = sigletons.get(key); //获取类型匹配的实例对象 } } } //允许访问private方法 setter.setAccessible(true); //把引用对象注入属性 setter.invoke(bean, value); } } } } catch (Exception e) { logger.error(e.getLocalizedMessage()); } } /** * 处理在字段上的注解 * @param bean */ private void fieldAnnotation (Object bean) { try { //获取全部属性对象数组 Field[] fields = bean.getClass().getFields(); for (Field field : fields) { if (field.isAnnotationPresent(SampleResource.class)) { SampleResource resouce = field.getAnnotation(SampleResource.class); Object value = null; if (resouce.name() != null && !"".equals(resouce.name())) { value = sigletons.get(resouce.name()); } else { value = sigletons.get(field.getName()); if (value == null) { for (String key : sigletons.keySet()) { //根据字段类型匹配 if (field.getType().isAssignableFrom(sigletons.get(key).getClass())) { value = sigletons.get(key); break; } } } } field.setAccessible(true); field.set(bean, value); } } } catch (Exception e) { e.getLocalizedMessage(); logger.error("字段注解解析异常:" + e.getLocalizedMessage()); } } /** * 为bean对象的属性注入值 */ private void injectObject() { for (BeanDefinition beanDefinition : beanDefines) { Object bean = sigletons.get(beanDefinition.getId()); if (bean != null) { try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for (PropertyDefinition propertyDefinition : beanDefinition.getPropertys()) { for (PropertyDescriptor properdesc : ps) { if (propertyDefinition.getName().equals(properdesc.getName())) { Method setter = properdesc.getWriteMethod();// 获取属性的setter方法 if (setter != null) { Object value = null; if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim())) { value = sigletons.get(propertyDefinition.getRef()); } else { value = ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType()); } setter.setAccessible(true);//私有方法给与访问权限 setter.invoke(bean, value);// 把引用对象注入到属性 } break; } } } } catch (Exception e) { } } } } /** * 完成bean的实例化 */ private void instanceBeans() { for (BeanDefinition beanDefinition : beanDefines) { try { if (beanDefinition.getClassName() != null && !"".equals(beanDefinition.getClassName().trim())) sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } catch (Exception e) { e.printStackTrace(); } } } /** * 读取xml配置文件 * * @param filename */ private void readXML(String filename) { SAXReader saxReader = new SAXReader(); Document document = null; try { URL xmlpath = this.getClass().getClassLoader().getResource(filename); document = saxReader.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");// 创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap);// 设置命名空间 List<Element> beans = xsub.selectNodes(document);// 获取文档下所有bean节点 for (Element element : beans) { String id = element.attributeValue("id");// 获取id属性值 String clazz = element.attributeValue("class"); // 获取class属性值 BeanDefinition beanDefine = new BeanDefinition(id, clazz); XPath propertysub = element.createXPath("ns:property"); propertysub.setNamespaceURIs(nsMap);// 设置命名空间 List<Element> propertys = propertysub.selectNodes(element); for (Element property : propertys) { String propertyName = property.attributeValue("name"); String propertyref = property.attributeValue("ref"); String propertyValue = property.attributeValue("value"); PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref, propertyValue); beanDefine.getPropertys().add(propertyDefinition); System.out.println("propertyName:" + propertyName + "|propertyref:" + propertyref + "|propertyValue:" + propertyValue); } beanDefines.add(beanDefine); } } catch (Exception e) { e.printStackTrace(); } } /** * 获取bean实例 * * @param beanName * @return */ public Object getBean(String beanName) { return this.sigletons.get(beanName); } }
package com.sample.junit;
import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sample.service.IPersonService; public class SpringTest { static ApplicationContext ctx = null; @BeforeClass public static void setUpBeforeClass() throws Exception { ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); } @Test public void instanceSpring(){ IPersonService personService = (IPersonService)ctx.getBean("personService"); System.out.println(personService); personService.save(); } }
评论
|
发表评论
-
[spring]事务传播级别隔离级别以及高并发下的应用经验
2012-02-12 01:06 10044事务是逻辑处理原子 ... -
spring声明式事务策略 aop拦截-学习笔记
2011-12-16 17:42 2171声明式事务管理: S ... -
spring中的@Transaction配置详解-学习笔记
2011-12-16 17:42 21342spring中的@Transaction配置详解 1、 ... -
spring AspectJ的Execution表达式-备忘笔记
2011-12-16 23:57 14031Aspectj切入点语法定义 在使用spring框 ... -
spring 配置文件实现AOP-学习笔记
2011-12-15 16:15 1557Spring 对AOP的支持I: Aspect默认情况 ... -
spring JDK的Proxy技术实现AOP功能和CGBLB-学习笔记
2011-12-15 16:05 1841动态代理类(以下简称为代理类)是一个实现在创建类时在运行时指定 ... -
spring容器自动扫面管理bean-学习笔记
2011-12-15 16:42 1525通过在classpath自动扫描方式把组件纳入sp ... -
spring自动装配注解模式-学习笔记
2011-12-15 16:43 3406spring 自动装配注解模式 1、什么是自动装配 ... -
spring的注入依赖之构造器注入- 学习笔记
2011-12-15 16:44 2146Spring的注入依赖(DI)主要有三种注入方式, ... -
spring Bean的作用域scope-学习笔记
2011-12-14 22:09 1811Spring容器最初提供了 ... -
添加xml文件自动提示方法,以spring配置文件为例-备忘录
2011-12-14 22:01 1027用eclipse,在导入相应的包后(有人说不导包也有提示,因为 ... -
基于Struts、Hibernate、Spring 的学习框架-笔记备忘
2011-12-13 20:24 1111基于Struts、Hibernate、Spring 的学 ... -
spring声明式事务策略 aop拦截-学习笔记
2011-12-13 20:20 1503声明式事务管理: Spring提供了声明式事务管理 ... -
三种实例化Spring中Bean对象的方式
2011-12-13 20:17 16331、使用类构造器实例化。 <!-- 使用类构造器实 ... -
spring中的@Transaction配置详解
2011-12-13 20:13 7936spring中的@Transaction配 ... -
SSH整合包详解.Struts2.2.3+Spring3.1.0.M2+Hibernate3.6.6-学习笔记
2011-12-13 20:33 1733SSH整合包详解: a) commons commo ...
相关推荐
Spring的核心是IOC(Inversion of Control)容器,通过控制反转实现对象之间的解耦,使得代码更加灵活和可测试。 二、依赖注入(DI) 依赖注入是Spring的核心功能之一,它允许开发者在运行时动态地将依赖关系注入到...
控制反转(Inversion of Control,英文缩写为IoC),也叫依赖注入(Dependency Injection)。我个人认为控制反转的意思是依赖对象(控制权)发生转变,由最初的类本身来管理依赖对象转变为IoC框架来管理这些对象,使得...
在本篇 Spring 学习笔记中,我们将探讨 Spring 的入门、优点、组成以及重要的IOC理论。 1. **Spring 简介** Spring 是一个开源的、免费的 Java 框架,它的目标是减少企业级开发的复杂性。它集成了许多现有的技术,...
Spring框架是Java开发中广泛使用的轻量级框架,以其依赖注入(Dependency Injection,简称DI)和面向切面编程(Aspect-Oriented Programming,简称AOP)的核心特性,极大地简化了企业级应用的开发。以下是对Spring...
在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...
Spring框架是Java开发中广泛应用的一个轻量级框架,它的核心特性包括控制反转(IoC)和依赖注入(DI)。这两个概念是理解Spring的基础。 控制反转(IoC)是指应用程序的控制权不再由程序本身来掌控,而是交给了外部...
Spring框架是中国Java开发领域中最广泛使用的轻量级框架之一,以其IoC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)为核心特性,极大地简化了企业级应用的开发。这份学习...
Spring 的核心优势在于它的 IoC(Inversion of Control,控制反转)和 DI(Dependency Injection,依赖注入)特性。通过 IoC 容器,Spring 能够管理对象间的依赖关系,使得开发者不再需要在代码中手动创建和管理对象...
其核心特性是依赖注入(Dependency Injection,DI)和控制反转(Inversion of Control,IoC),这两者是理解 Spring 的基础。 **1. 依赖注入与控制反转** 依赖注入(DI)是 Spring 框架的核心,它允许组件在运行时...
Spring是企业级应用程序开发的重要框架,它通过控制反转(IoC)和面向切面编程(AOP)技术,显著降低了组件间的耦合度,提高了代码的可读性和可维护性。Spring框架的主要版本包括3.x、4.x和5.x,随着版本的升级,其...
【Spring 学习笔记】 Spring 是一个开源的 Java 应用框架,主要关注于应用程序的分层架构,提供控制反转(IOC)和面向切面编程(AOP)的支持。本笔记将详细介绍 Spring 的核心概念、配置和应用。 **面向接口(抽象...
Spring框架是Java应用开发中广泛使用的轻量级框架,它以IoC(Inversion of Control,控制反转)和DI(Dependency Injection,依赖注入)为核心,提供了丰富的功能,包括但不限于组件管理、AOP(Aspect Oriented ...
IOC(Inversion of Control,控制反转)和DI(Dependency Injection,依赖注入)是Spring的核心特性。IOC使得组件的创建和管理由Spring容器负责,而DI允许开发者在不修改代码的情况下改变组件之间的依赖关系。在...
Spring框架是Java开发中不可或缺的一部分,它以IoC(Inversion of Control,控制反转)和DI(Dependency Injection,依赖注入)为核心,简化了组件的管理并降低了代码间的耦合度。在Spring的学习过程中,理解其核心...
源代码 博文链接:https://danielkwo.iteye.com/blog/40945
Spring 5 框架学习笔记 本笔记主要介绍了 Spring 5 框架的相关知识点,包括 IoC 原理分析、基于 XML 的 IoC 实现、基于 XML 的 DI 使用、基于注解的 IoC 实现、Spring 纯注解实现方式、Spring 整合 Junit、Spring ...
**依赖注入** 是Spring.NET的核心特性之一,它反转了对象之间依赖关系的控制权。传统的编程中,对象通常会自行创建其依赖的对象实例,而在Spring.NET中,依赖关系由外部容器(IoC容器)负责管理和注入,使得对象只需...