Spring框架学习小结

92 阅读2分钟

「这是我参与11月更文挑战的第8天,活动详情查看:2021最后一次更文挑战」。

引言:最近学习了Spring框架,再次记录下学习过程中的几个知识点。

Spring IoC

IoC(控制反转)也被称为依赖注入(DI)。与其说Ioc是一种技术不如说其是一种思想。简单来说其就是使你设计好的对象交给容器控制,而不是你在对象内部进行控制。

然后需要使用对象的时候就去向IoC容器要,而不需要考虑该对象什么时候创建什么时候删除。这样可以有效的降低对象之间的耦合性,使每个对象都比较独立,毕竟对象之间的调用和联系都是通过IOC容器来获取的。

Spring Bean

bean是一个被实例化,组装通过Spring Ioc进行管理的对象。bean是通过容器提供的配置元数据创建的,配置元数据以XML、Java注解或Java代码表示。

bean的属性表如下图所示:

image.png

接下来我们来一个小例子:

首先先创建一个User类:

package com.cao.pojo;

public class User {
    private String name;

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--  name多个别名  -->
    <bean id="u" class="com.cao.pojo.User" name="Us aa">
        <property name="name" value="ccc"/>
    </bean>

</beans>

再写一个Tset类:

import com.cao.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    public static void main(String[] args) {
        //读取配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //获取配置文件中bean对象
        User user = (User) context.getBean("aa");

        System.out.println(user.getName());
    }
}

最后可以得到结果为ccc。

Spring AOP

AOP(面向切面编程)也是一种编程思想,是面向对象编程的一种补充,AOP要达到的效果是,保证开发者不修改源代码的前提下,去为系统中的业务组件添加某种通用功能。简单讲的话AOP其实是为了实现不同模块间一些重复代码的复用。

面向切面编程AOP中涉及到的设计模式是动态代理模式,动态代理模式代理的是接口。

下面来个例子

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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--  注册bean  -->
    <bean id="userService" class="com.service.UserServiceImpl"/>
    <bean id="log" class="com.log.Log"/>
    <bean id="afterLog" class="com.log.AfterLog"/>

    <!-- 配置aop,导入aop约束   -->
    <aop:config>
        <!--   切入点:expression表达式     -->
        <aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>

        <!--  执行环绕      -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>


</beans>

Log类

package com.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {


    @Override
    public void afterReturning(Object returnVal, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnVal);
    }
}

Test类

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 动态代理代理的是接口

        UserService userService = (UserService) context.getBean("userService");

        userService.add();
    }

}

运行结果如下,可以看见在方法执行的末尾输出了一段提示,这段提示可以运行在任何.xml中注册的方法中。

image.png