首页新闻找找看学习计划

请教java大神 SSH框架整合 依赖泛型注入 BaseService配置出错

0
悬赏园豆:10 [已关闭问题] 关闭于 2016-09-27 15:21

先说下项目的结构:

 

action    //Structs功能

   city functions.......

service

  IBaseService

  ICityService

service.impl

  BaseServiceImpl

  CitySdeviceImpl

dao

  IBaseDao

  ICityDao

dao.impl

  BaseDaoImpl

  CityDaoImpl

相信大神把上面的目录大家一看,基本上已经非常清楚各个个目当的功能

 

本次配置主要是基于注解的方式实现,先简单的配置(action,service,dao),没有报错,然后再在简单的结构上增加接口层才出现配置错误。

具体的代码如下:

action:

package com.ssh.action;

import javax.annotation.Resource;
import javax.persistence.Id;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.City;
import com.ssh.entity.User;
import com.ssh.service.impl.CityServiceImpl;
import com.ssh.service.impl.UserService;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Component("userAction")
@Namespace("/u")
@ParentPackage("struts-default")
@Scope("prototype")
@Results(@Result(name = "succeed", location = "/pages/succeed.jsp"))
public class UserAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    //@Resource
    //private UserService userService;
    @Resource
    private CityServiceImpl cityService;

    private User user;

    public void setUser(User user) {
        this.user = user;
    }
  //测试代码
    @Action(value = "addUser")
    public String addUser() {
        // userService.addUser(user);
        City city = new City();
        city.setCityName("uuuu");
        city.setCityZip("jjjjj");

        cityService.insert(city);
        return "succeed";
    }

}

IBaseService

package com.ssh.service;

import java.util.List;

public interface IBaseService<T> {
    boolean insert(T t);
    
    boolean deleteById(Object id);
    boolean deleteByObj(T t);
    
    boolean update(T t);
    
    List<T> getAall();
}

ICityService

 package com.ssh.service; import com.ssh.entity.City; public interface ICityService extends IBaseService<City> { } 

BaseServiceImpl:

package com.ssh.service.impl;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ssh.dao.IBaseDao;
import com.ssh.dao.ICityDao;
import com.ssh.service.IBaseService;


public class BaseServiceImpl<T extends Serializable> implements IBaseService<T> {



    private IBaseDao<T> iBaseDao;
    
    public void setiBaseDao(IBaseDao<T> iBaseDao) {
        this.iBaseDao = iBaseDao;
    }



    @Override
    public boolean insert(T t) {
        System.out.println(t);
        iBaseDao.insert(t);
        return false;
    }

    @Override
    public boolean deleteById(Object id) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean deleteByObj(T t) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean update(T t) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public List<T> getAall() {
        // TODO Auto-generated method stub
        return null;
    }

}

CityServicesImpl:

package com.ssh.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ssh.dao.ICityDao;
import com.ssh.entity.City;
import com.ssh.service.ICityService;


@Service("cityService")
@Transactional
public class CityServiceImpl extends BaseServiceImpl<City> implements ICityService {
    
    @Resource
    private ICityDao cityDao;
    
    
    public void setCityDao(ICityDao cityDao) {
        
        super.setiBaseDao(cityDao); 
        this.cityDao = cityDao;
        
    }

    
}

IBaseDao:

package com.ssh.dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

public interface IBaseDao<T extends Serializable> {
    
    boolean insert(T t);
    
    boolean deleteById(Object id);
    boolean deleteByObj(T t);
    
    boolean update(T t);
    
    List<T> getAall();
}

ICityDao

package com.ssh.dao;
import org.springframework.stereotype.Repository;

import com.ssh.entity.*;


public interface ICityDao extends IBaseDao<City> {
    
}

BaseDaoImpl:

package com.ssh.dao.impl;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.ssh.dao.IBaseDao;

@Repository("baseDao")
public class BaseDaoImpl<T extends Serializable>  implements IBaseDao<T>  {
    
    @Resource
    protected SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    
    public Session getSession() {
        return this.sessionFactory.getCurrentSession();
    }
    
    public  BaseDaoImpl() {
        
    }
    
    public BaseDaoImpl(SessionFactory sessionFactory)
    {
        
    }

    
    @Override
    public boolean insert(T t) {
        getSession().save(t);
        return true;
    }

    @Override
    public boolean deleteById(Object key) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean deleteByObj(T t) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean update(T t) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public List<T> getAall() {
        // TODO Auto-generated method stub
        return null;
    }
    
}

CityDaoImpl:

package com.ssh.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.sql.Insert;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ssh.dao.ICityDao;
import com.ssh.entity.*;

@Repository("cityDao")
public class CityDaoImpl extends BaseDaoImpl<City> implements ICityDao{
    
    
    public CityDaoImpl(){
        
    }
    
    public void testSessionSave(City city)
    {
        getSession().save(city);
        
    }
    
}

到此,代码贴完了。

下面贴一下报的错误信息:

Error:


Struts Problem Report


Struts has detected an unhandled exception:


Messages:
  1. Bean named 'cityService' must be of type [com.ssh.service.impl.CityServiceImpl], but was actually of type [com.sun.proxy.$Proxy43]
  2. Error creating bean with name 'com.ssh.action.UserAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'cityService' must be of type [com.ssh.service.impl.CityServiceImpl], but was actually of type [com.sun.proxy.$Proxy43]
  3. Unable to instantiate Action, com.ssh.action.UserAction, defined for 'addUser' in namespace '/u'Error creating bean with name 'com.ssh.action.UserAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'cityService' must be of type [com.ssh.service.impl.CityServiceImpl], but was actually of type [com.sun.proxy.$Proxy43]
File: org/springframework/beans/factory/support/AbstractBeanFactory.java
Line number: 378
 

 

我尝试把services的注解从cityService 改为CityServiceImpl 后又报错:

Struts has detected an unhandled exception:

Messages:
  1. No qualifying bean of type [com.ssh.service.impl.CityServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
  2. Error creating bean with name 'com.ssh.action.UserAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ssh.service.impl.CityServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
  3. Unable to instantiate Action, com.ssh.action.UserAction, defined for 'addUser' in namespace '/u'Error creating bean with name 'com.ssh.action.UserAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ssh.service.impl.CityServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
File: org/springframework/beans/factory/support/DefaultListableBeanFactory.java
Line number:  

 

请大神帮忙看看,到底是那个地方的注解出了问题,继承IbaseService这里搞了好久了。。

beggar_的主页 beggar_ | 初学一级 | 园豆:18
提问于:2016-05-30 15:01
< >
分享
所有回答(1)
0

注入的地方写错了

beggar_ | 园豆:18 (初学一级) | 2016-09-01 17:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册