2016-12-04 186 views
0

當自動裝配數據源時,項目會出現錯誤,當我刪除數據源時,項目工作正常。我在春天添加數據源豆,我不知道是哪裏不對 我把我的項目上git的地址是:找不到符合[javax.sql.DataSource]類型的符合條件的bean [javax.sql.DataSource]

[email protected]:驪威綠色/ springMVC.git 的pom.xml

<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/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.spring</groupId> 
<artifactId>springMVC</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>springMVC Maven Webapp</name> 
<url>http://maven.apache.org</url> 
<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>3.0-alpha-1</version> 
    </dependency> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.30</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>4.1.7.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>4.3.1.RELEASE</version> 
    </dependency> 

</dependencies> 
<build> 
    <finalName>springMVC</finalName> 
</build> 
</project> 

彈簧

<?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:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context- 4.1.xsd"> 
<!-- 加載配置文件 --> 
<!-- 掃描service自動注入爲bean --> 
<context:component-scan base-package="com.springdemo" /> 
<context:property-placeholder location="jdbc.properties" /> 
<context:annotation-config /> 

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

的DemoController.java

package com.springdemo.controller; 

import java.io.UnsupportedEncodingException; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.sql.DataSource; 

import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

import com.springdemo.entity.User; 
import com.springdemo.service.UserService; 

@Controller 
public class DemoController { 
@Autowired 
private UserService userService; 
/*加上 
@Autowired 
private DataSource dataSource 
編譯報錯,沒有這句編譯正常*/ 
@Autowired 
private DataSource dataSource; 

@RequestMapping("login.do") 
public ModelAndView index(HttpServletRequest request, 
     HttpServletResponse response) throws UnsupportedEncodingException { 
    request.setCharacterEncoding("UTF-8"); 
    System.out.println(userService); 
    System.out.println(dataSource); 
    String userName=request.getParameter("userName"); 

    String userAge=request.getParameter("userAge"); 

    User user= userService.findById(3); 
    if(userName.equals(user.getName())){ 
     return new ModelAndView("sys/success","user",user); 
    }else{ 
     return new ModelAndView("sys/faild","user",user); 
    } 





} 

} 

jdbc.properties

jdbc.driver = com.mysql.jdbc.Driver 
jdbc.url=mysql://localhost:3306/maven?useUnicode=true&amp;characterEncoding=UTF-8 
jdbc.username = root 
jdbc.password = 123456 

#hibernate config 
hibernate.dialect = org.hibernate.dialect.MySQLDialect 
hibernate.show_sql = true 
hibernate.format_sql = false 
hibernate.hbm2ddl.auto = update 
hibernate.cache.use_second_level_cache = true 
hibernate.cache.use_query_cache = true 
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory 
hibernate.cache.provider_configuration_file_resource_path = ehcache.xml 

enter image description here

回答

0

上下文配置監聽器丟失在web.xml,所以在spring.xml bean不初始化,您可以將其添加到你的web.xml,

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

BTW:在web.xml中spring.xml路徑不正確,你可以改成這個。

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:spring.xml</param-value> 
</context-param> 
+0

非常感謝你,聽衆我忘了補充,我被困在這裏三天 – liwei

相關問題