【问题标题】:Springboot JdbcTemplate Autowired failedSpringboot JdbcTemplate Autowired 失败
【发布时间】:2019-01-10 21:38:51
【问题描述】:

我正在尝试使用 springboot 访问数据库,但是 spring 应用程序在下面抛出异常。

创建名为“welcomeController”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.springframework.jdbc.core.JdbcTemplate com.mysite.soLexiconWebSpring.jsp.WelcomeController.dataSource;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: 预期至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我认为主要是

org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有找到符合条件的 [org.springframework.jdbc.core.JdbcTemplate] 类型的依赖项

表示没有从应用程序配置文件创建 bean。 但是我用谷歌搜索了一段时间并没有运气。谁能告诉我如何正确注入 JdbcTemplate?

这是我的 POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <!-- Your own application should inherit from spring-boot-starter-parent -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.2.RELEASE</version>
    </parent>
    <artifactId>soLexiconWebSpring</artifactId>
    <groupId>com.mysite</groupId>
    <packaging>war</packaging>
    <name>Spring Boot Web JSP Sample</name>
    <description>Spring Boot Web JSP Sample</description>
    <version>0.0.1-SNAPSHOT</version>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
        <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Application.properties

spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
application.message=Hello SuperLucky

#spring.datasource basic database parameter
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/so_lexicon
spring.datasource.username=xxx
spring.datasource.password=xxx
#set data pooling provider to org.apache.tomcat
spring.datasource.type = org.apache.tomcat.jdbc.pool.DataSource
#tomcat datasource settings
spring.datasource.tomcat.initial-size=20
spring.datasource.tomcat.max-wait=2000
spring.datasource.tomcat.max-active=100
spring.datasource.tomcat.max-idle=16
spring.datasource.tomcat.min-idle=4
spring.datasource.tomcat.test-on-connect=true
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.test-on-return=true

应用

package com.mysite.soLexiconWebSpring.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleWebJspApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SampleWebJspApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebJspApplication.class, args);
    }
}

控制器

package com.mysite.soLexiconWebSpring.jsp;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mysite.soLexiconWebSpring.jsp.beans.FullLexiconRowBean;
import com.mysite.soLexiconWebSpring.jsp.beans.LoginTokenBean;
import com.mysite.soLexiconWebSpring.jsp.dao.LexiconDaoImpl;
import com.mysite.soLexiconWebSpring.jsp.utils.DBUtils;

@Controller
public class WelcomeController {

    @Value("${application.message:Hello World}")
    private String message = "Hello World";

   @Autowired
   private JdbcTemplate dataSource;

    @Autowired
   private DBUtils dbUtils;

    @RequestMapping("/soLexiconWebSpring")
    public String welcome(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", this.message);
        return "welcome";
    }

    @RequestMapping("/soLexiconWebSpring/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map<String, Object> model)
    {
       LoginTokenBean loginToken = new LoginTokenBean();
       loginToken.setUsername(username);
       loginToken.setPassword(password);
       model.put("loginToken", loginToken);


       LexiconDaoImpl lexRowDao = new LexiconDaoImpl(dataSource);
       List<FullLexiconRowBean> result = lexRowDao.getLexiconRow(1, 5);
       if(result != null) model.put("result", result);
       else model.put("result", dbUtils.getLastException().toString());

       dbUtils.setLastException(new Exception("A B C"));
       model.put("dbUtils", dbUtils);

       return "lexicon";
    }

}

【问题讨论】:

  • 提供的代码编译得很好。检查异常中提到的 com.mysite.soLexiconWebSpring.jsp.(上面没有显示)。
  • 你为什么不用@SpringBootApplication 而不是@Configuration @EnableAutoConfiguration @ComponentScan ??
  • @mrkernelpanic 它来自一个 maven 自动生成的项目,该项目使用尚未实现该注释的旧版本 springboot。
  • @mp31415 我所有的 Java 类都在那个包中。能否提供您的测试代码供参考?
  • 您的 pom 中的 spring-boot-starter-parent 工件的版本为 1.0.2.RELEASE。这是2014年的你是认真的吗??为什么不直接使用最新版本 2.0.4.RELEASE?

标签: spring spring-boot jdbctemplate


【解决方案1】:

感谢@mrkemelpanic,我将我的项目更新为 springboot 2.0.4(付出了一些努力),它终于可以工作了。所以它可能是一个 1.0.2 错误或什么的。 谢谢。

【讨论】:

    猜你喜欢
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多