stenley123
stenley123
2011-05-04 10:19

Spring报错:expected at least 1 matching bean还是没解决

已采纳

严重: Exception sending context initialized event to listener instance of class cn.tg.core.web.ApplicationListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemConfigMngImpl': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.tg.core.manager.impl.SystemConfigMngImpl.setDao(cn.tg.core.dao.SystemConfigDao); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [cn.tg.core.dao.SystemConfigDao] is defined: Unsatisfied dependency of type [interface cn.tg.core.dao.SystemConfigDao]: expected at least 1 matching bean

实体类
SystemConfig

Java代码

@Entity

@Table(name = "tg_system")

public class SystemConfig implements Serializable {

private static final long serialVersionUID = 2776461377886734127L;

private long id;

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

public long getId() { return id;

}

……

@Entity
@Table(name = "tg_system")
public class SystemConfig implements Serializable {
private static final long serialVersionUID = 2776461377886734127L;
private long id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
……

Mng接口

Java代码

1.public interface SystemConfigMng extends TGCoreManager{

2.}

public interface SystemConfigMng extends TGCoreManager{
}
Mng实现类
@Service
// systemConfigMngImpl
@Transactional
public class SystemConfigMngImpl extends TGCoreManagerImpl
implements SystemConfigMng {
@Autowired
public void setDao(SystemConfigDao dao) {
super.setDao(dao);
}

public SystemConfigDao getDao() {
return (SystemConfigDao) super.getDao();
}
}

在一个servelt里:

Java代码

1.public final class ApplicationListener implements ServletContextListener {

2. private static final String BEAN_NAME = "systemConfigMngImpl";

3. private static final Logger log = LoggerFactory

4. .getLogger(ApplicationListener.class);

5.

6. public void contextDestroyed(ServletContextEvent event) {

7. }

8.

9. public void contextInitialized(ServletContextEvent event) {

10. WebApplicationContext wac = WebApplicationContextUtils

11. .getRequiredWebApplicationContext(event.getServletContext());

12. systemConfigMng = (SystemConfigMng) wac.getBean(BEAN_NAME, SystemConfigMng.class);

13. log.info("系统启动,读取所有站点信息到缓存。");

14. systemConfigMng.loadAllSystemConfigToCache();

15. }

16.

17. private SystemConfigMng systemConfigMng;

18.}

public final class ApplicationListener implements ServletContextListener {
private static final String BEAN_NAME = "systemConfigMngImpl";
private static final Logger log = LoggerFactory
.getLogger(ApplicationListener.class);

public void contextDestroyed(ServletContextEvent event) {
}

public void contextInitialized(ServletContextEvent event) {
    WebApplicationContext wac = WebApplicationContextUtils
            .getRequiredWebApplicationContext(event.getServletContext());
    systemConfigMng = (SystemConfigMng) wac.getBean(BEAN_NAME, SystemConfigMng.class);
    log.info("系统启动,读取所有站点信息到缓存。");
    systemConfigMng.loadAllSystemConfigToCache();
}

private SystemConfigMng systemConfigMng;

}

用了systemConfigMngImpl就报上面的错,那位大侠能帮忙解决吗?现在那个热锅上的蚂蚁啊!!只有那么多分了……万分感谢!!!

Java代码

1.public interface BaseDao {…//底层dao接口…}

2.

3.@Repository

4.public abstract class BaseDaoImpl implements BaseDao {

5. protected Logger log = LoggerFactory.getLogger(getClass());

6.

7. protected SessionFactory sessionFactory;

8.

9. @Autowired

10. public void setSessionFactory(SessionFactory sessionFactory) {

11. this.sessionFactory = sessionFactory;

12. }

13.…//底层dao实现类…

14.}

15.public interface BaseManager {//底层

16.Mng接口}

17.

18.@Transactional

19.public class BaseManagerImpl implements BaseManager {

20. protected Logger log = LoggerFactory.getLogger(getClass());

21. private BaseDao dao;

22.

23. public void setDao(BaseDao dao) {

24. this.dao = dao;

25. }

26.

27. protected BaseDao getDao() {

28. return this.dao;

29. }

30.//底层Mng实现类

31.}

32.

33.public interface TGCoreDao extends BaseDao {

34.

35.}

36.

37.public class TGCoreDaoImpl extends BaseDaoImpl

38. implements TGCoreDao {

39.

40.

41.}

42.

43.public interface TGCoreManager extends BaseManager {

44.}

45.

46.public class TGCoreManagerImpl extends

47. BaseManagerImpl implements TGCoreManager {

48.}

public interface BaseDao {…//底层dao接口…}

@Repository
public abstract class BaseDaoImpl implements BaseDao {
protected Logger log = LoggerFactory.getLogger(getClass());

protected SessionFactory sessionFactory;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

…//底层dao实现类…
}
public interface BaseManager {//底层
Mng接口}

@Transactional
public class BaseManagerImpl implements BaseManager {
protected Logger log = LoggerFactory.getLogger(getClass());
private BaseDao dao;

public void setDao(BaseDao<T> dao) {
    this.dao = dao;
}

protected BaseDao<T> getDao() {
    return this.dao;
}

//底层Mng实现类
}

public interface TGCoreDao extends BaseDao {

}

public class TGCoreDaoImpl extends BaseDaoImpl
implements TGCoreDao {

}

public interface TGCoreManager extends BaseManager {
}

public class TGCoreManagerImpl extends
BaseManagerImpl implements TGCoreManager {
}

Dao接口

Java代码

1.public interface SystemConfigDao extends TGCoreDao {

2.

3.}

public interface SystemConfigDao extends TGCoreDao {

}

Dao实现类

Java代码

1.public class SystemConfigDaoImpl extends TGCoreDaoImpl implements SystemConfigDao{

2.

3.}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • finallygo finallygo 10年前

    public class SystemConfigDaoImpl 上加上@Repository

    点赞 评论 复制链接分享
  • weixin_42557778 weixin_42557778 10年前

    config文件咩有定义相对应的bean
    还有你的bean需要加上@Service或者Repository

    点赞 评论 复制链接分享
  • mavlarn mavlarn 10年前

    说你的systemConfigMngImpl里面的dao没有定义相应的bean。

    点赞 评论 复制链接分享
  • hgj1024454508 敬敬11 10年前

    public class SystemConfigMngImpl extends TGCoreManagerImpl
    implements SystemConfigMng {
    @Autowired
    public void setDao(SystemConfigDao dao) {
    super.setDao(dao);
    }

    public SystemConfigDao getDao() {
    return (SystemConfigDao) super.getDao();
    }
    }

    这是你的代码吧,里面应该写注入的对象吧
    private SystemConfigDao dao;这一行之后才能get,set呀

    点赞 评论 复制链接分享

相关推荐