首页 新闻 搜索 专区 学院

今天使用ApplicationContextAware写了一个工具类 用来获取bean 时报错,错误在最下面。

0
[待解决问题]

今天使用ApplicationContextAware写了一个工具类 用来获取bean 时报错,错误在最下面。
[code=java]

复制代码
package com.zdmoney.riskmgt.ruleEngine.reflect;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* @author zhangc
* @since 2018/7/18 18:02
**/
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext context;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}


public static <T> T getBeanByClass(Class<T> c){
return context.getBean(c);
}
public static Object getBeanByName(String name){
return context.getBean(name);
}


}
复制代码

 

[/code]
[color=#FF0000]
我使用 SpringContextUtils.getBeanByClass(ZaService.class) 报如下错误, 但是用 IZaService.class 正常获取到(IZaService是ZaService的接口。本问题中的 I 开头的都是相应的 接口类。。)

我的排错过程
1. 于是我尝试使用 SpringContextUtils.getBeanByName("zaService") 也是正常的。。
2. 我尝试使用 SpringContextUtils.getBeanByClass(RiskService.class) 正常
SpringContextUtils.getBeanByClass(IRiskService.class) 正常
3. 于是我比较 ZaService 和 RiskService 和相应接口, 都是普通的service 和接口啊。没什么本质区别啊
4. 我把ZaService 中的 方法清空。再次尝试 SpringContextUtils.getBeanByClass(ZaService.class)竟然可以获取了。。
5. 可能是代码中有什么影响了spring容器。。 最后发现是 一个注解造成的 ,, 去掉该注解则可以正常获取bean

[code=java]@Override
@ExternalDsAccess(partner = DataSourcePartner.ZA,source = DataSourceEnum.ZA_SJSM)
public RiskResultDto<String> checkThreeElements(Map<String,Object> param) {
[/code]
该注解是一个切面的注解 保存操作日志使用的 。
[code=java]
@Pointcut("@annotation(com.zdmoney.riskmgt.component.aspect.ExternalDsAccess)")
public void dataSourceProcess() {
}

@AfterReturning(pointcut = "dataSourceProcess()", returning = "object")
public void afterProcess(JoinPoint joinPoint, Object object) {
[/code]

有人遇到这种情况吗 ,,求解救。?????????????????????????????


org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.zdmoney.riskmgt.support.zhongan.service.impl.ZaService] 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:1057)
at com.zdmoney.riskmgt.ruleEngine.reflect.SpringContextUtils.getBeanByClass(SpringContextUtils.java:28)
at com.zdmoney.riskmgt.component.queue.service.SpringContext.SpringContextUtilsTest.getBeanByClassTest(SpringContextUtilsTest.java:18)
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:498)
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.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


 


远方的人的主页 远方的人 | 初学一级 | 园豆:7
提问于:2018-07-19 14:14

不要沉啊 ,,,

远方的人 2年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册