gpt4 book ai didi

java - Spring 4 + Hibernate 4 配置

转载 作者:行者123 更新时间:2023-11-29 03:09:20 25 4
gpt4 key购买 nike

我正在尝试进行 Spring 4 + Hibernate 4 配置。但是我在 Controller 中面对 sessionFactory = null 。下面是配置和代码。这个配置有什么错误?

Spring 数据库.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- MySQL data source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/personal" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>/orm/Song.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>

<bean id="songDao" class="com.letsdo.impl.SongImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- MUST have transaction manager, using aop and aspects -->
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>

orm/宋/hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.letsdo.model.Song" table="song" catalog="personal">
<id name="id" type="int">
<column name="id" length="45" />
<generator class="increment"/>
</id>
<property name="filePath" column="filepath"/>
<property name="fileName" column="filename"/>
<property name="album" column="album"/>
<property name="title" column="title"/>
<property name="size" column="size"/>
</class>
</hibernate-mapping>

松道.java

public interface SongDao {

public List<String> getAllAlbums();
}

SongImpl.java

@Service
@Transactional
public class SongImpl implements SongDao{

private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public List<String> getAllAlbums(){
List<String> allAlbums = new ArrayList<String>();
Query query = getSessionFactory().getCurrentSession().createQuery("Select DISTINCT Album from song");
allAlbums = query.list();
return allAlbums;

}
public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

}

Controller :HomeController.java

public class HomeController {

@Autowired private SongImpl songDao;
@RequestMapping(value="/", method=RequestMethod.GET)
public ModelAndView welcomePageBeforeLogin(HttpServletRequest request, HttpServletResponse response,HttpSession session){
ModelAndView model = new ModelAndView();
List<String> album = songDao.getAllAlbums();
model.addObject("albumsize",album.size());
model.setViewName("hello");
return model;
}
}

最佳答案

我想你最终会得到两个 SongImpl 类的 bean——一个在 xml 中定义(名为 songDao),另一个用 @Service 注释(默认命名为 songImpl)。后者没有 Autowiring 的 SessionFactory,这就是它为 null 的原因。

您可以在其中一个答案中找到更多信息 here .

If component scanning is enabled, spring will try to create a bean even though a bean of that class has already been defined in the spring config xml. However if the bean defined in the spring config file and the auto-discovered bean have the same name, spring will not create a new bean while it does component scanning.

解决方案是删除 bean 的 xml 版本(如果您不需要它),并在 SongImpl 中 Autowiring SessionFactory。

关于java - Spring 4 + Hibernate 4 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com