很简单的一个程序
1.接口public interface ArithmeticCalculator {
public int add(int i,int j);  
    public int sub(int i,int j);  
    public int mul(int i,int j);  
    public int dev(int i,int j);
}
2.实现类@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

public int add(int i, int j) {  
        int result =i+j;  
        return result;  
    }  
  
public int sub(int i, int j) {  
        int result =i-j;  
        return result;  
    }  
  
public int mul(int i, int j) {  
        int result =i*j;  
        return result;  
    }  
  
public int dev(int i, int j) {  
        int result =i/j;  
        return result;  
    }  

}
3.切面@Component(value="loggingAspect")
public class LoggingAspect {

public void beforeMethod(JoinPoint joinPoint){  
        /*String methodName=joinPoint.getSignature().getName();  
        List<Object> args=Arrays.asList(joinPoint.getArgs());*/  
        System.out.println("the method "/*+methodName+" begins with "+args*/ );  
    }  

public void afterMethod(JoinPoint joinPoint){  
        String methodName=joinPoint.getSignature().getName();  
        List<Object> args=Arrays.asList(joinPoint.getArgs());  
        System.out.println("the method "+methodName+" ends" );  
    }  
      
  
public void afterReturnMethod(JoinPoint joinPoint,Object result){  
        String methodName=joinPoint.getSignature().getName();  
        System.out.println("the method "+methodName+" end ,and result with "+result );  
    } 

public void afterThrowing(JoinPoint joinPoint,Exception ex){  
        String methodName=joinPoint.getSignature().getName();  
        System.out.println("the method "+methodName+" end ,and Excetion with "+ex );  
    }

    public Object aroundMethod(ProceedingJoinPoint pjd){  
        Object result=null;  
        String methodName=pjd.getSignature().getName();  
        try {  
            //前置通知  
            System.out.println("the method "+methodName+" begins with " +Arrays.asList(pjd.getArgs()));  
            //执行目标方法  
            result =pjd.proceed();  
            //返回通知  
            System.out.println("the method "+methodName+" return with " + result);  
        } catch (Throwable e) {  
            // 异常通知  
            e.printStackTrace();  
        }  
        //后置通知  
        System.out.println("the method "+methodName+" ends");  
        return result;    
    }  
    
}
4.测试类@SuppressWarnings("resource")
public class XmlTest {
public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextXml.xml");
ArithmeticCalculatorImpl manager = context.getBean(ArithmeticCalculatorImpl.class); int x = manager.add(3, 4);
System.out.println(x); }
}
5.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
    <!-- 启动@AspectJ支持 -->
    <aop:aspectj-autoproxy/>
    
    <!-- 指定自动搜索Bean组件,自动搜索切面类 -->
    <context:annotation-config/>
    <context:component-scan base-package="com.xml">  
    </context:component-scan>        
    <!-- Vincent's xml property -->
    
    
    <!-- 配置aop 切入点,切面,通知-->  
<aop:config>  
    <!-- 配置拦截表达式 切入点:符合切入点表达式的类要产生代理对象,expression就是切入点表达式 -->  
    <aop:pointcut id="testOperation"  
    expression="execution(* com.xml.arithmeticcalculator.ArithmeticCalculator*.*(..))"/>  
    <!-- 配置切面 -->  
    <aop:aspect id="calculatorloggingAspect" ref="LoggingAspect">  
         <!--前置通知    
               1、在目标方法执行之前    
               2、前置通知中,方法有一个参数JoinPoint  -->    
        <aop:before method="beforeMethod" pointcut-ref="testOperation" />  
         <!--后置通知    
               1、在目标方法执行之后    
               2、能够获取目标方法的返回值  result  
                 returning="result" result就是切面中 后置通知中的参数名称  (须一致)  
                3、如果目标方法产生异常,则后置通知不再执行   -->    
         <aop:after-returning method="afterReturnMethod" pointcut-ref="testOperation" returning="result"/>   
        <!--异常通知    
                          获取目标方法抛出的异常信息   throwing="ex"  -->    
         <aop:after-throwing method="afterThrowing" pointcut-ref="testOperation" throwing="ex"/>   
         <!-- 最终通知 -->    
         <aop:after method="afterMethod" pointcut-ref="testOperation"/>   
        <!-- 环绕通知    
                    能够控制目标方法的执行    
                    环绕通知可以有返回值,这个返回值就是代理对象的方法的返回值    
                    前置通知和后置通知只能在目标方法执行之前和之后加代码,但是不能控制目标方法的执行   -->    
        <!-- <aop:around method="aroundMethod" pointcut-ref="testOperation"/> -->  
    </aop:aspect>  
</aop:config>  
</beans>
如上,xml中,当把aop的标签注释掉,程序就不报错,如果加上,出现如下错误信息:
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3abbfa04: defining beans [arithmeticCalculatorImpl,loggingAspect,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,testOperation,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xml.arithmeticcalculator.ArithmeticCalculatorImpl] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:295)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)
at com.xml.main.XmlTest.main(XmlTest.java:16)求教!!!
送分哦~~~

解决方案 »

  1.   

    看你的异常,做出以下修改//ArithmeticCalculatorImpl manager = context.getBean(ArithmeticCalculatorImpl.class);
    ArithmeticCalculatorImpl manager = context.getBean("arithmeticCalculatorImpl");
      

  2.   

    当一个组件在某个扫描过程中被自动检测到时,会根据那个扫描器的BeanNameGenerator 策略生成它的bean名称。默认情况下,任何包含 name值的Spring“典型”注解 (@Component、@Repository、 @Service和@Controller) 会把那个名字 提供给相关的bean定义。如果这个注解不包含name值或是其他检测到的组件 (比如被自定义过滤器发现的),默认bean名称生成器会返回小写开头的非限定(non-qualified)类名。