【Spring框架】Spring IOC 回忆总结

来源:互联网 发布:苹果手机怎么信任软件 编辑:程序博客网 时间:2024/06/18 02:00

0.本文目录

  • 本文目录
  • 开篇明志
  • 配置文件头xmlns解读
  • Spinrg IOC 知识点
    • 1 注入类型
      • 11set方式
      • 12构造方法注入很少用
    • 2选择bean标识符 id OR name
    • 3 bean 的 scope属性
    • 4 autowire自动装配
    • 5 lazy-init
    • 6 Autowired注解

1.开篇明志

之前一直只是使用Spring框架做项目, 从没有写过笔记总结一下, 接下来将会花费一到两个篇幅来介绍一下Spring IOC 依赖注入 和 Spring AOP 动态代理。

本文我将回忆并总结一下 Spring IOC .

2.配置文件头xmlns解读

Spring4.2 的 applicationContext.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"     xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:task="http://www.springframework.org/schema/task"    xsi:schemaLocation="          http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd           http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.2.xsd          http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd        http://www.springframework.org/schema/mvc           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd           http://www.springframework.org/schema/task           http://www.springframework.org/schema/task/spring-task-4.2.xsd">
  • beans —— xml文件的根节点。
  • xml 的版本 version="1.0" , xml的编码方式 encoding="UTF-8"
  • xmlns ——是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于java中的package。

  • xmlns部分

xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //这个是每个配置文件必须的部分,也就是spring的根本。//声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。//声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了。
xmlns:aop="http://www.springframework.org/schema/aop"

这个就是spring配置文件里面需要使用到aop的标签,声明前缀为aop的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。然后其他比如context(针对组件标签)、MVC(针对mvc标签)、tx(针对事务标签)都一样的意思。

  • xsi:schemaLaction部分:
http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   ......

是为上面配置的命名空间指定xsd规范文件,这样你在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,比如说每个标签是怎么写的,都有些什么属性是都可以智能提示的,以防配置中出错而不太容易排查,在启动服务的时候也会根据xsd规范对配置进行校验。但是这里需要为你上面xmlns里面配置的mvc、aop、tx等都配置上xsd规范文件。

3.Spinrg IOC 知识点

3.1 注入类型

  • set注入(important)
  • 构造方法注入
  • 接口注入

3.1.1.set方式

下面简单介绍一下 常用的set注入方式:
HelloWorld.java

package com.springmvc.beans;public class HelloWorld {    public String name;    public void setName(String name) {        this.name = name;        System.out.println("Method setName was invocated.");    }    public String getName() {        System.out.println("Method getName was invocated.");        return name;    }    public String toString(){        return "this is my value : " + name;    }    public void hello(){        System.out.println("Invocation Method hello()");    }}

applicationContext.xml

<!-- id="helloworld" 用来标识这个对象的 --><!-- 全类名,Spring通过反射方式帮助我们创建一个对象 --><bean id="helloworld" class="com.springmvc.beans.HelloWorld">        <property name="name" value="Spring"></property>  </bean><!-- property 属性: name="name" value="Spring" 对应着类属性为name, 通过对应的set方法, 比如setName 来注入 value -->

ClassPathXmlApplicationContext加载beans.xml

public static void testSetIOC() {        //创建Spring 的 IOC 容器对象        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");        HelloWorld helloworld = (HelloWorld)ctx.getBean("helloworld");        System.out.println("get value: " + helloworld.getName());        helloworld.hello();        //调用toString()方法        System.out.println(helloworld);}//控制台输出:Method setName was invocated.Method getName was invocated.get value: SpringInvocation Method hello()this is my value : Spring

可以看到Spring会调用bean的setName方法来完成自动注入的, 获得对象的值是通过调用getName方法获取的。

3.1.2.构造方法注入(很少用)

可以使用

如果多个参数,可以使用index或者type:
index:index = “0”,然后指定参数位置为0的值;index = “1”,然后指定参数位置为1的值。func(int a, int b),那么a就是index0,b就是index1
type:type=”int”, type=”java.lang.String”。和上面类似


3.2选择bean标识符 id OR name

<?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-4.2.xsd">    <!--使用id定义bean1  实现类为 `com.springmvc.beans.bean1`-->    <bean id="bean1" class="com.springmvc.beans.bean1">      </bean>    <!--使用name定义bean2 实现类为 `com.springmvc.beans.bean2`-->    <bean name="bean2" class="com.springmvc.beans.bean2">     </bean></beans>

使用class指定具体实现类。
bean的id或者name都可以,但是name可以有特殊字符。
建议使用id

注意: 如果在Bean中未指定id 和 name , 则Spring会将class值当做id使用。


3.3 bean 的 scope属性

可以在bean中指定scope(范围):

  • singleton: 单例模式,scope为singleton(单例模式),就是这个属性无论被拿出来几次,都是同一个对象

  • prototype: 原型模式,每次拿spring会根据这个原型创建一个新的对象,所以拿出来的每个都是全新的、不同的对象

3.4 autowire自动装配

对于自动装配来说,前2种是经常使用的:

  • byName是在bean中找到和属性名称完全相同的bean进行注入
  • byType是找到和属性类型完全一致的bean进行注入(如果存在多个可能要报错的)
  • default的使用中定义的装配方式default-autowire="byName"

3.5 lazy-init

lazy-init是在bean被get的时候才会初始化

3.6 @Autowired注解

@Autowired默认是byType的,如果有多个相同name的bean,就需要使用@Qualifier(“name1”)来指定使用name1的bean

未完待续。。。

0 0
原创粉丝点击