Spring Boot - 来自 JNDI 的数据源:NoInitialContextException:需要在环境或系统属性中指定类名

问题描述 投票:0回答:1

我对 Spring Boot 的网络应用程序比较陌生,所以请耐心等待。

问题当我运行我的 Spring Boot 应用程序时,我得到

   Caused by: org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/spring-jdbc-test'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup.getDataSource(JndiDataSourceLookup.java:48) ~[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration.dataSource(JndiDataSourceAutoConfiguration.java:61) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration$$EnhancerBySpringCGLIB$$495e06f0.CGLIB$dataSource$0(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration$$EnhancerBySpringCGLIB$$495e06f0$$FastClassBySpringCGLIB$$7a21dc8c.invoke(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration$$EnhancerBySpringCGLIB$$495e06f0.dataSource(<generated>) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ... 59 common frames omitted
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) ~[na:1.8.0_102]
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) ~[na:1.8.0_102]
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) ~[na:1.8.0_102]
    at javax.naming.InitialContext.lookup(InitialContext.java:417) ~[na:1.8.0_102]
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup.getDataSource(JndiDataSourceLookup.java:45) ~[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ... 70 common frames omitted

Failed to look up JNDI DataSource with name 'jdbc/spring-jdbc-test'
让我怀疑该应用程序在其自己的tomcat服务器上运行,而不是在我安装的服务器上运行。

应用程序在我的 pom 中配置的 tomcat 8.5.23 上启动(见下文)

2017-12-20 13:22:35.477  INFO 19188 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-12-20 13:22:35.478  INFO 19188 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23

在eclipse中我添加了该版本的tomcat服务器,将sqlite jar添加到

catalina.base/lib/
并在
catalina.base/conf/context.xml

中配置了JNDI资源
<Resource 
    auth="Container" 
    driverClassName="org.sqlite.JDBC" 
    maxActive="100" 
    maxIdle="30" 
    maxWait="10000" 
    name="jdbc/spring-jdbc-test" 
    type="javax.sql.DataSource" 
    url="jdbc:sqlite:/${catalina.home}/databases/spring-test.sqlite">
 </Resource>

application.properties

#server.tomcat.basedir=C:\Dev\tomcat\apache-tomcat-8.5.23\ <-- Error when I uncomment this
spring.datasource.jndi-name=jdbc/spring-jdbc-test

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>

    <groupId>spring.jdbc.demo</groupId>
    <artifactId>spring-demo-jdbc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-demo-jdbc</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <tomcat.version>8.5.23</tomcat.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-juli</artifactId>
            <version>${tomcat.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

应用程序

@SpringBootApplication
@ComponentScan(basePackages="spring.jdbc.demo.*")
public class SpringDemoJdbcApplication {

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

使用DataSource的类

@Repository
public class JdbcMessageDAO implements MessageDAO
{
    private static final String SELECT_STATEMENT = "SELECT id, name, message FROM MESSAGES WHERE id=?";
    private static final String SELECT_ALL_STATEMENT = "SELECT id, name, message FROM MESSAGES";

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public JdbcMessageDAO(DataSource dataSource)
    {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }
    /*...*/
}

感谢您的帮助!

java spring maven spring-mvc tomcat
1个回答
0
投票

我刚刚在 Tomcat 9 中解决了这个问题。你所拥有的看起来不错,只是缺少对我有用的两件事:

    Spring 配置中的
  1. @Resource
     注释。比如:
@Configuration public class AppManifest { @Resource(name = "jdbc/spring-jdbc-test", lookup = "jdbc/spring-jdbc-test") private javax.sql.DataSource springJdbcTest; }

    应用程序属性:
spring.datasource.jndi-name=java:comp/env/jdbc/spring-jdbc-test
    
© www.soinside.com 2019 - 2024. All rights reserved.