我收到了一个很长的嵌套异常,最后一个是:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] 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 ...
这是我的DAO
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import ar.edu.itba.paw.FileUploadDao;
import ar.edu.itba.paw.models.UploadFile;
@Repository
public class FileUploadDAOImpl implements FileUploadDao {
@Autowired
private SessionFactory sessionFactory;
public FileUploadDAOImpl() {
}
public FileUploadDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public void save(UploadFile uploadFile) {
sessionFactory.getCurrentSession().save(uploadFile);
}
}
这是DAO所在的持久性文件夹中的pom.xml。
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ar.edu.itba.paw</groupId>
<artifactId>paw2018b</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ar.edu.itba.paw</groupId>
<artifactId>interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
<name>interfaces</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>ar.edu.itba.paw</groupId>
<artifactId>models</artifactId>
<version>${parent.version}</version>
</dependency>
</dependencies>
</project>
pom.xml父亲包括:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
在WebConfig.java内部,我得到了:
@Autowired
@Bean(name = "fileUploadDao")
public FileUploadDao getUserDao(SessionFactory sessionFactory) {
return new FileUploadDAOImpl(sessionFactory);
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(20971520); // 20MB
multipartResolver.setMaxInMemorySize(1048576); // 1MB
return multipartResolver;
}
我对Spring / Hibernate没有深刻的了解。 我实际上是在关注:https://www.codejava.net/coding/upload-files-to-database-with-spring-mvc-and-hibernate
我正在使用PostgreSQL。
我应该显示的其他任何代码,请告诉我。
答案 0 :(得分:1)
将以下依赖项添加到pom,
(Assuming you're using mysql)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version><!--version--></version>
<scope>runtime</scope>
</dependency>
一个正确的连接池,
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>7.0.55</version>
</dependency>
由于您使用的是Java配置,请尝试将SessionFactory
定义为@Bean
。
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(
new String[] { "base.package.to.scan" }
);
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("drivr");
dataSource.setUrl("url"));
dataSource.setUsername("uname");
dataSource.setPassword("passwd");
return dataSource;
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", create);
setProperty("hibernate.dialect", dielect_to_use);
}
};
}
您可能还需要事务管理器,
@Bean
@Autowired
public HibernateTransactionManager transactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}
希望这会有所帮助。
答案 1 :(得分:0)
尝试添加sessionFactory bean,您可以看下面的示例。 (dataSoure与Oracle jdbc支持一起使用)
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="URL" value="jdbc:oracle:thin:@ip:port:sid" />
<property name="user" value="name" />
<property name="password" value="password" />
<property name="connectionCachingEnabled" value="true" />
<property name="connectionCacheProperties">
<props merge="default">
<prop key="InitialLimit">7</prop>
<prop key="MinLimit">3</prop>
<prop key="MaxLimit">100</prop>
</props>
</property>
</bean>
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>your.package</value>
</list>
</property>
<property
name="dataSource"
ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
</props>
</property>
</bean>