运行
ApplicationContext ctx = 
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");获取时候报错,javax.servlet.ServletException: No bean named 'customerService' is defined附代码:
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
   <param-name>contextConfigLocation</param-name> 
   <param-value>classpath*:/WEB-INF/applicationContext-service.xml,classpath*:/WEB-INF/applicationContext-hibernate.xml,classpath*:/WEB-INF/applicationContext.xml</param-value>
</context-param>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener></web-app>applicationContext-service.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="customerService" parent="txProxyTemplate">
<property name="target">
<bean class="com.test.service.impl.CustomerServiceImpl">
<property name="CustomerDAO" ref="CustomerDAO" />
</bean>
</property>
</bean>


</beans>applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:test">
</property>
<property name="username" value="test"></property>
<property name="password" value="test"></property>
</bean>


</beans>CustomerServiceImplpackage com.test.service.impl;import com.test.dao.CustomerDAO;
import com.test.hibernate.entity.Customer;
import com.test.service.CustomerService;public class CustomerServiceImpl implements CustomerService { private CustomerDAO dao;

public void addUser(Customer user) {
// TODO Auto-generated method stub
dao.save(user);
} public boolean checkLogin(String loginId, String pass) {
// TODO Auto-generated method stub
Customer cus=dao.getCustomerByLoginId(loginId);

if(cus!=null && cus.getPassword().equals(pass))
return true;

return false;
} public Customer getCustomer(Integer id) {
// TODO Auto-generated method stub
return dao.getCustomerById(id);
}}
Action/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.test.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.test.service.CustomerService;/** 
 * MyEclipse Struts
 * Creation date: 11-05-2009
 * 
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
 */
public class LoginAction extends DispatchAction {
/*
 * Generated Methods
 */ /** 
 * Method execute
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm loginForm = (DynaActionForm) form;// TODO Auto-generated method stub
String loginId = loginForm.getString("user");
String pass = loginForm.getString("password");


ApplicationContext ctx = 
WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
CustomerService svc = (CustomerService) ctx.getBean("customerService");
boolean login = svc.checkLogin(loginId, pass);


System.out.println(login==true?"true":"false");

return null;
}
}

解决方案 »

  1.   

    1、不知道你的servlet是什么变量,确认修改ApplicationContext ctx = 
    WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext.getServletContext()); 
    2、Spring容器修改一下启动方式试一试<context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>/WEB-INF/applicationContext.xml</param-value>  
      </context-param>  
      <servlet>  
       <servlet-name>springInitServlet</servlet-name>  
       <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  
       <load-on-startup>2</load-on-startup>  
      </servlet> 
    3、另外你的Spring配置文件怪怪的,为什么Service需要继承Transaction事务管理器
     <bean id="customerService" parent="txProxyTemplate"> ???
      

  2.   

    试试
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      

  3.   

    应该是服务启动的时候有问题..你看下tomcat正常启动了吗?
      

  4.   

    你的问题好像是 spring  无法找到那个 DAO 依赖注入的问题你在 ACTION 里 写set  get 方法了吗 
      

  5.   

    CustomerServiceImpl   你这个 是hibernate 实例?    怎么在这里写 dao 属性   继承 接口后该实例了啊? 你怎么还用dao 来写。。他上哪解决去啊而且 dao 通过依赖注入 要到ACTION 中   
      

  6.   

    applicationContext-service.xml文件没有加载吧,所以里面的bean没有被加载
      

  7.   

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
    </context-param>
    我这配置文件是在classpath下,你上面写的classpath下的WEB-INF??
      

  8.   

    LZ配置了:<bean id="customerService" parent="txProxyTemplate">
            <property name="target">
                <bean class="com.test.service.impl.CustomerServiceImpl">
                    <property name="CustomerDAO" ref="CustomerDAO" />
                </bean>
            </property>但是在impl中,却没见到有关于这个名字的属性的注入动作!而且依赖的属性也不是这个"CustomerDAO".所以,至少在这里,有些不懂楼主的写法了!不妨使用构造或是set方法注入一下,看看。