Spring框架零基础学习(一):IOC|DI、AOP

参考链接:
HOW2J.CN:Spring
idea创建一个spring项目

一、IDEA创建Spring项目

创建方法:idea创建一个spring项目
maven管理项目,生成项目结构如下:
在这里插入图片描述
在main文件夹下新建Resources目录,并且将此目录设置为资源文件夹,在此文件夹下创建文件applicationContext.xml
在这里插入图片描述
在这里插入图片描述
然后在pom.xml中添加spring的依赖:

<dependency>
   	<groupId>org.springframework</groupId>
  	<artifactId>spring-context</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>

到这里Spring的基本配置弄完了。


二、Spring: IOC和DI

IOC:反转控制,将对象创建的过程交给Spring
DI:依赖注入,拿到的对象,属性可以被注入了相关值,直接可以使用。

我觉得how2j的站长对于spring的IOC的比喻非常好:

传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。
用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。

接下来我们来看看我们如何直接去Spring“餐馆”点一只做好的鸡(对象),以下为写配置文件实现,还可以使用注解实现,注解方法不在此展示

  1. 创建一个Student类和一个Book类
    在这里插入图片描述
public class Book {
    private String name;
    private double money;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
}
public class Student {
    private String name;
    private Book book;
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  1. 在applicationContext.xml中编写关于Student类和Book类的配置:

下面这段代码的意思是通过Spring拿到Student对象时,Spring会注入属性name和book,并且name被赋值为张三,book被赋予名字语文,好比你直接拿到了一个姓名为张三,带着语文书的Student对象

<?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 name="Student" class="whu.xsy.spring.po.Student">
        <property name="name" value="张三"></property>
        <property name="book" ref="Book"></property>
    </bean>
    <bean name="Book" class="whu.xsy.spring.po.Book">
        <property name="name" value="语文" />
    </bean>

</beans>
  1. 获取对象
public class App {
    public static void main( String[] args ){
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        Student s = (Student)context.getBean("Student");
        System.out.println(s.getName());
        System.out.println(s.getBook().getName());
    }
}

结果如下:

张三
语文



三、Spring: AOP

AOP 即 Aspect Oriented Program 面向切面编程。
在面向切面编程的思想中,把功能分为了核心业务功能和周边功能。

核心业务功能:比如登录、增删改查数据库等逻辑业务功能
周边功能(切面):比如记日志、事务管理等等

在项目中,通常将核心业务功能周边功能分别独立开发,在项目运行时又“交织”在一起进行。这种编程思想就叫AOP

  1. 创建SaleService.java文件,作为学生卖书的逻辑业务类
public class SaleService {
    //标价
    public void markPrice(){
        System.out.println("标价服务");
    }
}
  1. 如果每次调用SaleService的对象之前,都要提醒一句话:”当前正在进行交易“,那么每次调用之前,都要写一句(非常麻烦):
System.out.println("当前正在进行交易")

如果使用AOP思想,只需要在applicationContext.xml中配置一下就行。
先在pom.xml中加入如下依赖:

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.9</version>
    </dependency>

创建专门的切面类(用于日志提示):

public class LoggerAspect {

    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

编写配置文件:

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

    <bean name="Student" class="whu.xsy.spring.po.Student">
        <property name="name" value="张三"></property>
        <property name="book" ref="Book"></property>
    </bean>
    <bean name="Book" class="whu.xsy.spring.po.Book">
        <property name="name" value="语文" />
    </bean>

    <bean name="SaleService" class="whu.xsy.spring.service.SaleService">
    </bean>

    <bean id="loggerAspect" class="whu.xsy.spring.log.LoggerAspect"/>

    <aop:config>
        <aop:pointcut id="loggerCutpoint"
                      expression=
                              "execution(* whu.xsy.spring.service.SaleService.*(..)) "/>

        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>

</beans>
  1. 三次调用SaleService对象
public class App {
    public static void main( String[] args ){
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        Student s = (Student)context.getBean("Student");
        SaleService service = (SaleService)context.getBean("SaleService");

        for(int i = 0; i < 3; i++){
            service.markPrice();
        }
    }
}

结果为(每次调用markPrice时,都在前后打印了日志):

start log:markPrice
标价服务
end log:markPrice
start log:markPrice
标价服务
end log:markPrice
start log:markPrice
标价服务
end log:markPrice

原文地址:https://www.cnblogs.com/theory/p/11884313.html