用Annotation整合 spring 和hibernate时出错,大侠看看!

gitlee 2012-04-12 09:35:42
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

这是什么错误啊,明明是没错啊,大侠赐教!
...全文
132 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
昨日凡阳 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

谢谢大家的回答,我已经知道哪里有问题了,其实很简单,base-package="com.spring" 这个地方我是拷贝另一处的,但我现在的包不是com.spring,把这里改成现有的包名就对了,,呵呵,,其实不写接口也是可以的
[/Quote]

要不要接口,這就是設計模式的選擇問題了。
gitlee 2012-04-16
  • 打赏
  • 举报
回复
谢谢大家的回答,我已经知道哪里有问题了,其实很简单,base-package="com.spring" 这个地方我是拷贝另一处的,但我现在的包不是com.spring,把这里改成现有的包名就对了,,呵呵,,其实不写接口也是可以的
昨日凡阳 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

楼主的问题是UserDao上没有加名称,导致spring在寻找dao时,userDao不唯一。

我把所有代码重写了一遍同,楼主作个参考吧。

实体类:
Java code

package com.annotation.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;……
[/Quote]

1、你不要接口实现类,你如何扩展?
一般都会写成如下:
@Service("userService")
public class UserServiceImpl implements UserService
2、既然用了注解注入,就没有必要再用setter方法注入。
private UserDao userDao;

public UserDao getUserDao() {
return userDao;
}

@Resource(name="u")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

可以改为:
@AutoWired
private UserDao userDao;
以为一般一个dao就一个实现类,所以默认按类别装配bean,是不会出问题。
3、
service层,最好用@Service("userService")而非@Component("userService"),这样看起来一目了然。
walkman_22 2012-04-15
  • 打赏
  • 举报
回复
楼主的问题是UserDao上没有加名称,导致spring在寻找dao时,userDao不唯一。

我把所有代码重写了一遍同,楼主作个参考吧。

实体类:

package com.annotation.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@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;
}

}


UserDao(楼主的代码出问题的地方),为了测试方便,hibernate的save()给注释掉了。

package com.annotation.dao;

import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.annotation.entity.User;

@Component("u")
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("user saved");
}

}



UserService(请楼主注意,setUserDao方法哪里,与UserDao的Component name是一致的,楼主的报错应该就是由于少加了这个。

package com.annotation.service;

import javax.annotation.Resource;
//import org.springframework.stereotype.Component;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.annotation.dao.UserDao;
import com.annotation.entity.User;

@Component("userService")
public class UserService {
private UserDao userDao;

public UserDao getUserDao() {
return userDao;
}

@Resource(name="u")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

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

}



beans.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.annotation" />

<!-- 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="root" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.annotation.entity.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,Junit里面加注解好像不行,我试过的,实际上也没有必要)


package com.annotation.dao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.annotation.entity.User;
import com.annotation.service.UserService;


public class UserDaoTest {

@Test
public void testSave() {
ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) act.getBean("userService");
service.add(new User());

}
}

walkman_22 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
你这都写的什么跟什么嘛。要用注解就用注解,要用xml配置,就用xml配置。你这牛头不对马尾的。
你对spring基本的东西都不明白。

Java code


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

不定义接口也是可以的。
llbupt 2012-04-15
  • 打赏
  • 举报
回复
试过你的代码,没有出现楼主所说的错误,表示很费解,你把你的代码上传一下吧,我也很想知道原因
gitlee 2012-04-14
  • 打赏
  • 举报
回复
UserDAO:
public interface UserDAO {

public void save(User user);
}

UserDAOImpl:

@Component("userDao")
public class UserDAOImpl implements 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);
}
}

UserService:

@Component("userService")
public class UserService {

private UserDAO userDao;

public UserDAO getUserDao() {
return userDao;
}

@Resource(name="userDao")
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public void add(User user){
userDao.save(user);
}
}

test:

@Autowired
private UserService userService;

public UserService getUserService() {
return userService;
}

public void setUserService(UserService userService) {
this.userService = userService;
}

@Test
public void testAdd() {

userService.add(new User());
}

你们说的我都试了,还是那个错误:NullPointer 或 userService not defined ,前辈们请说详细点,我是自学
gitlee 2012-04-14
  • 打赏
  • 举报
回复



gitlee 2012-04-14
  • 打赏
  • 举报
回复
bean.xml里面的内容都没错吧,全用注解的,hibernateTemplate还是在xml里面定义的吧。
@Test
public void testAdd() throws Exception{
//全用注解配置吧
@AutoWired
private UserService userService;
service.add(new User());
}

你们方法试了,还是不行,接口也写了,就是不行啊
昨日凡阳 2012-04-12
  • 打赏
  • 举报
回复
你这都写的什么跟什么嘛。要用注解就用注解,要用xml配置,就用xml配置。你这牛头不对马尾的。
你对spring基本的东西都不明白。

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

@AutoWired
private UserDAO userDao;//此处dao的定义跟service一样,需要dao接口和dao实现类

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


1、
@Test
public void testAdd() throws Exception{
//全用注解配置吧
@AutoWired
private UserService userService;
service.add(new User());
}
阿甘1976 2012-04-12
  • 打赏
  • 举报
回复
测试中getBean的方法必须要在XML配置文件中定义userService的<bean/>节点,而你的配置文件里没有,用的是注解方式,所以报错。
宏Lee 2012-04-12
  • 打赏
  • 举报
回复
楼主完全的用错了,你的UserDAO UserService都要写个接口,然后去实现接口
你的test类写为
UserService service = (UserService) act.getBean("userService");
hongse 的应该是接口
你的实习类应该是UserServiceImpl什么的,在UserServiceImpl注解
macower 2012-04-12
  • 打赏
  • 举报
回复
@Autowire
private UserSerivce userSerivce;
get...
set...

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


67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧