Spring(1)初学

2022-09-19

什么是spring

  • Spring是一个开源的免费的框架(容器)

  • 支持事务的处理
  • 控制反转(IOC)面向切面编程(AOP)

1.1IOC理论

根据之前的学习,在我们的业务中,用户需求可能会影响我们原来的代码,我们需要根据用户的需求修改原代码!有时修改成本昂贵!

比如,UserDao可能会根据客户的不同需求有多个实现类,我们在service实现类中需要某个特定种类的userdao的实现类对象,这时我们可以利用set方法实现动态值的注入:

private UserDao userDao
    
public void setUserdao(UserDao userDao){
    this.userDao = userDao;
}
  • 之前,我们需要修改代码来让userservice接收我们想要它接收的dao对象,主动权在我们
  • 使用set后,程序不再具有主动性,而是客户需要什么就接收什么

控制反转了!

系统的耦合性降低,可以更加专注的在业务实现上,IOC原型

1.2 HelloSpring

先写一个普通的类:

public class Hello {
    private String str;
    @Override
    getter/setter
       toString}

在Spring创建对象

通过配置文件在Spring创建对象,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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用Spring创建对象,在Spring中称为bean-->
    <bean id="hello" class="com.bo.spring01.Hello">
        <property name="str" value="Spring"/>
    </bean>
</beans>

每一个bean就是一个对象。

实例化容器

写一个测试实例化容器:

    @Test
	public void myTest(){
        //参数是配置文件名,获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象现在都在Spring中管理了,要使用直接取就行
        Hello hello = (Hello) context.getBean("hello");
        System.out.println("Spring中的hello对象:"+hello.toString());
    }

Spring容器给对象的属性赋值的本质(IOC)是上面提到的set注入,也就是说hello类必须得有set方法

到了现在我们就不用再程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改(创建对象,注册bean),即IOC:对象由Spring创建,管理和装配

注册bean的时候,如果有要Set的属性,比如:

    <bean id="hello" class="com.bo.spring01.Hello">
        <property name="str" value="Spring"/>
    </bean>
<property name="str" value="Spring"/>

和:

<property name="str" ref="hello"/>

区别:

  • ref 引用spring容器中创建好的对象
  • value 是具体的值

用户要什么,我们直接从容器修改,再获取

1.3 IOC创建对象的方式

  • 使用无参构造创建对象(默认)
  • 假设要使用有参构造创建对象:

你有一个有参构造:

public class Hello {
    private String str;
        public Hello(String str) {
        this.str = str;
    }

1.下标赋值(从零开始)

<bean id="hello" class="com.bo.spring01.Hello">
	<constructor-arg index="0" value="Spring"/>
</bean>

2.直接通过参数名:

<bean id="hello" class="com.bo.spring01.Hello">
	<constructor-arg name="str" value="Spring"/>
</bean>

1.4Spring相关配置

刚刚已经讲到了bean和property,constructor-arg等相关配置,还有一些配置:

给注册的对象起一个别名:

<alias name="hello" alias="hello2"/>
             原名           别名

第二种别名:在配置bean的时候的name,可以取多个:

<bean id="hello" class="com.bo.spring01.Hello" name="h1,h2,h3">

import:

一般用于团队开发使用,可以将多个配置文件导入为一个。当有多个配置文件比如beans1.xml,beans2.xml,beans3.xml,现在你再写一个beans.xml:

<import resource = "beans1.xml"/>
<import resource = "beans2.xml"/>
<import resource = "beans3.xml"/>

使用的时候直接使用这个总的beans.xml配置文件就可以了

1.5依赖注入

1.5.1构造器注入

前面已经提到过了–<constructor-arg name= “ “ value=””

1.5.2 Set方式注入【重点】

依赖注入:Set注入!

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性由容器注入

示例:

public class Student {
    private String name;
    private Address address;
    private String books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    
    getter/setter方法
    
       @Override
       toString
}

各种注入方式:

<?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.xsd">

    <!--使用Spring创建对象,在Spring中称为bean-->
    <bean id="hello" class="com.bo.spring01.Hello">
        <property name="str" value="Spring"/>
    </bean>

    <bean id="address" class="com.bo.spring02.Address"/>

    <bean id="Student" class="com.bo.spring02.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="bo"/>

        <!--第二种:bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组注入,ref-->
        <property name="books">
            <array >
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>

        <!--list-->
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>写电影</value>
            </list>
        </property>

        <!--map-->
        <property name="card">
            <map>
                <entry key="身份证" value="123456"></entry>
            </map>
        </property>

        <!--Set-->
        <property name="games">
            <set>
                <value></value>
            </set>
        </property>

        <!--String注入-->
        <property name="wife">
            <null></null>
        </property>

    </bean>

1.5.3 c命名和p命名空间注入

示例,user类:

public class User {
    private String name;
    private int age;
    @
    getter and setter
}

新建一个userbeans.xml,要使用p标签,配置文件中要加上这一句:

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

然后配置bean时就可以使用p:

<!--p命名空间注入-->
<bean id="user" class="com.bo.spring02.User" p:name="bo" p:age="18"/>

可以直接注入属性的值。

    @Test
    public void myTest(){
        //参数是配置文件名
        ApplicationContext context = new ClassPathXmlApplicationContext("Userbeans.xml");
        //我们的对象现在都在Spring中管理了,要使用直接取就行
        User user = (User)context.getBean("user");
        System.out.println(user);
    }

调用一下,测试输出:

User{name='bo', age=18}

同样的,我们如果想用c命名空间注入,就在xml中加:

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

但是这个时候要注意:这个时候user类必须加无参和有参构造

<bean id="user" class="com.bo.spring02.User" c:age="18" p:name="bo"/>

c和p总结

可以理解为p就是property,是Set注入;c就是constructor,是构造器注入

注意点:c命名和p命名注入不能直接使用,需要导入xml约束!即:

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