具有Spring引导gradle的H2数据库NamedParameterJdbcTemplate / JdbcTemplate

时间:2017-05-01 02:54:34

标签: gradle spring-boot jdbctemplate

我正在尝试设置“H2”以查询非常简单的restful服务,我在底部收到错误:我不确定我在哪里犯错误。任何有助于解决此问题的帮助都会有所帮助。我没有使用Spring启动的经验。我正在尝试关注Spring Data access with JDBC

===================================

相关代码

@Service
public class DBServiesImplementation implements DBServices {

    @Autowired
    DBOperation dbo;

    public DBServiesImplementation(final DBOperation dbo){
        this.dbo=dbo;
    }  

    @Override
    public List<Messages> readMessage(String userName) {
        dbo.query("");
        return null;
    }
//.....
}

和控制器:

@RestController
public class Controller {


    @Autowired
    DBServices dbServices;

    @RequestMapping(value = "/api/message", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String, String>> getMessages(@AuthenticationPrincipal UserDetails currentUser) {


        List<Messages> list = dbServices.readMessage(currentUser.getUsername());
        list.add(new Messages("Hello", 1));
        Map<String, String> messageMap = retriveMessages();
        return new ResponseEntity<Map<String, String>>(messageMap,
                HttpStatus.OK);
    }

    @Configuration
@Repository
public class DBOperation implements IDBOperation {

    private NamedParameterJdbcTemplate jdbc;
    public DBOperation(final NamedParameterJdbcTemplate jdbc) {
        this.jdbc=jdbc;
    }


    @Transactional(readOnly = true)
    public List<Object> query(String query) {
        query = "SELECT * FROM person";
        List<Object> list = jdbc.query(query,(rs, rowNum) -> new User(rs.getInt("id"), rs.getString("name")));
        return list;
    }

的build.gradle:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile ("commons-dbcp:commons-dbcp")   
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

application.properties

spring.datasource.url=jdbc:h2:mem:challenge
spring.datasource.schema=classpath:/schema.sql
spring.datasource.data=classpath:/data.sql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver

日志::

    Exception encountered during context initialization - cancelling refresh attempt: org.spr
ingframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller': Unsatisfied dependency expressed through field 'dbServices'; nested exception is org.sp
ringframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBServiesImplementation' defined in file [/Users/a/dev/challenge/challenge/bin/org/ws/web
/db/services/DBServiesImplementation.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyExce
ption: Error creating bean with name 'DBOperation' defined in file [/Users/a/dev/challenge/challenge/bin/org/ws/web/db/DBOperation.class]: Unsatisfied dependency expressed through 
constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.namedparam.NamedParame
terJdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-04-30 19:21:13.571  WARN 37050 --- [  restartedMain] o.s.boot.SpringApplication               : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defi
ned in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springfra
mework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' av
ailable)
2017-04-30 19:21:13.683 ERROR 37050 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.ws.web.db.DBOperation required a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' that could not be found.
    - Bean method 'namedParameterJdbcTemplate' not loaded because @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans


Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' in your configuration.

0 个答案:

没有答案