Java 无法在Spring MVC中使用控制器自动连接Dao
****大家好,我正在尝试用SpringMVC创建简单的登录应用程序。在运行我的应用程序时,我遇到以下异常,即无法自动连接Dao****Java 无法在Spring MVC中使用控制器自动连接Dao,java,Java,****大家好,我正在尝试用SpringMVC创建简单的登录应用程序。在运行我的应用程序时,我遇到以下异常,即无法自动连接Dao**** org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springf
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.dao.loginDao com.controller.HelloController.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' defined in ServletContext resource [/WEB-INF/spring-web-servlet.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframeenter code herework/dao/DataAccessException
下面是我的控制器类
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.beans.Customer;
import com.dao.loginDao;;
@Controller
public class HelloController {
@Autowired
private loginDao loginDao;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
// System.out.println("***********8888 controller *************************");
model.addAttribute("message", "Hi you are successfully logged in");
return "hello";
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String varifyLogin(ModelMap model , @RequestParam String userId , @RequestParam String password) {
Customer customer = loginDao.login(userId, password);
System.out.println("***********8888 controller *************************");
model.addAttribute("message", "welcome to Spring MVC");
return "welcome";
}
}
请参见下面的Dao类:-
package com.dao;
import com.beans.*;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class loginDao {
JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;}
public Customer login(String userId, String password){
String sql="select * from Customer where CUSTOMER_ID=?";
Customer customer = template.queryForObject(sql, new Object[]{userId},new BeanPropertyRowMapper<Customer>(Customer.class));
if(customer != null && customer.getPassword().equals(password))
{
return customer;
}
return null;
//template.
}
}
package com.dao;
导入com.beans。*;
导入org.springframework.jdbc.core.beanpropertyrowapper;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.stereotype.Component;
@组成部分
公共类登录{
jdbc模板;
公共void setTemplate(JdbcTemplate模板){
this.template=template;}
公共客户登录(字符串用户ID、字符串密码){
String sql=“选择*来自客户,其中客户_ID=?”;
Customer Customer=template.queryForObject(sql,新对象[]{userId},新BeanPropertyRowMapper(Customer.class));
if(customer!=null&&customer.getPassword().equals(password))
{
退货客户;
}
返回null;
//模板。
}
}
下面是我的spring.xml,其中我提到了bean定义:-
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.controller" />
<context:component-scan base-package="com.dao"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="byName">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"></property>
<property name="username" value="SHAKTI_ELE"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="dao" class="com.dao.loginDao">
<property name="template" ref="jt"></property>
</bean>
</beans>
/
.jsp
将限定符与自动连线一起使用:-
@Autowired
@Qualifier("dao")
private loginDao loginDao;
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。和:请参阅:此网站的几乎每个链接:。总之,请回答你的问题。发布一些代码,例如类中无法自动连接的代码。添加一些信息,目前我们只有一个错误日志。如果你不帮助我们了解你的问题,我们就帮不了你。这对我不起作用。Vishal,还有其他建议吗?请把问题发到哪里?