Spring 4 DataSource连接

时间:2018-06-15 09:17:13

标签: spring annotations datasource

我的Web应用程序中有一个Spring 4.1.4框架,我想要外部数据库配置,然后将db信息放入META-INF / context.xml

我经常使用XML Spring配置,如果我尝试按如下方式检索我的连接:

的applicationContext-dataSource.xml

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/informagiovani" />
    <property name="proxyInterface" value="javax.sql.DataSource" />
</bean>

的beans.xml

<import resource = "spring/applicationContext-dataSource.xml" />

Java DAO

public Connection getConnection() {
    Connection conn = null;
    try {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("resources/beans.xml"); 
        DataSource dataSource = (DataSource) context.getBean("dataSource");
        conn = dataSource.getConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

一切正常..如果我尝试使用如下注释:

的AppConfig

@Configuration
public class DataSourceConfiguration {
@Bean
public DataSource dataSource() {
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);
    DataSource dataSource = dsLookup.getDataSource("jdbc/informagiovani");
    return dataSource;
}
}

Java DAO

public Connection getConnection() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DataSourceConfiguration.class);
    Connection conn = null;
    try {
        DataSource dataSource = ctx.getBean(DataSource.class);
        conn = dataSource.getConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

我得到以下错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/lookup/JndiDataSourceLookup
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 28 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/lookup/JndiDataSourceLookup
at it.informagiovani.core.dbmanager.DataSourceConfiguration.dataSource(DataSourceConfiguration.java:14)
at it.informagiovani.core.dbmanager.DataSourceConfiguration$$EnhancerBySpringCGLIB$$6519ad12.CGLIB$dataSource$0(<generated>)
at it.informagiovani.core.dbmanager.DataSourceConfiguration$$EnhancerBySpringCGLIB$$6519ad12$$FastClassBySpringCGLIB$$4c237871.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
at it.informagiovani.core.dbmanager.DataSourceConfiguration$$EnhancerBySpringCGLIB$$6519ad12.dataSource(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 29 more

显然jar是在Spring-jdbc 4.1.4中。你能救我吗?

我的pom.xml如下:

<dependencies>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.0.5.RELEASE</version>
    <exclusions>
            <exclusion>
                <groupId>spring-orm</groupId>
                <artifactId>spring-orm</artifactId>
            </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.0.1.Final</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

即使我添加spring-boot-starter依赖项,错误也是一样的。 谢谢

0 个答案:

没有答案