spring IOC(DI)

本文深入介绍了Spring框架的核心概念,包括其轻量级特性、IOC(控制反转)、AOP(面向切面编程)以及事务支持。通过实例展示了如何通过XML配置实现对象创建和属性注入,探讨了不同方式的IOC容器创建对象,并详细讲解了依赖注入的各种形式,如构造器注入和setter注入等。此外,还提到了bean的作用域和自动装配机制,为读者提供了全面理解Spring框架的指导。
摘要由CSDN通过智能技术生成

Spring简介

spring作者:Rod Johnson,重要在思想上,理念:使现有技术更加实用,不去创建新技术,整合已有的框架技术。Spring本身是一个大杂烩
无论是什么框架,只是提供了一种规则给你。知识点如何运用到项目中,这才是精髓。
1、轻量级框架(轻量级,就是无侵入性,替换该框架时原有代码不需要改动)
2、IOC容器(控制反转)
3、AOP(面向切面编程)
4、对事务的支持
5、对框架的支持

IOC控制反转(inversion of control)

优点:
1、对象由原来程序本身创建,变为程序接对象
2、程序员主要精力集中于业务实现
3、实现service和dao解耦,service层和dao层实现了分离,没有直接依赖关系
4、如果dao的实现发生变化,service层不要改动
步骤:
1、导入相关spring jar包
2、编写spring配置文件
3、
Hello.java

public class Hello {
    private String name;
    public String getName() {
        return name;
    }

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

    public void show(){
        System.out.println("print,"+name);
    }
}

beans.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--bean就是java对象,由spring来创建和管理-->
    <bean name="hello" class="com.sbmybatis.user.Hello">
        <property name="name" value="张三"/>
    </bean>
</beans>

Test.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        //解析beans.xml文件,生成管理相应的bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = (Hello)context.getBean("hello");
        hello.show();
    }
}

思考:Hello对象是谁创建的?
是由spring容器创建的,在beans.xml配置即可
Hello对象属性是怎么设置的?
对象属性是由spring容器来设置的,在beans.xml配置即可
这个过程就称之为控制反转(控制指的是谁来控制对象的创建,传统应用程序对象的创建是通过new java代码创建的:
Hello hello1 = new Hello();
hello1.setName(“ddd”);
hello1.show();
使用spring后是由spring来创建对象的;以前是主动创建对象,现在是被动接收由容器创建的对象,beans.xml,此处指的是反转,创建对象权限的转移就是反转)控制反转也叫依赖注入(Dependency inject),通过什么注入的?通过set方法注入的,所以必须要有set方法

IOC容器创建对象的方式

IOC是一种编程思想,由主动编程编为被动接收。IOC的实现是通过IOC容器来实现的。IOC容器其实就是一个Bean工厂,BeanFactory,IOC容器就是帮我们创建对象、装配对象、管理对象。
使用IOC来创建对象有3种方式:
1、通过无参构造方法来创建对象
User.java

public class User {
    private String name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public User(){
        System.out.println("User的无参构造方法");

    }
    public void show(){
        System.out.println("print,"+name);
    }
}

beans.xml

    <bean id="hello" class="com.sbmybatis.user.User" >
    <property name="name" value="张三"/>
    </bean>

2、通过有参构造方法来创建对象
User.java

public class User {
    private String name;
    public User(String name){
        super();
        this.name=name;
    }

    public void show(){
        System.out.println("print,"+name);
    }
}

beans.xml配置
第一种 根据参数的下标来设置

 <!--index指构造方法 参数下标从0开始-->
    <bean id="hello" class="com.sbmybatis.user.User" >
        <constructor-arg index="0" value="李四"/>
    </bean>

第二种 根据参数名称来设置

        <!--name指参数名-->
    <bean id="hello" class="com.sbmybatis.user.User" >
        <constructor-arg name="name" value="李四"/>
    </bean>

第三种 根据参数类型来设置

        <!--type指参数类型-->
    <bean id="hello" class="com.sbmybatis.user.User" >
        <constructor-arg type="java.lang.String" value="李四"/>
    </bean>

3、通过工厂方法来创建对象
第一种 静态工厂
UserFactory.java

public class UserFactory {
    public static User newInstance(String name){
        return new User(name);
    }
}

beans.xml

    <bean id="hello" class="com.sbmybatis.user.UserFactory" factory-method="newInstance">
        <constructor-arg index="0" value="王五"/>
    </bean>

第二种 动态工厂
UserDynamicFactory.java

public class UserDynamicFactory {
    public  User newInstance(String name){
        return new User(name);
    }
}

beans.xml

    <bean id="userFactory" class="com.sbmybatis.user.UserDynamicFactory"/>
    <bean id="hello" factory-bean="userFactory" class="com.sbmybatis.user.UserFactory" factory-method="newInstance">
        <constructor-arg index="0" value="王五"/>
    </bean>

spring配置文件

alias:为bean设置别名;可以设置多个别名
    <!--设置别名-->
    <alias name="hello" alias="hello1 u2,u3"/>

id是bean的标识符 要唯一,如果没有配置id,name默认为标识符;如果配置了id,又配置了name,那么name是别名,
name可以设置多个别名,分隔符可以是空格 逗号 分号,class是bean的包名+类名;如果不配置id和name,那么可以根据
applicationContext.getBean(Class)获取对象

引入其他.xml文件
    <import resource="config/spring/entity.xml"/>

spring依赖注入(DI)

依赖:指bean对象创建依赖spring容器,bean对象的依赖资源(文件、类)
注入:指bean对象依赖的资源由容器来设置和装配
spring注入分为两类:
1、构造器注入,见IOC创建对象
2、setter注入
要求被注入的属性必须有set方法。set方法的方法名由set+属性首字母大写。如果属性是boolean类型,返回方法不是get,是is

 private boolean sex;

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public boolean isSex() {
        return sex;
    }

(1)常量注入

    <bean id="student" class="com.sbmybatis.user.Student" >
        <property name="name" value="学生"></property>
    </bean>

(2)bean注入

    <bean id="address" class="com.sbmybatis.user.Address">
        <property name="address" value="浙江杭州"></property>
    </bean>
    <bean id="student" class="com.sbmybatis.user.Student" >
        <property name="name" value="学生"></property>
        <property name="addr" ref="address"></property>
    </bean>

(3)数组注入

    <bean id="address" class="com.sbmybatis.user.Address">
        <property name="address" value="浙江杭州"></property>
    </bean>
    <bean id="student" class="com.sbmybatis.user.Student" >
        <property name="name" value="学生"></property>
        <property name="addr" ref="address"></property>
        <property name="books">
            <array>
                <value>傲慢与偏见</value>
                <value>仲夏夜之梦</value>
                <value>雾都孤儿</value>
            </array>
        </property>
    </bean>

(4)List注入

 <property name="hobbies">
            <list>
                <value>羽毛球</value>
                <value>乒乓球</value>
                <value>台球</value>
            </list>
        </property>

(5)Map注入

两种key value写法都行
<property name="cards">
            <map>
             <entry key="中国银行" value="111333311"></entry>
                <entry>
                    <key><value>建设银行</value></key>
                    <value>20400101022</value>
                </entry>
            </map>
        </property>

(6)Set注入

 <property name="games">
            <set>
                <value>捉猫猫游戏</value>
                <value>大话西游</value>
                <value>水果游戏</value>
            </set>
        </property>

(7)Null注入

<property name="wife"><null/></property>

(8)properties注入

<property name="info">
            <props>
                <prop key="学号">201201</prop>
                <prop key="sex">男</prop>
                <prop key="name">小强</prop>
            </props>
        </property>

(9)p命名空间注入
xml文件头需要添加:

xmlns:p="http://www.springframework.org/schema/p"

p就是属性property的意思,属性依然要设置setter方法

    <bean id="useru" class="com.sbmybatis.user.Useru" p:name="风青杨" p:age="20"/>

(10)c命名空间注入
xml文件头需要添加:

xmlns:c="http://www.springframework.org/schema/c"

c就是构造方法的意思,要求有对应类有构造方法

 <bean id="u1" class="com.sbmybatis.user.Useru"  c:name="郭靖" c:age="16"/>

Beand的作用域

scope指bean的作用域,在配置bean时,有scope属性来配置bean的作用域

   <bean id="address" class="com.sbmybatis.user.Address" scope="singleton">
        <property name="address" value="浙江杭州"></property>
    </bean>

默认是单例的
singleton单例:整个容器中只有一个对象实例
prototype原型:每次获取bean都产生一个新的对象
request:每次请求时创建一个新的对象
session:在会话的范围是一个对象,这个指的是http的session
global session:只在portlet下有用,表示是application
application:在应用范围只有一个对象

Bean的自动装配

自动装配是为了简化spring配置文件
在配置bean时,可以配置bean的autowire属性,用于指定装配类型,可以配置全局的自动装配类型(在头部配置default-autowire)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"
default-autowire="byName">

byName,根据名称(set方法名)去查找相应的bean,如有则装配上

    <bean id="student" class="com.sbmybatis.user.Student" autowire="byName" >

byType,根据类型进行自动装配,不用管bean的id,但是同一种类型的bean只能有一个,建议慎用byType

    <bean id="student" class="com.sbmybatis.user.Student" autowire="byType" >

constructor当通过构造器实例化bean时,使用byType的方式装配构造方法

    <bean id="student" class="com.sbmybatis.user.Student" autowire="constructor" >

不推荐使用自动装配,而使用注解annotation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值