当前位置: 代码迷 >> J2EE >> SSH——No bean named 'AssistantService' is defined。该如何处理
  详细解决方案

SSH——No bean named 'AssistantService' is defined。该如何处理

热度:86   发布时间:2016-04-17 23:16:58.0
SSH——No bean named 'AssistantService' is defined。。
报错
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;
}
}



------解决思路----------------------
没有找到实体类
------解决思路----------------------
你是不弄的有点混了  

<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注解注入业务方法
------解决思路----------------------
1、 
<bean id="AssistantService" class="service.AssistantServiceImpl">
<property name="AssistantDao">
<ref bean="AssistantDao" />
2、
@Autowired
private TeacherService teacherService; 

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

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



还是自己养成良好的习惯把 部署项目之前先clean一下 
------解决思路----------------------
不知道楼上的在说些什么
你是体里面声明:  private AssistantService assistantService; // 使用Spring注解注入业务方法
你要注入的是属性不是类名,也就是你需要注入的是 assistantService这个
而你配置文件里写的是类名:<property name="AssistantService"> 
所以把配置文件<property name="AssistantService">   这里的name改为 assistantService
这个问题肯定能解决
  相关解决方案