当前位置: 首页 > 知识库问答 >
问题:

无法自动连接字段:私有组织。springframework。jdbc。果心使用

邓正谊
2023-03-14
00:49:34.284 [localhost-startStop-1] ERROR o.s.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'puzzleDAOImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate com.mrhunt.dao.PuzzleDAOImp.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    ... 29 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    ... 31 common frames omitted

和我的web.xml(src/main/webapp/WEB-INF/web.xml)

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>MR.Hunt</display-name>

   <servlet>
      <servlet-name>main</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>main</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

和applicationContext。xml(src/main/webapp/WEB-INF/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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-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/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.mrhunt.web" />
    <context:component-scan base-package="com.mrhunt.model" />
    <context:component-scan base-package="com.mrhunt.dao"/>
    <context:component-scan base-package="com.mrhunt.service"/>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClass">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
    </bean>

    <!-- 配置Jdbc模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />


    <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp" />

</beans>

最后是. java文件:Puzzle.java(src/main/java/com/mrhunt/Model/Puzzle.java)

package com.mrhunt.model;
import javax.persistence.*;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;
import java.sql.*;

@Entity
@Table(name = "puzzles")
public class Puzzle {
    public Puzzle() {
        this.pz_id = 10;
        this.pz_name = "sin";
    }

    @Column(name = "pz_id")
    private Integer pz_id;

    @Column(name = "pz_group")
    private String pz_group;

    @Column(name = "pz_name")
    private String pz_name;

    @Column(name = "pz_difficulty")
    private Integer pz_difficulty;

    @Column(name = "pz_description")
    private String pz_description;

    @Column(name = "pz_input")
    private String pz_input;

    @Column(name = "pz_output")
    private String pz_output;

    @Column(name = "pz_source")
    private String pz_source;

    @Column(name = "pz_createdate")
    @Type(type = "java.sql.Date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date pz_createdate;

    public Integer getPz_id() {
        return pz_id;
    }
    public void setPz_id(Integer pz_id) {
        this.pz_id = pz_id;
    }
    /*And other getter/setter*/
}

java(src/main/java/com/mrhunt/ao)

package com.mrhunt.dao;
import java.util.*;
import com.mrhunt.model.Puzzle;
public interface PuzzleDAO {
    public Puzzle getPuzzleByID(int pz_id);
    public Map<String, String> getFunctionsByID(int pz_id);
    public List<String> getAllGroups();
    public Map<Integer, String> getAllPuzzles();
    public Map<Integer, String> getPuzzlesByGroup(String pz_group);
}

PuzzleDAOImp.java(src/main/java/com/mrhunt/ao/PuzzleDAOImp.java)

package com.mrhunt.dao;

import java.sql.*;
import java.util.*;

import org.springframework.beans.factory.annotation.*;
import org.springframework.jdbc.core.*;
import org.springframework.stereotype.*;

import com.mrhunt.model.Puzzle;

@Repository
public class PuzzleDAOImp implements PuzzleDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    /*functions are implemented here*/
}

抱歉我的问题太长了。。。如何修复这个错误?

共有1个答案

安轶
2023-03-14

添加

 类似资料: