Spring能否直接注入无接口具体类

凤凰涅槃 2013-12-06 10:24:17
<bean id="bService" class="com.spring.service.BServiceImpl"></bean>
BServiceImpl 没有接口,直接在spring中配置获取的是null,求高手
...全文
677 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
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
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
extends AbstractDependencyInjectionSpringContextTests执行时会自动调用test*的method
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
引用 11 楼 u011213572 的回复:
新增配置: <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(); } 加上上面配置,是可以测试通过的,能发一些你那边的调用类的相关代码莫
还是报相同的错误
aspirin199008 2013-12-09
  • 打赏
  • 举报
回复
这个必须能呀,另外一个可以获取到,这个获取是null肯定是配置问题了
UDown 2013-12-09
  • 打赏
  • 举报
回复
新增配置: <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(); } 加上上面配置,是可以测试通过的,能发一些你那边的调用类的相关代码莫
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
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 发送短信或邮件提醒 } }
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
上面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()"); } }
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
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
oh_Maxy 2013-12-09
  • 打赏
  • 举报
回复
引用 5 楼 hailongshih 的回复:
我是注入的类extends AbstractDependencyInjectionSpringContextTests,声明 private BServiceImpl bService; 设置getter setter method
能否把类源码发出来?还有BServiceImpl的源码
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
同时声明的还有private AService aService; 这个是okay的
凤凰涅槃 2013-12-09
  • 打赏
  • 举报
回复
我是注入的类extends AbstractDependencyInjectionSpringContextTests,声明 private BServiceImpl bService; 设置getter setter method
别闹腰不好 2013-12-06
  • 打赏
  • 举报
回复
三种注入方式 设值注入,接口注入,构造方法注入。 你的是那种?
少羽 2013-12-06
  • 打赏
  • 举报
回复
这样写是可以的,不知道楼主是怎么获取为null的呢?
SimonDW 2013-12-06
  • 打赏
  • 举报
回复
可以直接注入的。 问题是,你注入的地方是怎么声明属性和配置的。 另外声明周期,applicationContext的作用域也要考虑清楚。
ghw554557313 2013-12-06
  • 打赏
  • 举报
回复
这样的用构造注入一试

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧