我有春天项目的三个xml文件,如下所示
应用context.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema
/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema
/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema
/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema
/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema
/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/indi" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
CController-servlet.xml(经理级别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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema
/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema
/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema
/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema
/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="project4"/>
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="frm4" class="project4.CController" >
<property name="userDAO" ref="myUserDAO" />
</bean>
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
和UUController-servlet.xml(这是一个dao级别的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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema
/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema
/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema
/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema
/context/spring-context-2.5.xsd">
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="project4"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myUserDAO" class="project4.UserDAOImpl1">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>
我有两个xml文件,一个用于管理器调用,另一个用于dao调用...我知道从一个xml点到另一个xml中的bean的ref链接。我认为它们会自动链接但是我收到以下错误
Error creating bean with name
'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0'
defined in ServletContext resource [/WEB-INF/CController-servlet.xml]: Initialization
of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'frm4' defined in ServletContext resource [/WEB-INF/CController-servlet.xml]: Cannot
resolve reference to bean 'myUserDAO' while setting bean property 'userDAO'; nested
exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'myUserDAO' is defined
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'frm4' defined in ServletContext resource [/WEB-INF/CController-
servlet.xml]: Cannot resolve reference to bean 'myUserDAO' while setting bean property
'userDAO'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
'myUserDAO' is defined
UserDAOImpl1是我的hibernate模板类,CCController是我的spring控制器类
CCController.java
package project4;
import project4.UserDAO1;
import project4.User1;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.validation.BindingResult;
@Controller
@RequestMapping("frm4.do")
public class CController{
private UserDAO1 userDAO;
public void setUserDAO(UserDAO1 userDAO) {
this.userDAO = userDAO;
}
@RequestMapping(params = "/add", method = RequestMethod.POST)
public ModelAndView add( @ModelAttribute("add") User1 user,HttpServletRequest
request,HttpServletResponse response) throws Exception {
userDAO.saveUser(user);
System.out.println("hai");
return new ModelAndView("redirect:list.htm");
}
@RequestMapping(params = "delete", method = RequestMethod.POST)
@Transactional
public ModelAndView delete(@ModelAttribute("delete") User1 user,HttpServletRequest
request,HttpServletResponse response) throws Exception {
userDAO.deleteUser(user);
return new ModelAndView("redirect:list.htm");
}
@RequestMapping(params = "find", method = RequestMethod.POST)
@Transactional
public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest
request,HttpServletResponse response) throws Exception {
userDAO.findUser(user);
return new ModelAndView("redirect:list.htm");
}
@RequestMapping(params = "update", method = RequestMethod.POST)
@Transactional
public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest
request,HttpServletResponse response) throws Exception {
userDAO.updateUser(user);
return new ModelAndView("redirect:list.htm");
}
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("userList", userDAO.listUser());
modelMap.addAttribute("user", new User1());
return new ModelAndView("userForm", modelMap);
}
}
有人可以帮助PLZ
编辑:
web.xml如下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>
<servlet>
<servlet-name>CController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
答案 0 :(得分:1)
frm4
bean有一个引用userDAO
的属性,该属性在其他xml文件中声明。因此,请确保在应用程序上下文中加载所有xml文件。为此,您需要在web.xml中添加所有xml文件。
试试这个:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/UUController-servlet.xml,
/WEB-INF/application-context.xml,
/WEB-INF/CController-servlet.xml
</param-value>
</context-param>
编辑:
如果这不适合你。尝试在application-context.xml文件中添加其他文件。使用bean导入标记。
<beans:import resource="/WEB-INF/UUController-servlet.xml"/>
<beans:import resource="/WEB-INF/CController-servlet.xml"/>
并在启动时仅加载应用程序上下文文件。
编辑:
错误“无法映射处理程序”背后的原因是您在应用程序启动时扫描组件,并且还在应用程序上下文中声明了相同的bean。
<context:component-scan base-package="project4"/>
&安培;
<bean name="frm4" class="project4.CController" >
<property name="userDAO" ref="myUserDAO" />
</bean>
从xml文件中删除bean声明,因为您已经扫描了该特定组件。并在您的控制器类中自动装配myUserDAO
bean。
@Autowire
@Qualifier("myUserDAO")
private UserDAOImpl1 myUserDAO;
答案 1 :(得分:0)
问题是应用程序上下文未加载UUController-servlet
文件。由于它是数据库级别的上下文,因此将其添加到contextConfigLocation
,如下所示。我还建议将其重命名为dao-context.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
/WEB-INF/UUController-servlet.xml
</param-value>
</context-param>