代码之家  ›  专栏  ›  技术社区  ›  SuperLucky

Springboot JdbcTemplate自动连线失败

  •  0
  • SuperLucky  · 技术社区  · 6 年前

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

    错误创建bean的名称“Welcom控件”:嵌套异常是Or.SpReFraskWork.BeANS.Fas.BeNeCurcExcExc:不能自动字段:私有Or.SrpFraskWork.jdc.Cy.jdcTimeCm .MyStielSoxLimeWebSpRIP.jSP.WeloCeMeCopLLR.DATSURCE;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[org.springframework.jdbc.core.JdbcTemplate]类型的限定bean:应至少有1个bean符合此依赖项的自动连线候选。依赖项批注:{@org.springframework.beans.factory.annotation.Autowired(必需=true)}

    我认为主要是

    org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[org.springframework.jdbc.core.JdbcTemplate]的限定bean

    这表示没有从应用程序配置文件创建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>
    

    应用程序.属性

    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";
        }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   SuperLucky    6 年前

    感谢@kemelpanic先生,我把我的项目更新到了springboot 2.0.4(做了一些努力),终于成功了。所以它可能是一个1.0.2的bug或者其他什么东西。 谢谢。