Spring 的基于 Java 的配置对我不起作用



我是Spring编程的新手,目前正在努力使用Spring 3.1的基于Java的配置"我创建了以下配置类

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

现在当我尝试运行项目时,我得到以下错误:

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

卡住了,解决不了.....我在关注Spring Source Blog。

还请建议一些好的教程,其中Spring最新的基于Java的配置通过易于理解的示例进行解释…

提前致谢

从异常的角度看:

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

这个问题等于这个问题:Spring class EnvironmentCapable

所以正确答案可能是:

我认为需要使用3.1.0版本的包

由user810430给出:原始答案

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>

相关内容

  • 没有找到相关文章

最新更新