applicationContext.ml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>it.davidefruci.casadiscografica2.beans.Album</value>
<value>it.davidefruci.casadiscografica2.beans.Artist</value>
<value>it.davidefruci.casadiscografica2.beans.Song</value>
<value>it.davidefruci.casadiscografica2.beans.RecordCompany</value>
<value>it.davidefruci.casadiscografica2.beans.Category</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- DAO -->
<bean id="albumDao" class="it.davidefruci.casadiscografica2.daoImpl.AlbumDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="artistDao" class="it.davidefruci.casadiscografica2.daoImpl.ArtistDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="categoryDao" class="it.davidefruci.casadiscografica2.daoImpl.CategoryDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="recordCompanyDao" class="it.davidefruci.casadiscografica2.daoImpl.RecordCompanyDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="songDao" class="it.davidefruci.casadiscografica2.daoImpl.SongDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- BO -->
<bean id="albumBo" class="it.davidefruci.casadiscografica2.boImpl.AlbumBoImpl">
<property name="albumDao" ref="albumDao"></property>
</bean>
<bean id="artistBo" class="it.davidefruci.casadiscografica2.boImpl.ArtistBoImpl">
<property name="artistDao" ref="artistDao"></property>
</bean>
<bean id="categoryBo" class="it.davidefruci.casadiscografica2.boImpl.CategoryBoImpl">
<property name="categoryDao" ref="categoryDao"></property>
</bean>
<bean id="recordCompanyBo" class="it.davidefruci.casadiscografica2.boImpl.RecordCompanyBoImpl">
<property name="recordCompanyDao" ref="recordCompanyDao"></property>
</bean>
<bean id="songBo" class="it.davidefruci.casadiscografica2.boImpl.SongBoImpl">
<property name="songDao" ref="songDao"></property>
</bean>
AlbumDao 打包it.davidefruci.casadiscografica2.dao
public interface AlbumDao {
public void save(Album album) throws Exception;
public void delete(int id) throws Exception;
public void update(Album album) throws Exception;
public Album findById(int id) throws Exception;
public Album findByName(String name) throws Exception;
public List<Album> getAllAlbum() throws Exception;
}
AlbumDaoImpl 打包it.davidefruci.casadiscografica2.daoImpl;
public class AlbumDaoImpl implements AlbumDao {
@Autowired
@Qualifier("sessionFactory")
SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory factory) {
this.sessionFactory = factory;
}
@Override
public void save(Album album) throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(album);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
} else {
e.printStackTrace();
}
} finally {
session.close();
}
}
@Override
public void delete(int id) throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Album album = (Album) session.get(Album.class, id);
session.delete(album);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
} else {
e.printStackTrace();
}
} finally {
session.close();
}
}
@Override
public void update(Album album) throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(album);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
@Override
public Album findById(int id) throws Exception {
Session session = sessionFactory.openSession();
Album album = null;
try {
album = (Album) session.get(Album.class, id);
} catch (HibernateException e) {
throw e;
} finally {
session.close();
}
return album;
}
@Override
public Album findByName(String name) throws Exception {
Session session = sessionFactory.openSession();
Album album = null;
List<Album> list = new ArrayList<Album>();
try {
Query query = session.createQuery("FROM Album WHERE name= :name");
query.setParameter("name", name);
list = query.list();
if (list != null) {
if (list.size() == 1) {
album = list.get(0);
}
}
} catch (HibernateException e) {
throw e;
} finally {
session.close();
}
return album;
}
@Override
public List<Album> getAllAlbum() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
List<Album> list = null;
try {
list = session.createQuery("FROM Album").list();
} catch (HibernateException e) {
throw e;
} finally {
session.close();
}
return list;
}
}
Tomcat没有部署该应用程序。它说
失败 - 在上下文路径/ CasaDiscografica2部署应用程序但是 上下文无法启动
并告诉我错误:
org.springframework.beans.factory.BeanCreationException:创建名称为&#39; albumDao&#39;的bean时出错在ServletContext资源[/WEB-INF/applicationContext.xml]中定义:设置属性值时出错;嵌套异常是org.springframework.beans.NotWritablePropertyException:无效的属性&#39; sessionFactory&#39; bean类
和
org.springframework.beans.NotWritablePropertyException:无效的属性&#39; sessionFactory&#39; bean类[it.davidefruci.casadiscografica2.daoImpl.AlbumDaoImpl]:Bean属性&#39; sessionFactory&#39;不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?
为什么呢?感谢。
答案 0 :(得分:0)
如果您要自动装配SessionFactory
,则无需在上下文中明确注入它。
如果您希望自动装配工作,请不要忘记将<context:annotation-config />
添加到您的应用程序上下文中。