我有一个相当大的应用程序,试图将.xml bean定义转换为Java批注。该应用程序分为多个子项目,其中一个是Web应用程序(Tomcat),另一个是被编译为jar并放在Web应用程序类路径中的库。
在这些子项目库之一中,我有一个Spring配置类,用于定义SessionFactory bean。在许多DAO实现中,该bean都是自动装配的。但是,每当我尝试启动我的应用程序时,Spring都无法创建DAO的bean,因为它找不到SessionFactory bean。我得到了很长的堆栈跟踪,最后带有NoSuchBeanDefinitionException。
在Web应用程序子项目中,我尝试更改组件扫描以首先使用SessionFactoryConfig.java扫描该程序包,然后再使用定义的其他程序进行扫描。发生相同的错误,但是它尝试创建的bean不同。在与SessionFactoryConfig.java相同的程序包中还有另一个配置类,该类可以自动装配SessionFactory bean,但是无法创建而不是DAO。
SessionFactoryConfig.java
@Configuration
public class SessionFactoryConfig {
@Autowired
private DataSource dataSource;
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource);
localSessionFactoryBean.setPackagesToScan("com.mycompany.model", "com.mycompany.extra.model");
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
properties.setProperty("hibernate.show_sql", "false");
properties.setProperty("hibernate.connection.release_mode", "after_transaction");
properties.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
properties.setProperty("hibernate.cache.use_query_cache", "true");
properties.setProperty("hibernate.generate_statistics", "false");
properties.setProperty("hibernate.max_fetch_depth", "2");
properties.setProperty("hibernate.cache.use_second_level_cache", "false");
properties.setProperty("hibernate.cache.use_structured_entries", "true");
localSessionFactoryBean.setHibernateProperties(properties);
return localSessionFactoryBean.getObject();
}
}
堆栈跟踪的底部
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1252)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 28 more
项目布局和组件扫描
project_root
+-- dao
| +-- src/main/java
| +-- com
| +-- mycompany
| +-- dao
| +-- config
| | +-- DataSourceConfig.java
| | +-- SessionFactoryConfig.java
| | +-- TransactionConfig.java
| +-- impl
| +-- <Each model>DaoImpl.java
+-- web_server
+-- src/main/webapp
+-- WEB-INF
+-- web.xml
+-- applicationContext.xml
+-- <others>.xml
web.xml
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml
/WEB-INF/applicationContext-security-jdbc.xml
/WEB-INF/camelConfig.xml
</param-value>
</context-param>
...
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
">
<context:component-scan base-package="com.mycompany" />
...
我也尝试过
<context:component-scan base-package="com.mycompany.dao.config, com.mycompany" />
,没有运气。