Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring's Java Based configuration not working for me

I am new to Spring programming and currently struggling with Spring 3.1's Java Based Configuraion" I have created following Configuration class

@Configuration
@ImportResource("classpath:/resources/jdbc.properties")
public class AppConfig {

    @Autowired
    Environment env;
    private @Value("${jdbc.url}")
    String url;
    private @Value("${jdbc.username}")
    String username;
    private @Value("${jdbc.password}")
    String password;

    @Bean
    public DataSource dataSource() {
        System.out.println("Creating data Source.");
        return new DriverManagerDataSource(url, username, password);
    }

    @Bean
    public SessionFactory sessionFactory () throws Exception {
        return new AnnotationSessionFactoryBuilder().setDataSource(dataSource()).setPackagesToScan("com.argusoft.loginmodule.domain").buildSessionFactory();
    }
}

now when I try to run the project I get following error.

OUTPUT

SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable

stuck into it, Cant solve it..... I am following Spring Source Blog.

please also suggest some good tutorial in which Spring's latest Java based configuration is explained by easy to understand examples...

Thanks in advance,

like image 715
Amit Avatar asked Feb 11 '26 09:02

Amit


2 Answers

From the perspective of the exception:

java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable

This question is equals to the question: Spring class EnvironmentCapable

So the correct answer might be:

I think that need use version 3.1.0 - in package org.springframework.core-3.1.0.M2.jar this class presents.

given by user810430 here: original answer.

like image 76
Ralph Avatar answered Feb 15 '26 12:02

Ralph


you can puth configuration like this
inside application context:
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/configuration.properties</value>
            </list>
        </property>
    </bean>

<import resource="db-config.xml" />

and   
db-config.xml is:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">

        <property name="driverClass">
            <value>${jdbc.driver.className}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" autowire="byName">
        <property name="dataSource">
        <ref bean="dataSource" />
        </property>
        <property name="packagesToScan" value="com.huawei.sa.album" />
        <property name="hibernateProperties">
                 <props>
                                <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- uncomment this for first time run-->
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>

    </bean>


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

       <tx:annotation-driven />

</beans>
like image 35
Muhammed şahsuvaroğlu Avatar answered Feb 15 '26 11:02

Muhammed şahsuvaroğlu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!