Code Bye

spring配置报错

2014-10-27 9:56:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@a981ca: startup date [Mon Oct 27 09:56:20 GMT 2014]; root of context hierarchy
2014-10-27 9:56:21 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ece65: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2014-10-27 9:56:21 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6e293a] to prepare test instance [com.baobaotao.service.TestUserService@54a328]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”com.baobaotao.service.TestUserService””: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.baobaotao.service.UserService com.baobaotao.service.TestUserService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.baobaotao.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:307)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
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)

这是错误信息 , 下面贴出applicationContext.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:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd”>

<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao”/>

<!– 扫描本地service类包,应用Spring注解配置 –>
<context:component-scan base-package=”com.baobaotao.service” />

<!– 配置事务管理器 –>
<bean id=”transactionManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”
p:dataSource-ref=”dataSource” />

<!– 通过aop配置提供事务增强 , 让service 包下所有的bean的所有方法拥有事务 –>
<aop:config proxy-target-class=”true”>
<aop:pointcut expression=”execution(*com.baobaotao.service..*(..))” id=”serviceMethod”/>
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”serviceMethod”/>
</aop:config>
<tx:advice id=”txAdvice” transaction-manager=”transactionManager”>
<tx:attributes>
<tx:method name=”*”/>
</tx:attributes>
</tx:advice>

<!– 定义一个使用dbcp实现的数据源 –>
<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”
destroy-method=”close” p:driverClassName=”com.mysql.jdbc.Driver”
p:url=”jdbc:mysql://localhost:3306/sampledb”
p:username=”root”
p:password=”1234″ />

<!– 定义jdbc模板的bean –>
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”
p:dataSource-ref=”dataSource” />

</beans>

帮我看下哪里有问题啊 ,谢谢啦  

 Error creating bean with name “”com.baobaotao.service.TestUserService””   各位大牛帮看看 

30分
Could not autowire field: private com.baobaotao.service.UserService com.baobaotao.service.TestUserService.userService
是你的类里面定义错了、或是
<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao“/>
看见没有、是你定义初始bean范围太小了、service包里面的类都没有初始化为bean.
<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao“/>
楼上正解,改一下就好了
引用 2 楼 runner1989 的回复:

Could not autowire field: private com.baobaotao.service.UserService com.baobaotao.service.TestUserService.userService
是你的类里面定义错了、或是
<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao“/>
看见没有、是你定义初始bean范围太小了、service包里面的类都没有初始化为bean.

我按照你说的尝试了一下  还是不行 还是报错 !~~ 

引用 5 楼 zhangheng1633 的回复:
Quote: 引用 2 楼 runner1989 的回复:

Could not autowire field: private com.baobaotao.service.UserService com.baobaotao.service.TestUserService.userService
是你的类里面定义错了、或是
<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao“/>
看见没有、是你定义初始bean范围太小了、service包里面的类都没有初始化为bean.

我按照你说的尝试了一下  还是不行 还是报错 !~~ 

报的错变了没、错误信息和上面一样么?

引用 6 楼 runner1989 的回复:
Quote: 引用 5 楼 zhangheng1633 的回复:
Quote: 引用 2 楼 runner1989 的回复:

Could not autowire field: private com.baobaotao.service.UserService com.baobaotao.service.TestUserService.userService
是你的类里面定义错了、或是
<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao“/>
看见没有、是你定义初始bean范围太小了、service包里面的类都没有初始化为bean.

我按照你说的尝试了一下  还是不行 还是报错 !~~ 

报的错变了没、错误信息和上面一样么?

嗯是的  , 报错一样的   

重启服务了么
引用 8 楼 runner1989 的回复:

重启服务了么

我是用Junit4 单元测试

<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao”/>

<!– 扫描本地service类包,应用Spring注解配置 –>
<context:component-scan base-package=”com.baobaotao.service” />

这两个改成一个
<context:component-scan base-package=”com.baobaotao” />

引用 10 楼 fengspg 的回复:

<!– 扫描类包,将标注spring注解的类自动转化为bean,同时完成bean的注入 –>
<context:component-scan base-package=”com.baobaotao.dao”/>

<!– 扫描本地service类包,应用Spring注解配置 –>
<context:component-scan base-package=”com.baobaotao.service” />

这两个改成一个
<context:component-scan base-package=”com.baobaotao” />

已经试过了 , 还是那样的  

问题我自己解决了 , 分数给第一位热心的网友吧 ,  谢谢热心的帮助 

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring配置报错