`
icarusliu
  • 浏览: 235484 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring学习

阅读更多

1.关于依赖注入(控制反转):

在依赖注入的模式下,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者实例的工作通常由spring窗口完成,然后注入调用者,因此也称为依赖注入。

如:ApplicationContext.xml的部分内容:
<bean id="person" class="Person">    
<property name="name" value="wawa"/>
</bean>

在调用当中:

Person p = (Person)ctx.getBean("person");
p.info();

 

以三种情况来说明依赖注入:

原始的情况下,如果一个人要一把斧头,那么只能自己去生产。

工厂模式下,可以从工厂去买,而不用自己生产。

在依赖注入的模式下,都不用管斧头是从哪个工厂生产的,到需要的时候会由一个分配的直接分配给他。

所谓依赖注入,就是指程序运行过程当中,如果要调用另一个对象协助时,无须在代码当中创建被调用者,而是依赖于外部的注入。

 

2.两种注入方式:

1.设值注入

像上面的方式

2.构造注入

 

<bean id="chinese" class="lee....">   
     <constructor-arg ref="steelAxe"/>
</bean>
<bean id="steelAxe" class="..."/>

 

3.BeanWrapper接口:

  • 操作 bean

    BeanWrapper接口其实就是通过一个Java bean的对象来构造一个BeanWrapper对象,通过这个对象可以对Java Bean的对象中的属性进行设置,而不用去调用Java Bean对象的相关函数。其调用过程如下:

TestBean bean = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.setPropertyValue("name","liuqi");
//这里就相当于调用了bean.setName("liuqi");
  • 使用BeanWrapper的PropertyEditors包来转换属性:

        有时候为了使用方便,我们要以另外一种形式展现对象的属性。为达到这样的目的,我们可以编写自定义编辑器,使其继承java.beans.PropertyEditor类型,并将自定义编辑器注册到BeanWrapper上,通过注册,BeanWrapper将知道如何把属性转换成所需类型的信息。

public class Person {
    public void setBirthDay(Date d);
    public Date getBirthDay();
}
/** and some other method */
public void doIt() {
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    // CustomDateEditor located in    org.springframework.beans.propertyeditors
    // true indicated that null values are NOT allowed
    CustomDateEditor editor = new CustomDateEditor(df, false);
    Person p = new Person();
    BeanWrapper bw = new BeanWrapper(p);
    bw.registerCustomEditor(editor);
    // this will convert the String to the desired object type: java.util.Date!
    bw.setPropertyValue("birthDay", "22-12-1966");}

 

 4.BeanFactory

    a.使用:

 

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("hello.xml"));
TestBean bean= (TestBean)factory.getBean("testBean");

    b.

 

5.applicationContext

  A.它与BeanFactory不同的地方在于:

      a.附加了更多的功能

      b.在上下文启动之后载入所有的单实例Bean,而BeanFactory是在getBean调用的时候才载入实例。

  B. applicationContext的有一种实现:

      a.ClassPathXmlApplicationContext:从类路径的XML文件当中载入

      b.FileSystemXMLApplicationContext:从文件系统的XML文件当中载入

      c.XmlWebApplicationContext:从Web路径的XML文件当中载入

 

6.AOP

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www..."
...
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
 
  <bean id="testAspect" class="..."/>
  <aop:config>
    <aop:aspect ref="testAspect">
       <aop:pointcut id="test" expression="execution(* *.fun()) and target(bean)"/>
       <aop:before method="before" pointcut-ref="test" arg-names="bean"/>
       <aop:after-returning" .../>
    </aop:aspect>
</aop:config>
</beans>
 

 

 

附:spring教程:

1.spring framework :http://www.redsaga.com/spring_ref/2.0/html/

分享到:
评论

相关推荐

    spring 学习

    由于提供的文件内容中存在大量重复的网址信息,并没有实际的教学内容或者相关知识点,我将从标题“spring 学习”出发,结合描述“通过搭建基本的工程,从中学习spring的原理”来详细阐述Spring框架的相关知识点。...

    spring学习资料大全

    以下是对"Spring学习资料大全"的详细解析: 1. **Spring框架基础**: - **依赖注入(Dependency Injection,DI)**:Spring的核心特性之一,它允许开发者在运行时通过XML配置或注解方式来管理对象间的依赖关系,...

    spring学习.zip

    本资源集合围绕"spring学习.zip",提供了多本深入讲解Spring及其相关技术的电子书籍,旨在帮助读者深入理解和掌握Spring生态。 1. **《深入实践Spring Boot.陈韶健.pdf》**:这本书详细介绍了Spring Boot,它是...

    Spring学习笔记 自我总结

    spring学习笔记

    spring学习资料,精心总结,不可错过,速领!.zip

    这个"spring学习资料,精心总结,不可错过,速领!.zip"压缩包显然是为那些想要深入理解Spring框架的人准备的。以下是压缩包内可能包含的一些关键知识点,以及它们在实际开发中的应用和重要性: 1. **IoC...

    Spring学习资料文档合集

    Spring学习资料文档合集,包含 spring2.0-reference_RC2.1_zh_cn spring_reference_inchinese_m2 SpringGuide Spring基础教程 spring框架,技术详解及使用指导

    Spring学习笔记&源码

    本资料“Spring学习笔记&源码”是基于网易云课堂黑马程序员的Spring四天精通课程,旨在帮助学习者深入理解和实践Spring框架。 笔记部分可能会涵盖以下内容: 1. **Spring概述**:介绍Spring框架的历史、特点和主要...

    spring学习文档 适合新手

    spring学习文档 适合新手

    spring学习资料

    这个"spring学习资料"压缩包包含了多个文档,可以帮助我们深入理解并掌握Spring的核心概念和技术。 首先,"spring2.0-reference_final_zh_cn.chm"是Spring 2.0的中文参考手册,对于初学者来说非常宝贵。它详细介绍...

    Spring学习思维导图(仅供参考)

    Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图Spring学习思维导图...

    Spring学习笔记.xmind

    Spring学习笔记.xmind

    Spring学习手册

    【Spring学习手册】 Spring框架是Java开发中的一个核心组件,尤其在企业级应用开发中扮演着重要角色。它提供了一种全面的编程和配置模型,旨在简化开发过程并提高可测试性。本手册专为Spring的初学者设计,旨在帮助...

    Spring学习笔记+学习源码.zip

    这份"Spring学习笔记+学习源码.zip"资源包含了深入学习Spring及其相关技术的知识点,以及实践代码,对提升Spring技能将大有裨益。 首先,我们来详细讨论Spring框架的主要组件和功能: 1. **依赖注入(Dependency ...

Global site tag (gtag.js) - Google Analytics