首页 新闻 会员 周边 捐助

使用spring的aop出现了个小问题

0
悬赏园豆:5 [待解决问题]

配置文件加上<aop:aspectj-autoproxy></aop:aspectj-autoproxy>就抛下面的异常信息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.net.bysoft.lesson7.ArithmenticImpl] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)
    at cn.net.bysoft.lesson7.Lesson7Test.test(Lesson7Test.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

 

jar包如下:

配置文件:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

    <context:component-scan base-package="cn.net.bysoft.lesson7">
    </context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans> 

几个测试类:

package cn.net.bysoft.lesson7;

public interface Arithmentic {
    int add(int i, int j);
    int sub(int i, int j);
}
package cn.net.bysoft.lesson7;

import org.springframework.stereotype.Component;

@Component
public class ArithmenticImpl implements Arithmentic {

    @Override
    public int add(int i, int j) {
        return i + j;
    }

    @Override
    public int sub(int i, int j) {
        return i - j;
    }

}
package cn.net.bysoft.lesson7;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    
    @Before("execution(public int cn.net.bysoft.lesson7.Arithmentic.add(int, int))")
    public void beforeMethod() {
        System.out.println("The method begins");
    }
}
package cn.net.bysoft.lesson7;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Lesson7Test {

    private AbstractApplicationContext ctx;

    @Before
    public void initApplicationContext() {
        // 创建Spring容器
        ctx = new ClassPathXmlApplicationContext(
                "applicationContext-lesson7.xml");
    }

    @Test
    public void test(){
        Arithmentic arithmentic = ctx.getBean(ArithmenticImpl.class);
        int result = arithmentic.add(3, 6);
        System.out.println(result);
        ctx.close();
    }

}

删除配置文件中的<aop:aspectj-autoproxy></aop:aspectj-autoproxy>就不报错了。。。

初学,求教,谢谢!

TheBlackPearl的主页 TheBlackPearl | 初学一级 | 园豆:7
提问于:2016-04-26 23:24
< >
分享
所有回答(1)
0

问题找到了。

测试类错误!马虎了!

@Test
    public void test(){
//这句话错误,不应该用impl去get //Arithmentic arithmentic
= ctx.getBean(ArithmenticImpl.class);
//改成
     Arithmentic arithmentic = ctx.getBean(Arithmentic.class);
int result = arithmentic.add(3, 6); System.out.println(result); ctx.close(); }
TheBlackPearl | 园豆:7 (初学一级) | 2016-04-26 23:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册