这样写是可以的,不知道楼主是怎么获取为null的呢?

解决方案 »

  1.   

    可以直接注入的。问题是,你注入的地方是怎么声明属性和配置的。另外声明周期,applicationContext的作用域也要考虑清楚。
      

  2.   

    我是注入的类extends AbstractDependencyInjectionSpringContextTests,声明    private BServiceImpl bService; 设置getter setter method
      

  3.   

    同时声明的还有private AService aService;  这个是okay的
      

  4.   


    能否把类源码发出来?还有BServiceImpl的源码
      

  5.   

    public class AServiceImpl implements AService { @Override
    public void fooA(String _msg) {
      System.out.println("AServiceImpl.fooA(msg:"+_msg+")");   } @Override
    public void barA() {
    System.out.println("AServiceImpl.barA()");  
    }}
    public interface AService { public void fooA(String _msg);  
      
        public void barA();  

    }
    public class AServiceImpl implements AService { @Override
    public void fooA(String _msg) {
      System.out.println("AServiceImpl.fooA(msg:"+_msg+")");   } @Override
    public void barA() {
    System.out.println("AServiceImpl.barA()");  
    }}public class AOPTest extends AbstractDependencyInjectionSpringContextTests{ private AService aService;  
        
        private BServiceImpl bService;  
          
        
        @Override 
        protected String[] getConfigLocations() {  
            String[] configs = new String[] { "/app.xml"};  
            return configs;  
        }  
        
        /** 
         * 测试正常调用 
         */  
        public void testCall()  
        {  
            System.out.println("SpringTest JUnit test");  
            aService.fooA("JUnit test fooA");  
            aService.barA();  
            bService.fooB();  
            bService.barB("JUnit test barB",0);  
        }  
          
        /** 
         * 测试After-Throwing 
         */  
        public void testThrow()  
        {  
            try {  
                bService.barB("JUnit call barB",1);  
            } catch (IllegalArgumentException e) {  
                  
            }  
        }  
          
        public void setAService(AService service) {  
            aService = service;  
        }  
          
        public void setBService(BServiceImpl service) {  
            bService = service;  
        } public void setaService(AService aService) {
    this.aService = aService;
    } public void setbService(BServiceImpl bService) {
    this.bService = bService;
    }  

        
    }  <aop:config>  
            <aop:aspect id="TestAspect" ref="aspectBean">  
                <!--配置com.spring.service包下所有类或接口的所有方法-->  
                <aop:pointcut id="businessService"  
                    expression="execution(* com.spring.service.*.*(..))" />  
                <aop:before pointcut-ref="businessService" method="doBefore"/>  
                <aop:after pointcut-ref="businessService" method="doAfter"/>  
                <aop:around pointcut-ref="businessService" method="doAround"/>  
                <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
            </aop:aspect>  
        </aop:config>  
          
        <bean id="aspectBean" class="com.spring.aop.TestAspect" ></bean>  
        <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
        <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  public class AOPTest extends AbstractDependencyInjectionSpringContextTests{ private AService aService;  
        
        private BServiceImpl bService;  
          
        
        @Override 
        protected String[] getConfigLocations() {  
            String[] configs = new String[] { "/app.xml"};  
            return configs;  
        }  
        
        /** 
         * 测试正常调用 
         */  
        public void testCall()  
        {  
            System.out.println("SpringTest JUnit test");  
            aService.fooA("JUnit test fooA");  
            aService.barA();  
            bService.fooB();  
            bService.barB("JUnit test barB",0);  
        }  
          
        /** 
         * 测试After-Throwing 
         */  
        public void testThrow()  
        {  
            try {  
                bService.barB("JUnit call barB",1);  
            } catch (IllegalArgumentException e) {  
                  
            }  
        }  
          
        public void setAService(AService service) {  
            aService = service;  
        }  
          
        public void setBService(BServiceImpl service) {  
            bService = service;  
        } public void setaService(AService aService) {
    this.aService = aService;
    } public void setbService(BServiceImpl bService) {
    this.bService = bService;
    }  

        
    }
    如果app中的配置<bean id="bService" class="com.spring.service.BServiceImpl"></bean>  和AOPTest中bService相关全部删掉的话,aService调用方法执行aop就okay,如果配置中不删掉bService,执行AOPTest就报异常java.lang.noSuchMethod:org.springframework.util.ClassUtils.isCglibProxyClass
      

  6.   

    上面AServiceImpl发重复,加上public class BServiceImpl { public void barB(String _msg, int _type) {  
            System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")");  
            if(_type == 1)  
                throw new IllegalArgumentException("测试异常");  
        }  
      
        public void fooB() {  
            System.out.println("BServiceImpl.fooB()");  
        }  

    }
      

  7.   

    public class TestAspect {
     public void doAfter(JoinPoint jp) {  
            System.out.println("log Ending method: "  
                    + jp.getTarget().getClass().getName() + "."  
                    + jp.getSignature().getName());  
        }  
      
        public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
            long time = System.currentTimeMillis();  
            Object retVal = pjp.proceed();  
            time = System.currentTimeMillis() - time;  
            System.out.println("process time: " + time + " ms");  
            return retVal;  
        }  
      
        public void doBefore(JoinPoint jp) {  
            System.out.println("log Begining method: "  
                    + jp.getTarget().getClass().getName() + "."  
                    + jp.getSignature().getName());  
        }  
      
        public void doThrowing(JoinPoint jp, Throwable ex) {  
            System.out.println("method " + jp.getTarget().getClass().getName()  
                    + "." + jp.getSignature().getName() + " throw exception");  
            System.out.println(ex.getMessage());  
        }  
      
        private void sendEx(String ex) {  
            //TODO 发送短信或邮件提醒  
        }  
    }
      

  8.   

    新增配置:
    <bean id="aopTest" class="com.spring.aop.AOPTest" >
         <property name="aService" ref="aService"/>
         <property name="bService" ref="bService"/>
    </bean>
    测试调用:
       @Test
       public void testSpring(){             
                    ClassPathXmlApplicationContext context = new                            
                                    ClassPathXmlApplicationContext("config/applicationContext.xml");
    AOPTest test = (AOPTest)context.getBean("aopTest");
    test.testCall();
        }
    加上上面配置,是可以测试通过的,能发一些你那边的调用类的相关代码莫
      

  9.   

    这个必须能呀,另外一个可以获取到,这个获取是null肯定是配置问题了
      

  10.   

    extends AbstractDependencyInjectionSpringContextTests执行时会自动调用test*的method
      

  11.   

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bService' defined in class path resource [app.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.isCglibProxyClass(Ljava/lang/Class;)Z
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.spring.AOPTest.testSpring(AOPTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    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)
    Caused by: java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.isCglibProxyClass(Ljava/lang/Class;)Z
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:159)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 34 more