使用JNDI

时间:2018-12-12 11:02:05

标签: java spring jndi

我有一个JndiDatasourceCreator类,并且在应用程序上下文中有一个bean

<bean id="jndi" class="se.test.util.JndiDatasourceCreator" lazy-init="false" />

工作正常。但是,当我将其移入Java配置时,它无法正常工作。

这样的Java配置bean

@Bean
  @Lazy(value = false)
  public JndiDatasourceCreator jndi(){
    return new JndiDatasourceCreator();
  }

我现在遇到这样的错误

Caused by: org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/sharedTender'; 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)
    at se.mindspot.tender.backend.config.HibernateConfig.sharedDataSource(HibernateConfig.java:102)
    at se.mindspot.tender.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$ba1e5c30.CGLIB$sharedDataSource$7(<generated>)
    at se.mindspot.tender.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$ba1e5c30$$FastClassBySpringCGLIB$$755d2b99.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at se.mindspot.tender.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$ba1e5c30.sharedDataSource(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 43 more
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(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unkno

我的JNDI创建者类在这里:

public class JndiDatasourceCreator {

    public JndiDatasourceCreator(){
        try {
            DriverManagerDataSource dataSource=null;
            final SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
            dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/rshared?autoReconnect=true");
            dataSource.setUsername("root");
            dataSource.setPassword("root");
            builder.bind("jdbc/sharedTender", dataSource);
            builder.activate();
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
}

感谢您的帮助!

0 个答案:

没有答案