【Java学习记录】spring框架(IOC部分)

目录

1、spring框架概述

2、IOC

(1)IOC的底层原理

(2)IOC接口

(3)IOC操作Bean管理(xml)

(3)IOC操作Bean管理(基于注解)


1、spring框架概述

(1)spring是轻量级的开源的JavaEE框架,可以解决企业应用开发的复杂性。

(2)spring有两个核心部分:IOC和AOP

        IOC:控制反转,把创建对象的过程交给spring进行管理。

        AOP:面向切面,不修改源代码进行功能增强。

(3)spring特点:

        1) 方便解耦,简化开发。

        2)AOP编程支持

        3)方便程序测试

        4)方便和其他程序进行整合

        5)方便进行事务操作

        6)降低API开发难度

2、IOC

什么是IOC?

IOC即控制反转,可以把对象的创建和对象的调用过程交给spring来管理,目的是降低程序的耦合度。

(1)IOC的底层原理

        xml解析、工厂模式、反射机制

        第一步:xml配置文件,配置需要创建的对象,比如:

<bean id="user" class="com.bjgy.spring5.User"></bean>

        第二步:我们有一个A类和B类,现在想要A类去调用B类的方法,一般的方法是在A类中new B(),再去调方法,但是这种方式程序耦合度太高,一旦B类有改动,则必须改动A类的程序,这样太繁琐。考虑到这层原因,于是我们创建一个工厂类。并且在工厂类中使用反射机制。A类可以通过调用这个工厂类来创建对象。这样的话,一旦B类有改动,只需要去改动xml配置文件,而不需要去改动程序。

class BFactory{
    String classValue = xml文件中class属性值,由解析xml文件获取;
    //反射机制
    Class BClass = Class.forName(classValue);
    return (B)BClass.newInstance();
}

(2)IOC接口

        IOC思想基于IOC容器完成,IOC容器底层就是对象工厂。

        spring为IOC容器的实现提供了两个接口:

        BeanFactory:IOC容器的基本实现,是spring内部的使用接口,一般不提供给开发人员使用。在加载配置文件时不创建对象,在获取对象时才会创建对象。

        ApplicationContextBeanFactory接口的子接口,提供更多更强大的功能,一般提供给开发人员使用。在加载配置文件时创建对象

(3)IOC操作Bean管理(xml)

        什么是Bean管理?

        spring创建对象与spring注入属性

        1、基于xml创建对象

        在xml文件中,使用bean标签,在标签中添加对应属性可以创建对象,例如:

<bean id="user" class="com.bjgy.spring5.User"></bean>

        其中id是自定义的名称,是唯一标识,class是带包类名

        2、基于xml注入属性

        DI:依赖注入,即注入属性

        一般给对象的属性赋值有两种方式:一种是调用有参数构造方法,一种是调用set方法

        用xml注入属性也是如此!

        使用set方法注入属性:其中name是属性名,value是需要赋的值

<!--配置User对象创建-->
    <bean id="user" class="com.bjgy.spring5.User">
        <!--set方法注入属性-->
        <property name="userName" value="zhangsan"></property>
        <property name="email" value="5465@163.com"></property>
        <property name="id" value="111"></property>
    </bean>
</beans>

        使用有参构造注入属性

<bean id="user" class="com.bjgy.spring5.User">
        <!--有参构造注入属性-->
        <constructor-arg name="email" value="456@189.com"></constructor-arg>
        <constructor-arg name="id" value="222"></constructor-arg>
        <constructor-arg name="userName" value="lisi"></constructor-arg>
    </bean>

        xml注入其他类型属性

        1、字面量

        (1)null值   

 <constructor-arg name="email"> <null/> </constructor-arg>

        (2)属性值包含特殊符号

                比如想要实现把email=<<123@163.com>>

                1)可以用转义字符   &lt;代表<     &gt;代表>   

<constructor-arg name="email" value="&lt;&lt;123@163.com&gt;&gt;"></constructor-arg>

               2)把带特殊符号的内容写到CDATA                  

<constructor-arg name="email">
            <value><![CDATA[<<123@163.com>>]]></value>
            </constructor-arg>

       2、注入属性--外部bean

        在service层调用dao层的类,在配置文件中需要用ref

        3、注入属性--内部bean方式

        其实就是在bean标签内部嵌套一个bean标签,也就是生成一个对象,相对于外部bean而言,这个在内部生成的对象,在其他bean标签内不能使用

<!--注入属性,内部bean的方式-->
    <bean id="emp" class="com.bjgy.spring5.bean.Emp">
        <property name="empno" value="111"></property>
        <property name="ename" value="jack"></property>
        <property name="dept">
            <bean id="Dept" class="com.bjgy.spring5.bean.Dept">
                <property name="deptno" value="10"></property>
                <property name="dname" value="财务部"></property>
            </bean>
        </property>
    </bean>

        4、注入属性--级联赋值

        其实就是用ref调用外部bean生成的对象,然后在自己的bean标签内给对象的属性赋值,但是要注意emp类中一定要有dept属性的get方法

<!--级联赋值-->
    <bean id="emp" class="com.bjgy.spring5.bean.Emp">
        <property name="empno" value="123"></property>
        <property name="ename" value="luck"></property>
        <property name="dept" ref="Dept"></property>
        <property name="dept.dname" value="财务部"></property>
        <property name="dept.deptno" value="10"></property>
    </bean>
    <bean id="Dept" class="com.bjgy.spring5.bean.Dept"></bean>

        5、注入集合类型的属性

        注入数组、list、map、set。集合中存放的均为String类型

<!--集合类型的属性注入-->
    <bean id="student" class="com.bjgy.spring5.bean.Student">
        <property name="courses">
            <array>
                <value>数学</value>
                <value>语文</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>zhangsan</value>
                <value>lisi</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="java" value="JAVA"></entry>
                <entry key="c++" value="C++"></entry>
            </map>
        </property>
        <property name="set">
            <set>
                <value>mysql</value>
                <value>redis</value>
            </set>
        </property>
    </bean>

        如果想在集合中放对象怎么办呢?

<!--list集合,值是对象-->
        <property name="bookList">
            <list>
                <ref bean="book1"></ref>
                <ref bean="book2"></ref>
            </list>
        </property>
    </bean>
    <bean id="book1" class="com.bjgy.spring5.bean.Book">
        <property name="bname" value="数学书"></property>
    </bean>
    <bean id="book2" class="com.bjgy.spring5.bean.Book">
        <property name="bname" value="语文书"></property>
    </bean>

        把集合的赋值部分放在外面,让所有的bean标签都可以用

        这就需要引入名称空间util

<?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 http://www.springframework.org/schema/util/spring-util.xsd">
    <util:list id="list">
        <value>数学</value>
        <value>语文</value>
        <value>英语</value>
    </util:list>
    <bean id="student" class="com.bjgy.spring5.bean.Student">
        <property name="list" ref="list"></property>
    </bean>
</beans>

        6、FactoryBean

        spring有两种bean,一种是普通bean,另一种是工厂bean

        工厂bean与普通bean的区别是:它在配置文件中定义的类型class,可以与返回值类型不一样

        需要将对应的类,实现FactoryBean接口,在实现方法中定义返回值的类型,,比如:在MyBean类中,定义返回book对象

public class MyBean implements FactoryBean {

    @Override
    public Book getObject() throws Exception {
        Book book = new Book();
        book.setBname("asd");
        return book;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

而在配置文件中,仍然bean类型使用的是com.bjgy.spring5.factoryBean.MyBean

<bean id="mybean" class="com.bjgy.spring5.factoryBean.MyBean"></bean>

这样调用getBean方法,返回的是Book类型

    @Test
    public void testMyBean(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean6.xml");
        Book book = context.getBean("mybean", Book.class);
        System.out.println(book);
    }

        7、如何设置单实例还是多实例?

        配置文件bean标签中有scope属性,其默认值为singleton,表示单实例,意思就是如果调用多次getBean,得到的对象的内存地址都是一样的。

还有一个值为prototype,表示多实例,意思是每次调用getBean方法,会得到不同地址的对象。

另外,在scope=“singleton” 时,加载配置文件时就会创建单实例对象

在scope=“prototype” 时,加载配置文件时不会创建对象,而是在调用getBean方法时才会创建多实例对象。

        8、bean生命周期

        (1)通过无参构造方法创建bean实例对象

        (2)为bean的属性设置值和对其他bean引用(调用set方法)

        (3)调用bean的初始化方法(需要配置)

        (4)获取对象,并且可以使用

        (5)关闭容器,并且调用bean的销毁方法(需要配置)

配置文件代码:

<?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">
    <bean id="order" class="com.bjgy.spring5.bean.Order" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="订单"></property>
    </bean>
</beans>

类代码:

public class Order {
    private String oname;

    public Order() {
        System.out.println("第一步执行无参构造方法");
    }

    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步执行set方法设置属性值");
    }
    public void initMethod(){
        System.out.println("第三步执行初始化方法");
    }
    public void destroyMethod(){
        System.out.println("第五步销毁方法执行了");
    }
}

测试代码:

@Test
    public void testOrder(){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean7.xml");
        Order order = context.getBean("order", Order.class);
        System.out.println("第四步获取对象");
        System.out.println(order);
        //手动销毁bean实例
        context.close();

        9、bean的后置处理器

        通过使另一个类实现BeanPostProcessor接口,把bean实例对象传递给两个实现方法:

postProcessBeforeInitialization方法,它会在第三步在初始化之前执行
postProcessAfterInitialization方法,它会在第三步在初始化之后执行

加一个后置处理器类:

public class BeanPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(bean+"。。在初始化之前执行的方法");
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(bean+"。。在初始化之后执行的方法");
        return bean;
    }
}

在配置文件中:

<?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">
    <bean id="order" class="com.bjgy.spring5.bean.Order" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="订单"></property>
    </bean>
    <!--后置处理器-->
    <bean id="beanpost" class="com.bjgy.spring5.bean.BeanPost"></bean>
</beans>

运行测试方法得到:

        10、xml自动装配

        根据指定装配规则(属性名称或属性类型),spring自动将匹配的属性值自动注入。而不需要ref

在bean标签中,使用autowire属性,常用两个值

byName:根据属性名称注入,此时注入bean的id值必须和类属性名称一样

byType:根据属性类型注入,此时注入bean属性类型必须唯一

 <!--自动装配,emp的属性有ename,empno,dept,对dept自动注入-->
    <bean id="emp" class="com.bjgy.spring5.bean.Emp" autowire="byName">
        <property name="ename" value="zhangsan"></property>
        <property name="empno" value="111"></property>
    </bean>
    <bean id="dept" class="com.bjgy.spring5.bean.Dept">
        <property name="deptno" value="10"></property>
        <property name="dname" value="人事部"></property>
    </bean>

        11、引入外部属性文件

        创建属性配置文件jdbc.prperties

         在xml文件中,引入context名称空间

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--引入外部属性文件-->
    <context:property-placeholder location="jdbc.properties"/>
    <!--配置德鲁伊连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.userName}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>
</beans>

(4)IOC操作Bean管理(基于注解)

        1、spring针对bean管理中创建对象提供4个注解,这四个注解功能一样

        @Component      @Service     @Controller    @Repository

        2、使用注解需要添加的依赖:

        (1)在xml文件中使用context名称空间,开启组件扫描,目的是扫描指定包下的带注解的类

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--使用context名称空间 开启组件扫描,
    1、如果扫描多个包,可以把每个包名用逗号隔开
    2、也可以直接写包的上层目录
    -->
    <context:component-scan base-package="com.bjgy.spring5"></context:component-scan>

    <!--use-default-filters="false" 表示现在不使用默认过滤器,自定义
        context:include-filter,设置扫描那些内容,比如只扫描带有Component注解的类  -->
    <context:component-scan base-package="com.bjgy.spring5" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--context:exclude-filter,设置那些内容不扫描,比如不扫描带有Component注解的类-->
    <context:component-scan base-package="com.bjgy.spring5">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Component"/>
    </context:component-scan>
</beans>

        (2)在对应的类上使用注解,四个注解都有相同的作用

package com.bjgy.spring5.service;

import org.springframework.stereotype.Component;

/*注解里面的value属性值可以不写,
默认值是类名的首字母小写,即UserService-->userService
加这一个注解的作用相当于xml文件的
<bean id="userService" class="com.bjgy.spring5.service.Uservice"></bean>*/
@Component(value = "userService")
public class UserService {
    public void add(){
        System.out.println("测试方法add。。。");
    }
}

        (3)测试类与之前相同

import com.bjgy.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {
    @Test
    public void testUserService(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }
}

        3、注解实现属性注入

        (1) @Autowired:根据属性类型自动注入

@Service(value = "userService")
public class UserService {
    //注入属性注解,根据属性类型注入,不需要set方法
    @Autowired
    private UserDao userDao;
    public void add(){
        userDao.add();
        System.out.println("userService.add()执行。。。");
    }
}

        (2) @Qualifier:根据属性名称进行注入,需要和@Autowired一起使用

@Service(value = "userService")
public class UserService {
    //注入属性注解,根据属性类型注入,不需要set方法
    @Autowired
    @Qualifier(value = "userDaoImpl")//根据属性名注入,需要配合@Autowired使用
    private UserDao userDao;
    public void add(){
        userDao.add();
        System.out.println("userService.add()执行。。。");
    }
}

        (3) @Resource:根据类型注入,也可以根据名称注入

@Service(value = "userService")
public class UserService {
//    @Resource
    @Resource(name = "userDaoImpl")
    private UserDao userDao;
    public void add(){
        userDao.add();
        System.out.println("userService.add()执行。。。");
    }
}

        (4) @Value:以上三个只能注入对象属性,这一个注入普通类型

@Service(value = "userService")
public class UserService {
    @Resource(name = "userDaoImpl")
    private UserDao userDao;

    @Value(value = "zhangsan")
    private String name;

    public void add(){
        userDao.add();
        System.out.println("userService.add()执行。。。"+name);
    }
}

        4、完全注解开发(实际开发会用spring boot)

        创建配置类,替代xml文件

@Configuration
@ComponentScan(basePackages = {"com.bjgy.spring5"})
public class SpringConfig {
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值