Spring框架学习---IDEA创建最简单spring demo(实现最基本的IOC与AOP)

1.spring最重要的两个概念

IOC:控制反转,把对象或相关资源的创建与管理交由IOC容器,实现解耦。

AOP:面向切面编程,把多次用到的模板代码作为切面,代码中某个需要切面的具体位置作为连接点,切点负责联系切面与连接点,在连接点织入切面。

2.所依赖的jar包

spring-beans:spring核心包,spring中一切资源皆为bean

spring-core:spring核心包,包含一系列bean管理工具

spring-context:spring核心包,应用上下文:可认为是IOC容器,IOC功能实现必导包

spring-aop:AOP功能实现必导包

aspectjweaver:提供完整的aop支持

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringProjectTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

<!--        spring万物皆是bean,提供bean实现-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

<!--        提供bean管理-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

        <!--        提供aop支持-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

<!--        spring应用上下文,提供IOC支持-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

<!--        提供完整的aop支持-->
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>


    </dependencies>

</project>

3.测试

  • 在已经搭建好的Maven工程中,往pom文件导入上述jar包
  • 创建相关类

person类

package com.demo.pojo;

/**
 * @Author YMJ
 * @Description TODO
 * @DATE 2019-12-03 20:16
 * @Version 1.0
 */
public class Person {
    String name;
    public Person(){}

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

 personService类

package com.demo.service;

import com.demo.pojo.Person;

/**
 * @Author YMJ
 * @Description TODO
 * @DATE 2019-12-04 10:41
 * @Version 1.0
 */
public class PersonService {

    public Person show2(Person person){
        return person;
    }
}

 切面类

package com.demo.advice;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @Author YMJ
 * @Description TODO
 * @DATE 2019-12-03 22:02
 * @Version 1.0
 */

public class PersonAdvice {
    public void before(){
        System.out.println("前置通知");
    }

    public void after(){
        System.out.println("后置通知");
    }
    public void afterReturning(){
        System.out.println("返回通知");
    }
    public void afterThrowing(){
        System.out.println("异常通知");
    }

    /**
     * 环绕通知:可代替上面四个
     * @param proceedingJoinPoint
     */
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        String methodName = proceedingJoinPoint.getSignature().getName();
        System.out.println("环绕通知-前置通知:"+methodName);
        try {
            // 表示调用真实的方法
            Object obj = proceedingJoinPoint.proceed();
            System.out.println("环绕通知-返回通知:"+methodName+" 返回结果:" + obj.toString());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知-异常通知:"+methodName);
        } finally {
            System.out.println("环绕通知-后置通知:"+methodName);
        }

    }
}
  • 创建spring的配置文件springApplicationContext.xm
<?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"
       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/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置person类交由IOC容器管理-->
    <bean id="person" class="com.demo.pojo.Person">
        <property name="name" value="蔡徐坤"></property>
    </bean>

    <bean id="personService" class="com.demo.service.PersonService"></bean>

    <bean id="personAdvice" class="com.demo.advice.PersonAdvice"></bean>

    <!--aop配置-->
    <aop:config>
        <!--配置切面(表示将会调用切面里的,方法,切点对应的方法交连接点)-->
        <aop:aspect ref="personAdvice">
            <aop:pointcut id="cut" expression=" execution(* com.demo.service.*.*(..))" ></aop:pointcut>
            <aop:before method="before"  pointcut-ref="cut"/>
            <aop:after method="after" pointcut-ref="cut"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="cut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="cut"/>
 
            <aop:around method="around" pointcut-ref="cut"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>
  • 创建测试类

测试IOC功能

测试AOP功能

 

 

温馨提示:菜鸡出道,如有错误,请指出,不胜感激。

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值