Sla*_*hin 8 java spring spring-mvc
我正在使用Spring MVC 3.1.0M2并尝试将我的配置移动到java bean.但我遇到以下错误:
2011-09-14 18:43:42.301:警告:/:不可用org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration#0'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配方法:void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection); 嵌套异常是org.springframework.beans.factory.BeanCreationException:在类ru.mystamps.web.config.DbConfig中定义名为'entityManagerFactory'的bean时出错:bean的实例化失败; 嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ru.mystamps.web.config.DbConfig.entityManagerFactory()]引发异常; 嵌套异常是java.lang.IllegalArgumentException:DataSource不能为null
映射来自web.xml
:
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
ru.mystamps.web.config.MvcConfig,
ru.mystamps.web.config.DbConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)
DbConfig.java
:
@Configuration
@EnableTransactionManagement
@ImportResource("classpath:spring/datasource.xml")
public class DbConfig {
@Autowired
private DataSource dataSource;
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
final HibernateJpaVendorAdapter jpaVendorAdapter =
new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabasePlatform(dialectClassName);
jpaVendorAdapter.setShowSql(showSql);
return jpaVendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManagerFactory =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
entityManagerFactory.setDataSource(dataSource);
final Map<String, String> jpaProperties = new HashMap<String, String>();
jpaProperties.put("hibernate.format_sql", formatSql);
jpaProperties.put("hibernate.connection.charset", "UTF-8");
jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddl);
entityManagerFactory.setJpaPropertyMap(jpaProperties);
return entityManagerFactory;
}
@Bean
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager =
new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
...
}
Run Code Online (Sandbox Code Playgroud)
spring/datasource.xml
:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<context:property-placeholder location="classpath:spring/database.properties" />
<beans profile="dev">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
</beans>
<beans profile="test">
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:test-data.sql" />
</jdbc:embedded-database>
</beans>
</beans>
Run Code Online (Sandbox Code Playgroud)
我希望dataSource
在导入后创建bean ,datasource.xml
但我总是遇到这个错误.
TIA
我找到了错误原因,只有当我手动定义时才会发生PersistenceAnnotationBeanPostProcessor
:
@Bean
public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() {
// enable injection of EntityManager to beans with @PersistenceContext annotation
return new PersistenceAnnotationBeanPostProcessor();
}
Run Code Online (Sandbox Code Playgroud)
很抱歉,因为我没有在问题中发布完整的代码(因为我认为这个 bean 并不重要)。当我删除这个定义时,一切都按预期进行。我还发现在我的例子中这个 bean 已经被注册了:
注意:默认的 PersistenceAnnotationBeanPostProcessor 将通过“context:annotation-config”和“context:component-scan”XML 标记注册。如果您打算指定自定义 PersistenceAnnotationBeanPostProcessor bean 定义,请删除或关闭那里的默认注释配置。
归档时间: |
|
查看次数: |
7207 次 |
最近记录: |