码迷,mamicode.com
首页 > 编程语言 > 详细

Spring学习记录-IOC

时间:2015-08-03 21:06:32      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:spring

思想仍然是 将代码转为配置,类的管理交给Spring容器来做。

IOC:控制反转,控制权的转移,即应用程序本身不负责依赖对象的创建和维护,而是由 外部容器负责创建和维护。获得依赖的过程被反转,由自身管理变成IOC注入
实现方式DI:依赖注入,创建对象并组装对象之前的关系。

Spring允许通过如下几个元素为Bean实例的属性指定值:
value、ref、bean、list、set、map、props

spring注入也可以给static变量赋值,不一定非要生成对象

spring——Spring注入方式

两种方法都写上setter方法,避免麻烦
Spring是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
设值注入:配置properties属性
构造注入:使用constructor-arg
根据ref,将ref所指对象的实例赋值给name所指成员变量
注解注入:…

spring——bean的配置及作用域

  1. bean的常用配置项:(理论上只有class是必须的)
    id:唯一标识
    class:具体是哪一个类
    scope:范围
    constructor arguments:构造器的参数
    properties:属性
    Autowiring mode:自动装配模式
    lazy-initialization mode:懒加载模式
    initialization/destruction method:初始化/销毁的方法
  2. bean的作用域
    singletion 单例 bean容器只有唯一的对象(默认模式)
    prototype 每次请求会创建新的实例,destory方式不生效
    request 对于request创建新的实例,只在当前request内有效
    session 对于session创建新的实例,只在当前session内有效
    global session 基于portlet(例如单点登录的范围)的web中有效,如果在web中同session
  3. bean的生命周期
    定义、初始化、使用、销毁
    -#- spring——bean的生命周期-#
    生命周期:定义,初始化,使用,销毁
    默认写法:beans 最后面写default-init-method=”xxx”

Bean的自动装配

一种是byName,另一种是byType
作用:在类初始化时,不用显式的指出如何装配(属性赋值),default-autowire=”byName”
byName时bean必须配置id,且id和参数名必须一致。
byType和constructor无需配置对应的bean无需配置id。
byType对应< bean class=”“/>,注:type要唯一;

类的自动检测及扫描

< context:component-scan base-package=”org.example”/>
base-package指定包下的所有类
可以扫描基于类的注解
上面那句包含< context:annotation-config>
这句只能在完成bean的注册后扫描成员的注解

注解的解析是由ApplicationContext中的
CommonAnnotationBeanPostProcessor发现并处理的

Autowired

autoWired就是”注入”,帮我们自动”装配”(“注入”)那些类里面的属性(就相当于在xml文件中定义的property/constructor),同时有了autoWird修饰的属性就等于具有了setter方法。

<bean id="injectionService" lass="com.imooc.ioc.injection.service.InjectionServiceImpl">
<constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg>
</bean>
<bean id="injectionDAO" class="com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>

还记得property吗?它帮我们定义了一个类中的属性,同时如果有同名的bean就自动注入到该属性中,那么autoWired就是同样的道理,自动的帮我们将autoWired注释的属性注入,他注入的原则也是根据bean id。“同时有了autoWird修饰的属性就等于具有了setter方法”这句话应该这样理解,因为通过set方法我们才能进行填充,因此setter方法才是我们注入的关键.

使用IOC
1. spring.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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <bean id = "visitor" class = "com.alibaba.webx.tutorial1.app1.Visitor">
    <property name="name" value="bingone"></property>
    <property name="name2" value="bingone.xsy"></property>
    </bean>
    <bean id = "aaa" class = "com.alibaba.webx.tutorial1.app1.AAA"/>
</beans>

  1. 新建需要映射的类,其中spring.xml的加载通过Resource
package com.alibaba.webx.tutorial1.app1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AAA {
    @Autowired
    Visitor visitor;
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cls = new ClassPathXmlApplicationContext("spring.xml");

        System.out.println(Visitor.name2);
        AAA a = (AAA) cls.getBean("aaa");
        System.out.println(a.visitor.getName());
    }
}
=======================================================
/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.webx.tutorial1.app1;

public class Visitor {
    private String name;
    public static String name2;

    public String getName() {
        return name;
    }

    public static String getName2() {
        return name2;
    }

    public static void setName2(String name2) {
        Visitor.name2 = name2;
    }

    public void setName(String name) {
        this.name = name;
    }
}

PS:

在filter里注入被注解的bean?
问题已经解决了,这其中涉及到web启动的原理。 众所周知,web应用启动的顺序是:listener->filter->servlet,而因为项目应用了spring mvc,所以我们会有两个配置文件(applixationContext.xml和springMVC-sevlet.xml),我们在配置spring时会用到spring的listener,它会读取application.xml里的配置对spring context进行初始化;而springMVC-servlet.xml则是在spring mvc的dispathServlet启动的时候读取进行配置。而如果项目里用到了注解,则需要在springMVC-servlet.xml中加上“”。 ok,经过上面的铺垫后,进入重点。 问题就是项目启动时,先初始化listener,因此配置在applicationContext.xml里的bean会被初始化和注入;然后再来就filter的初始化,再接着才到我们的dispathServlet的初始化,因此,当我们需要在filter里注入一个注解的bean时,就会注入失败,因为filter初始化时,注解的bean还没初始化,没法注入。所以,如果想要在filter里注入注解bean的话,就要在applicationContext.xml里配置context,也就是上面说的那句配置。在这里配置时需要注意的是,需要把tx和aop的配置放在最下面,否则也会导致spring的context初始化失败。 把配置弄好之后,我们就在filter的init方法里,通过获取webApplicationContext的getBean方法对需要的bean进行注入。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Spring学习记录-IOC

标签:spring

原文地址:http://blog.csdn.net/gg_gogoing/article/details/47261121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!