我是Spring编程的新手,目前正在努力使用Spring 3.1的Java Based Configuraion“我创建了以下Configuration类
@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();
}
}
现在当我尝试运行项目时,我得到了以下错误。
输出
严重:加载应用时出现异常: java.lang.IllegalStateException:ContainerBase.addChild:start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException:javax.servlet.ServletException: java.lang.NoClassDefFoundError: 组织/ springframework的/核心/ ENV / EnvironmentCapable
坚持下去,无法解决它......我正在关注Spring Source Blog。
还请提供一些很好的教程,其中Spring的最新基于Java的配置通过易于理解的示例进行解释...
提前致谢,
答案 0 :(得分:2)
从例外的角度来看:
java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable
这个问题等于问题:Spring class EnvironmentCapable
所以正确答案可能是:
我认为需要使用版本3.1.0 - in package org.springframework.core-3.1.0.M2.jar这个类介绍。
由user810430在此处提供:original answer。
答案 1 :(得分:1)
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>