我正在构建一个使用 spring 4.1.2 和 Cassandra 2.1.2 作为数据库的 Web 应用程序。在 spring-data-cassandra 的集成过程中,我遇到了 bean 创建异常。谁能指导我哪里出错了?错误跟踪是
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.cassandra.core.CassandraOperations someapp.dao.role.JdbcSomeDao.cassandraOperations; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException. No qualifying bean of type [org.springframework.data.cassandra.core.CassandraOperations] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的源代码如下:
Web 应用程序被初始化的 Web 初始化文件
public class WebInitConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(DispatcherConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(CassandraConfig.class);
// The main Spring MVC servlet.
ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(mvcContext));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
for (String s : mappingConflicts) {
System.out.println("Mapping conflict: " + s);
}
throw new IllegalStateException(
"'appServlet' cannot be mapped to '/' under Tomcat versions <= 7.0.14");
}
}
}
包含 spring 调度程序代码和视图解析器的 DispatcherConfig 文件。
@Configuration
@EnableWebMvc
@PropertySource("classpath:cassandra.properties")
@ComponentScan("randompackage.*")
public class DispatcherConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
}
@Bean
public ViewResolver configureViewResolver() {
InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
viewResolve.setPrefix("/WEB-INF/jsp/");
viewResolve.setSuffix(".jsp");
return viewResolve;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
}
spring-data-cassandra 配置文件“CassandraConfig.java,在应用启动时加载在 webinitconfig.java
@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = { "someapp.dao" })
public class CassandraConfig {
/*
@Autowired
private Environment env;
*/
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints("10.20.13.157");
cluster.setPort(Integer.parseInt("9042"));
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName("spring_data");
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
@Bean(name="someManager")
public SomeManager getSomeManager() throws Exception {
return new someClassManager();
}
@Bean(name="someDao")
public SomeDao getSomeDao() throws Exception {
return new someDao();
}
}
我有各自的控制器、服务和道。
@Controller
public class SomeRegistrationController {
@Autowired
SomeManager someManager;
@RequestMapping(value="registration.htm")
public String registration() {
someManager.Registration(new SomeDom());
return "registration";
}
}
一些映射的服务类
@Service("someManager")
public class SomeClassManager implements SomeManager {
@Autowired
SomeDao someDao;
@Override
public void registration(SomeDom details) {
someDao.registration(details);
}
}
jdbc交互的dao类
@Repository("someDao")
//@EnableAutoConfiguration
public class JDBCSomeDao implements SomeDao{
@Autowired
private CassandraOperations cassandraOperations;
@Override
public void Registration(SomeDom details) {
System.out.println("JDBC");
System.out.println(cassandraOperations.toString());
}
}