spring 5.1.5版本(三)

spring 5.1.5版本(一)

spring 5.1.5版本(二)

spring 5.1.5版本(三)

spring 5.1.5版本(四)

spring 5.1.5版本(五)

demo地址:https://gitee.com/gwlCode/springDemo

注解

导包

448146790d6762a56b3fa7b3f5f354830a2.jpg

配置文件添加

xmlns:context="http://www.springframework.org/schema/context"
    <!--
        指定扫描 com.company.annotations 包及所有子孙包下的所有类中的注解
    -->
    <context:component-scan base-package="com.company.annotations"></context:component-scan>
package com.company.annotations;

import com.company.bean.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

//相当于<bean name="user2" class="com.company.bean.User"></bean>
//四种注解无区别,用于区分识别对象属于哪一层
//@Component("user2")
//@Service("user2") //service层
//@Controller("user2") //web层
@Repository("user2") //dao层

@Scope(scopeName = "prototype")
public class User2 {

    //@Value("tom")
    private String name;
    private Integer age;
    private Car2 car2;

    public String getName() {
        return name;
    }

    /**
     * 两种
     * 1.放在成员变量:     通过反射的Field赋值
     * 2.放在setName方法: 通过set方法赋值
     */
    @Value("tom")
    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    @Value("18")
    public void setAge(Integer age) {
        this.age = age;
    }

    public Car2 getCar2() {
        return car2;
    }

    /**
     * 对象注入
     *
     * @Autowired
     * @Qualifier("car2") 上两个组合使用或者直接使用 @Resource(name = "car2")
     */
    //@Autowired
    //@Qualifier("car2")
    @Resource(name = "car2")
    public void setCar2(Car2 car2) {
        this.car2 = car2;
    }

    @PostConstruct
    public void init() {

    }

    @PreDestroy
    public void destory() {
        
    }
}

spring aop

导包

fb7ac0442e15f357abbce3ac3eccaf7e745.jpg

准备目标对象

package com.company.aop;

public class UserServiceImpl implements UserService {

    @Override
    public void save() {

    }

    @Override
    public void delete() {

    }

    @Override
    public void update() {

    }

    @Override
    public void find() {

    }
}

准备通知

package com.company.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAdvice {

    //前置通知
    public void before() {
        System.out.println("这是前置通知");
    }
    //后置通知
    public void afterReturning() {
        System.out.println("这是后置通知(如果出现异常不会调用)");

    }
    //环绕通知
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("这是环绕通知之前的部分");
        Object proceed = proceedingJoinPoint.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分");

        return proceed;
    }
    //异常拦截通知
    public void afterException() {
        System.out.println("出现异常");

    }
    //后置通知
    public void after() {
        System.out.println("这是后置通知(如果出现异常也会调用)");

    }
}

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

    <!--导入 xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation 添加 http://www.springframework.org/schema/aop/spring-aop.xsd
    -->

    <!--配置目标对象-->
    <bean name="userService" class="com.company.aop.UserServiceImpl"></bean>

    <!--配置通知对象-->
    <bean name="myAdvice" class="com.company.aop.MyAdvice"></bean>

    <!--配置将通知织入目标对象-->
    <aop:config>
        <!-- 配置切入点
        public void com.company.aop.UserServiceImpl.save()
        void com.company.aop.UserServiceImpl.save()
        * com.company.aop.UserServiceImpl.save()
        * com.company.aop.UserServiceImpl.*()
        * com.company.aop.UserServiceImpl.*(..)
        * com.company.aop.*ServiceImpl.*(..)
        * com.company.aop..*ServiceImpl.*(..)
        -->
        <aop:pointcut id="pc" expression="execution(* com.company.aop.*ServiceImpl.*(..))"></aop:pointcut>

        <aop:aspect ref="myAdvice">
            <!--指定名为before的方法作为前置通知-->
            <aop:before method="before" pointcut-ref="pc"></aop:before>
            <aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning>
            <aop:around method="around" pointcut-ref="pc"></aop:around>
            <aop:after-throwing method="afterException" pointcut-ref="pc"></aop:after-throwing>
            <aop:after method="after" pointcut-ref="pc"></aop:after>
        </aop:aspect>

    </aop:config>

</beans>

错误提示:Caused by: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 17; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'aop:config' 的声明。

添加  xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

测试

package com.company.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/company/aop/applicationContext.xml")
public class Demo {

    @Resource(name = "userService")
    private UserService userService;

    @Test
    public void func() {

        userService.save();
    }
}

输出

154b9aada7ae3925c77bd9840f6197196b0.jpg

注解配置

<?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="userService" class="com.company.aop.UserServiceImpl"></bean>

    <!--配置通知对象-->
    <bean name="myAdvice" class="com.company.aop.MyAdvice"></bean>

    <!--配置将通知织入目标对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
package com.company.aop2;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

//表示该类为通知类
@Aspect
public class MyAdvice {

    @Pointcut("execution(* com.company.aop.*ServiceImpl.*(..))")
    public void pc() {

    }

    //前置通知
    @Before("MyAdvice.pc()")
    public void before() {
        System.out.println("这是前置通知");
    }

    //后置通知
    @AfterReturning("MyAdvice.pc()")
    public void afterReturning() {
        System.out.println("这是后置通知(如果出现异常不会调用)");

    }

    //环绕通知
    @Around("MyAdvice.pc()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("这是环绕通知之前的部分");
        Object proceed = proceedingJoinPoint.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分");

        return proceed;
    }

    //异常拦截通知
    @AfterThrowing("MyAdvice.pc()")
    public void afterException() {
        System.out.println("出现异常");

    }

    //后置通知
    @After("MyAdvice.pc()")
    public void after() {
        System.out.println("这是后置通知(如果出现异常也会调用)");

    }
}

 

转载于:https://my.oschina.net/gwlCode/blog/3028280

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR 5436 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/back/comment_list.html]")] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'list' cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
06-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值