mrjimoy_05
mrjimoy_05

Reputation: 3568

Error `specify class name in environment or system property` while using Jndi

What is wrong with this code? I tried to run the file through .sh and get this error :

Error creating bean with name 'entityManagerFactory' defined in class path resource [com/.../beans.xml]: 
Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource [com/.../beans.xml]: 
Invocation of init method failed; 
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

This is the code that file .sh execute (I omitted unnecessary code) :

public final class FraudCacheBatchJob {
    private static final String CLASS_NAME = FraudCacheBatchJob.class.getCanonicalName();
    private static final String CONTEXT_XML_NAME = "/beans.xml";

    private static String getPackageLocation() {
        return FraudCacheBatchJob.class.getPackage().getName().replace(".", "/");
    }

    public static void main(String[] args) {
        try {
            ApplicationContext context = new ClassPathXmlApplicationContext(getPackageLocation() + CONTEXT_XML_NAME);

            CachedRule cachedRule = context.getBean("cachedRule", CachedRule.class);

            ...
        } catch (Exception ex) {
        }
    }
}

beans.xml :

...
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="com....." />

<jpa:repositories base-package="com....." />

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jca:/console.dbpool/Connection1/JCAManagedConnectionFactory/Connection1"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="hibernateJpaAdapter" />         
    <property name="packagesToScan" value="com.....persistence"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>              
        </props>        
    </property>
</bean>

<bean id="hibernateJpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
...

I have specified the jndi parameter on the server.

Is there something else? Thanks.

Upvotes: 1

Views: 5695

Answers (1)

Cristian Sevescu
Cristian Sevescu

Reputation: 1489

It is complaining that you did not set "java.naming.factory.initial".

As you already use Spring, you could create a JndiTemplate like this:

<bean id="myJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
    <props>
        <prop key="java.naming.factory.initial"org.jnp.interfaces.NamingContextFactory</prop>
        <prop key="java.naming.provider.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
    <props>
        <prop key="java.naming.provider.url">jnp://myhost:1099</prop>
    <props>
</property>

These configurations are for JBoss. If you use another implementation of JNDI you need to adjust

Upvotes: 1

Related Questions