为什么我的会报错

为什么我的会报错

package com.imooc.aop.demo4;

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/imooc/aop/demo4/applicationContext2.xml")
public class SpringDemo4 {
    @Resource(name="customDao")
    private CustomDao customDao;

    @Test
    public void demo2(){
        customDao.save();
        customDao.update();
        customDao.delete();
        customDao.find(); 
    }
}
package com.imooc.aop.demo4;

public class CustomDao {
    public void find(){
        System.out.println("查询客户");
    }
    public void update(){
        System.out.println("修改客户");
    }
    public void save(){
        System.out.println("保存客户");
    }
    public void delete(){
        System.out.println("删除客户");
    }
}
package com.imooc.aop.demo4;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyAroundAdvice implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {

        System.out.println("环绕前增强========");
        Object obj = invocation.proceed();
        System.out.println("环绕后增强==========");

        return obj;
    }
}
<?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="customDao" class="com.imooc.aop.demo4.CustomDao"/>
    <!--配置通知类 -->
    <bean id="myAroundAdvice" class="com.imooc.aop.demo4.MyAroundAdvice"/>
  <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <!--pattern中配置正则表达式-->
      <property name="pattern" value=".*"></property>
      <property name="advice" ref="myAroundAdvice"></property>
  </bean>
    <!--配置产生代理 -->
    <bean id="customDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="customDao"></property>
        <property name="proxyTargetClass" value="true"></property>
        <property name="interceptorNames" value=""></property>
    </bean>



</beans>
<?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>com.imooc</groupId>
  <artifactId>spring_aop</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>spring_aop Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>jsr250-api</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>spring_aop</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

1、首先,老师测试了同学的代码,没有发送异常,但是也没有起到作用,这里老师做了一下修改:

http://img1.sycdn.imooc.com//climg/5c9836810001113a09120219.jpg

同学这里忘记填写了

2、这应该注入的是它的代理类

http://img1.sycdn.imooc.com//climg/5c9836a1000148e708160409.jpg

3、运行结果如下:

http://img1.sycdn.imooc.com//climg/5c9836c60001e1a511480745.jpg

4、同学的报错信息因为没有贴上了,老师无法判断同学的错误,但经过老师的测试,同学的代码经过以上修改应该是没有问题的。建议同学可以新建一个工程,把代码粘过去再试试。

如果项目中有Import Changes:请点击这个选项

http://img1.sycdn.imooc.com//climg/5c9837300001a2b103830108.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

chrismorgen 2019-03-24 11:14:32

你好同学,请问你是哪里报错呢?建议你将报错提示粘贴一下,方便老师具有针对性的为你解答,祝学习愉快~

  • 提问者 李个浪 #1
    是运行测试类的时候报错 报错信息超过8000字粘贴不了
    2019-03-24 11:33:55
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师