org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '全类名'
该错误是在练习Spring - aop的时候出现的先说总结有利于大家还有没有必要往后面在继续看下去:错误出在获取bean的时候,我获取的是该接口的实现类类型,解决办法就是获取该接口的类型,不要获取该接口的实现类项目架构Maven<dependencies><!--spring核心包--><dependenc...
·
该错误是在练习Spring - aop的时候出现的
先说总结有利于大家还有没有必要往后面在继续看下去:错误出在获取bean的时候,我获取的是该接口的实现类类型,解决办法就是获取该接口的类型,不要获取该接口的实现类
项目架构
Maven
<dependencies>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!--aop 需要的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- spring 核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- 日志包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描com.stone.dome包下的所有注解 -->
<context:component-scan base-package="com.stone.aopdome"/>
<!-- 开启基于注解的aop 功能-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
Operation类
@Component
public interface Operation
{
// 加法
double add(double i,double j);
// 减法
double subtract(double i,double j);
// 乘法
double mul(double i,double j);
// 除法
double div(double i,double j);
}
Operation目标类
@Component
public class OperationImpl implements Operation
{
@Override
public double add(double i, double j)
{
return i+j;
}
@Override
public double subtract(double i, double j)
{
return i-j;
}
@Override
public double mul(double i, double j)
{
return i*j;
}
@Override
public double div(double i, double j)
{
return i/j;
}
}
OperationProxy 目标实现类
@Aspect
@Component
public class OperationProxy
{
// execution切入点表达式 (访问修饰符 返回值类型 目标执行的方法)
@Before("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
public void logbefore(){
System.out.println("我是前置通知");
}
@After("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
public void logAfter(){
System.out.println("我是后置通知");
}
@AfterThrowing("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
public void logException(){
System.out.println("我是异常通知");
}
@AfterReturning("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
public void logFinal(){
System.out.println("我是最终通知");
}
}
SpringAopTest 测试类
public class SpringAopTest
{
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testaop1(){
Operation bean = ioc.getBean(OperationImpl.class);
double add = bean.add(12.0, 20.0);
System.out.println(add);
}
}
个人本来以为是缺少了什么Maven依赖造成的 ,可原因并不是如此,而是我获取bean的时候,是获取了该类的实现类,解决错误的办法就是在获取bean的时候,不要获取目标对象的实现类,直接获取该接口类就行
错误代码
错误修改
更多推荐
已为社区贡献1条内容
所有评论(0)