- 浏览: 414771 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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:
[flash=200,200][url][url][img][ ...
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 10029事务是逻辑处理原子 ... -
spring声明式事务策略 aop拦截-学习笔记
2011-12-16 17:42 2154声明式事务管理: S ... -
spring中的@Transaction配置详解-学习笔记
2011-12-16 17:42 21320spring中的@Transaction配置详解 1、 ... -
spring AspectJ的Execution表达式-备忘笔记
2011-12-16 23:57 14002Aspectj切入点语法定义 在使用spring框 ... -
spring 配置文件实现AOP-学习笔记
2011-12-15 16:15 1542Spring 对AOP的支持I: Aspect默认情况 ... -
spring JDK的Proxy技术实现AOP功能和CGBLB-学习笔记
2011-12-15 16:05 1817动态代理类(以下简称为代理类)是一个实现在创建类时在运行时指定 ... -
spring容器自动扫面管理bean-学习笔记
2011-12-15 16:42 1509通过在classpath自动扫描方式把组件纳入sp ... -
spring自动装配注解模式-学习笔记
2011-12-15 16:43 3388spring 自动装配注解模式 1、什么是自动装配 ... -
spring的注入依赖之构造器注入- 学习笔记
2011-12-15 16:44 2125Spring的注入依赖(DI)主要有三种注入方式, ... -
spring Bean的作用域scope-学习笔记
2011-12-14 22:09 1790Spring容器最初提供了 ... -
添加xml文件自动提示方法,以spring配置文件为例-备忘录
2011-12-14 22:01 1012用eclipse,在导入相应的包后(有人说不导包也有提示,因为 ... -
基于Struts、Hibernate、Spring 的学习框架-笔记备忘
2011-12-13 20:24 1092基于Struts、Hibernate、Spring 的学 ... -
spring声明式事务策略 aop拦截-学习笔记
2011-12-13 20:20 1485声明式事务管理: Spring提供了声明式事务管理 ... -
三种实例化Spring中Bean对象的方式
2011-12-13 20:17 16111、使用类构造器实例化。 <!-- 使用类构造器实 ... -
spring中的@Transaction配置详解
2011-12-13 20:13 7917spring中的@Transaction配 ... -
SSH整合包详解.Struts2.2.3+Spring3.1.0.M2+Hibernate3.6.6-学习笔记
2011-12-13 20:33 1710SSH整合包详解: a) commons commo ...
相关推荐
控制反转(Inversion of Control,英文缩写为IoC),也叫依赖注入(Dependency Injection)。我个人认为控制反转的意思是依赖对象(控制权)发生转变,由最初的类本身来管理依赖对象转变为IoC框架来管理这些对象,使得...
在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...
源代码 博文链接:https://danielkwo.iteye.com/blog/40945
核心概念:深入探讨了 Spring 的核心概念,如控制反转(IoC)和面向切面编程(AOP),以及它们如何改善应用程序的设计和结构。 Spring 容器:介绍了 Spring 容器的不同类型,包括 BeanFactory 和 ...
基础篇包括:控制反转,环境搭建,实现简易IOC框架,对象的创建,容器中对象作用域,依赖注入,依赖对象的注入,集合类型的注入等 所有代码都是基于.net平台的C#语言实现的,资源整理自互联网,欢迎爱好学习研究框架...
依赖注入DI/IoC控制反转:提供松耦合的一种实现技术 AOP面向切面编程:(可以在不修改源代码的前提下,增强权限,日志,事务功能)切面编程将业务逻辑从应用服务中分离 容器(spring):管理dao,service,action,...包含并...
web开发技术spring框架控制反转思想IOC学习笔记,并对java的静态代理和动态代理进行了相关叙述
体验.net2.0的优雅(四):Provider、策略、控制反转和依赖注入 泛型最佳实践 asp.net 2.0下嵌套masterpage页的可视化编辑 C# 2.0与泛型 动态调用对象的属性和方法——性能和灵活性兼备的方法 泛型技巧系列:用泛型...