SSH No bean named AssistantService is defined。。

J2EE 码拜 9年前 (2015-04-02) 1783次浏览 0个评论

报错
org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”LoginAction”” defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean “”AssistantService”” while setting bean property “”AssistantService””; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named “”AssistantService”” is defined

代码如下
applicationContext.xml:
<bean id=”AssistantDao” class=”dao.impl.AssistantDaoImpl”>
<property name=”sessionFactory” ref=”sessionFactory”></property>
</bean>
<bean id=”AssistantService” class=”service.AssistantServiceImpl”>
<property name=”AssistantDao”>
<ref bean=”AssistantDao” />
</property>
</bean>
<bean id=”LoginAction” class=”action.LoginAction”>
<property name=”AssistantService”>
<ref bean=”AssistantService” />
</property>
</bean>

AssistantDaoImpl.java:
public class AssistantDaoImpl implements AssistantDao {
@Resource
private SessionFactory sessionFactory;
private static AssistantDaoImpl assistantDaoImpl  = new AssistantDaoImpl();

public static AssistantDaoImpl getInstance() {
// TODO Auto-generated method stub
return assistantDaoImpl;
}

public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}
public SessionFactory getSessionFactory(){
return sessionFactory;
}
}

AssistantServiceImpl.java:
@Service 
public class AssistantServiceImpl implements AssistantService{
private AssistantDao assistantDao;
public AssistantDao getAssistantDao() {
return assistantDao;
}
public void setAssistantDao(AssistantDao assistantDao) {
this.assistantDao = assistantDao;
}
}

LoginAction.java:
@Repository
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;

@Autowired
private StudentService studentService; // 使用Spring注解注入业务方法
@Autowired
private TeacherService teacherService; // 使用Spring注解注入业务方法
@Autowired
private PersonInChargeService personinchargeService; // 使用Spring注解注入业务方法

private AssistantService assistantService; // 使用Spring注解注入业务方法
public AssistantService getAssistantService() {
return assistantService;
}
public void setAssistantService(AssistantService assistantService) {
this.assistantService = assistantService;
}
}

SSH No bean named AssistantService is defined。。
5分
没有找到实体类
SSH No bean named AssistantService is defined。。
引用 1 楼 u011280732 的回复:

没有找到实体类

可不可以具体点?

SSH No bean named AssistantService is defined。。
15分
你是不弄的有点混了  

<bean id=”AssistantService” class=”service.AssistantServiceImpl”>
<property name=”AssistantDao”>
<ref bean=”AssistantDao” />

使用 bean 注入 了对象 就要使用 注解注入 @Autowired 
如果采用配置注入 你的 teacherService 属性就需要加get set 方法
如果采用下面注解注入 就把上面的配置删除  
  
但是dao还是需要 配置 bean  注入 sessionFactory   service 去掉  
如果你的dao是hibernate 自动生成的有对应的配置 那么在 service 引用的dao 不需要注解  也不需要get set

action  需要在你的struts 配置文件中配置 对应的 action  引用的对象是 你在spring配置的action id
@Autowired
private TeacherService teacherService; // 使用Spring注解注入业务方法

SSH No bean named AssistantService is defined。。
10分
1、 
<bean id=”AssistantService” class=”service.AssistantServiceImpl”>
<property name=”AssistantDao”>
<ref bean=”AssistantDao” />
2、
@Autowired
private TeacherService teacherService; 

 这两者都是对象的注入方法 只需要保留一种即可 另外 使用 第一种方式 在 类中注入的对象需要加入get set 方法

SSH No bean named AssistantService is defined。。
引用 4 楼 huangcan_liu 的回复:

1、 
<bean id=”AssistantService” class=”service.AssistantServiceImpl”>
<property name=”AssistantDao”>
<ref bean=”AssistantDao” />
2、
@Autowired
private TeacherService teacherService; 

 这两者都是对象的注入方法 只需要保留一种即可 另外 使用 第一种方式 在 类中注入的对象需要加入get set 方法

谢谢你回复了这么多~~
我把Assistant全部换成了Student,它依然报错No bean named “”AssistantService”” is defined,请问是不是与缓存有关?~~

SSH No bean named AssistantService is defined。。
搞好了,是build里的class文件保持不变造成的!手动修改build里的class文件就哦了!请问各位大牛,这是eclipse出问题了吗?
SSH No bean named AssistantService is defined。。
5分
引用 6 楼 wendylee1994 的回复:

搞好了,是build里的class文件保持不变造成的!手动修改build里的class文件就哦了!请问各位大牛,这是eclipse出问题了吗?

还是自己养成良好的习惯把 部署项目之前先clean一下 

SSH No bean named AssistantService is defined。。
5分
不知道楼上的在说些什么
你是体里面声明:  private AssistantService assistantService; // 使用Spring注解注入业务方法
你要注入的是属性不是类名,也就是你需要注入的是 assistantService这个
而你配置文件里写的是类名:<property name=”AssistantService”> 
所以把配置文件<property name=”AssistantService”>   这里的name改为 assistantService
这个问题肯定能解决

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SSH No bean named AssistantService is defined。。
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!