先说下项目的结构:
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: |
|
File: | org/springframework/beans/factory/support/AbstractBeanFactory.java |
Line number: | 378 |
我尝试把services的注解从cityService 改为CityServiceImpl 后又报错:
Struts has detected an unhandled exception:
Messages: |
|
File: | org/springframework/beans/factory/support/DefaultListableBeanFactory.java |
Line number: |
请大神帮忙看看,到底是那个地方的注解出了问题,继承IbaseService这里搞了好久了。。
注入的地方写错了