Весенний конфиг Java с JNDI - PullRequest
       7

Весенний конфиг Java с JNDI

0 голосов
/ 12 декабря 2018

У меня был класс JndiDatasourceCreator, и у него есть компонент в контексте приложения

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

Работает нормально.Но когда я перемещаю его в конфигурацию Java, он не работает.

Java config 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();
        }
    }
}

Спасибо за помощь !!!

...