这样写是可以的,不知道楼主是怎么获取为null的呢?
解决方案 »
- SSH简单范例中出现的问题,求高手解答
- 有类似于ecshop(php)这样比较专业而开源的购物/网店J2ee程序吗?
- 在Weblogic里,配置Data Source Factory和配置Data Source 有什么特别需要注意的吗?
- java程序不能运行
- 邮件错误
- <%@ page contentType="application/xml; charset=gb2312" import="Employee"%>这一句报错
- 如何计算两个java.util.Date之间相差的天数啊?
- jaas中授权问题??各位老大,请现身指教下!
- tcp包数据监视
- 上机题
- java怎么把txt文件中的数据放到ArrayList中呢?求解
- 怎么给table加一个滚动条
能否把类源码发出来?还有BServiceImpl的源码
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
System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")");
if(_type == 1)
throw new IllegalArgumentException("测试异常");
}
public void fooB() {
System.out.println("BServiceImpl.fooB()");
}
}
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 发送短信或邮件提醒
}
}
<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();
}
加上上面配置,是可以测试通过的,能发一些你那边的调用类的相关代码莫
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