2015-10-12 6 views
5

मुझे पता है कि वसंत बूट एक dataSource बीन स्वचालित रूप से पैदा करेगा संबंधित विन्यास की तरह, application.properties में स्थापित कर रहे हैं, तो:वसंत बूट एप्लिकेशन में डेटासोर्स को स्वचालित क्यों नहीं किया जा सकता है?

spring.datasource.url = jdbc:mysql://192.168.10.103:3306/hms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull 
spring.datasource.username=root 
[email protected] 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

आवेदन कोड:

package com.synline.mdataserver; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.apache.tomcat.jdbc.pool.DataSource; 

@SpringBootApplication 
public class Application implements CommandLineRunner { 

    @Autowired 
    AnnotationConfigApplicationContext context; 

    /*@Autowired 
    DataSource dataSource;*/ 

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

    @Override 
    public void run(String... args) throws Exception { 
     DataSource dataSource = (DataSource)context.getBean("dataSource"); 
     System.out.println(dataSource); 

     while (true) { 
      Thread.sleep(5000); 
     } 

    } 
} 

तो @Autowired डेटा स्रोत पर टिप्पणी की है, बीन जानकारी मुद्रित की जाएगी:

[email protected]{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; ....} 

तो मुझे लगता है कि वसंत बूट रील वाई बीन बनाया।

लेकिन अगर @Autowried डेटास्रोत प्रयोग किया जाता है, अपवाद शिकायत करने के लिए इस तरह की कोई बीन

Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.apache.tomcat.jdbc.pool.DataSource com.synline.mdataserver.Application.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.apache.tomcat.jdbc.pool.DataSource] 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)} 
+0

पूरा मामला वह काम नहीं करता, न केवल एक टुकड़ा पोस्ट करें। इसके अलावा आपको एक 'javax.sql.DataSource' का उपयोग करना चाहिए जो विशिष्ट tomcat प्रकार नहीं है। –

+0

धन्यवाद। मैं पोस्ट को दोबारा संपादित करता हूं और पूरा कोड जोड़ता हूं। –

+0

आह, दीनम, आपको बिंदु मिल गया। "आयात jgax.sql.DataSource" के बजाय, "import org.apache.tomcat.jdbc.pool.DataSource;" के बजाय, कोई समस्या नहीं है! –

उत्तर

4

आपका चर एक मानक JDBC डेटा स्रोत (अर्थात javax.sql.DataSource) के रूप में घोषित किया जाना चाहिए, न कि इंटरफेस की एक विशेष कार्यान्वयन के रूप में होता है।

+0

हां, यह मूल कारण है। धन्यवाद! –

संबंधित मुद्दे