读书人

用Annotation调整 spring 和hibernate

发布时间: 2012-09-28 00:03:35 作者: rapoo

用Annotation整合 spring 和hibernate时出错,大侠看看!
model包下实体类:
@Entity
public class User {

private int id;
private String name;

@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

dao包下UserDAO:

@Component
public class UserDAO {

private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

public void save(User user){

this.getHibernateTemplate().save(user);

System.out.println("a user saved");
}
}

service包下UserService:

@Component("userService")
public class UserService {

private UserDAO userDao;

public UserDAO getUserDao() {
return userDao;
}

@Resource
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}

public void add(User user){
userDao.save(user);
}
}

bean.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">

<context:annotation-config />
<context:component-scan base-package="com.spring" />

<!-- more bean definitions go here -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/spring" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.spring.model.User
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

测试代码如下(用jUnit):
@Test
public void testAdd() throws Exception{
ApplicationContext act=new ClassPathXmlApplicationContext("bean.xml");


UserService service = (UserService) act.getBean("userService");
service.add(new User());
}

测试时出现如下错误:

No bean named 'userService' is defined

这是什么错误啊,明明是没错啊,大侠赐教!

[解决办法]
@Autowire
private UserSerivce userSerivce;
get...
set...

UserSerivce 这个类也需要定义成spring组件或者在配置文件中配置并加载配置文件



[解决办法]
楼主完全的用错了,你的UserDAO UserService都要写个接口,然后去实现接口
你的test类写为
UserService service = (UserService) act.getBean("userService");
hongse 的应该是接口
你的实习类应该是UserServiceImpl什么的,在UserServiceImpl注解
[解决办法]
测试中getBean的方法必须要在XML配置文件中定义userService的<bean/>节点,而你的配置文件里没有,用的是注解方式,所以报错。
[解决办法]
你这都写的什么跟什么嘛。要用注解就用注解,要用xml配置,就用xml配置。你这牛头不对马尾的。
你对spring基本的东西都不明白。

Java code
1、定义接口public interface UserService {    public void add(User user);}2、定义接口实现类@Component("userService")public class UserServiceImpl implements UserService {@AutoWiredprivate UserDAO userDao;//此处dao的定义跟service一样,需要dao接口和dao实现类public void add(User user){userDao.save(user);}}
[解决办法]
试过你的代码,没有出现楼主所说的错误,表示很费解,你把你的代码上传一下吧,我也很想知道原因
[解决办法]
探讨
你这都写的什么跟什么嘛。要用注解就用注解,要用xml配置,就用xml配置。你这牛头不对马尾的。
你对spring基本的东西都不明白。

Java code


1、定义接口
public interface UserService {
public void add(User user);
}
2、定义接口实现类
@Component("userService")
……

读书人网 >J2EE开发

热点推荐