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

时间:2017-12-20 14:15:08

标签: java spring maven spring-mvc tomcat

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

问题 当我运行弹簧启动应用程序时,我得到了

JButton btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {

                String query = "select * from xyzlibrary.user_account";

                Statement st = con.createStatement();
                ResultSet rs=st.executeQuery(query);

                while (rs.next()) {

                    String username= rs.getString(1);
                    String password= rs.getString(2);

                    if (tfusername.getText().equals(username) && tfpasswordField.getText().equals(password)) {

                        JOptionPane.showMessageDialog(null, "Login successfull!");

                        Admin ad = new Admin();
                        ad.setVisible(true);

                        dispose();

                    }else

                        JOptionPane.showMessageDialog(null, "Incorrect User ID or Password!");


                }


            } catch (Exception e) {
                // TODO: handle exception
            }

        }
    });
    btnLogin.setBounds(125, 187, 89, 23);
    contentPane.add(btnLogin);

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 让我怀疑应用程序是在自己的tomcat服务器上运行而不是我已经安装的服务器。

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

Failed to look up JNDI DataSource with name 'jdbc/spring-jdbc-test'

在eclipse中我添加了该版本的tomcat服务器,将sqlite jar添加到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 并在catalina.base/lib/中配置了JNDI资源

catalina.base/conf/context.xml

application.properties

<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>

POM

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

应用

<?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>

使用DataSource的类

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

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

感谢您的帮助!

0 个答案:

没有答案