我收到错误的autowired bean。在测试类之外它可以工作。做错了什么?如何提升应用程序上下文。甚至指定哪些部分?这是我的代码:
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.0.RELEASE</org.springframework-version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<scope>test</scope>
</dependency>
根context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="hibernate-config.xml"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
//
</bean>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/root- context.xml" })
public class AdminManagerTest5 {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
@Test
public void t(){
System.out.print(sessionFactory);
}
}
冬眠-配置
bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>WEB-INF/spring/hibernate.cfg.xml</value>
</property>
ERROR
Caused by: java.io.FileNotFoundException: class path resource [WEB- INF/spring/hibernate.cfg.xml] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:642)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 41 more
P.S.Sryry for my english
答案 0 :(得分:2)
您的sessionFactory
配置指向
<property name="configLocation">
<value>WEB-INF/spring/hibernate.cfg.xml</value>
</property>
但目录WEB-INF/spring
未添加到类路径中,因此Spring无法找到它。
您需要指定file:
前缀路径(相对或绝对路径)或将文件放在类路径中的某个位置。
答案 1 :(得分:1)
你正在使用spring-test 4.0和3.1.0.RELEASE版本的其他弹簧罐。不要那样做。使用spring-test 3.1.0,或使用所有其他弹簧罐的4.0版本,但不要将不兼容的版本混合在一起。