Spring-data cassandra tomcat webapp - 无法自动装配CassandraOperations

时间:2015-10-13 08:42:23

标签: tomcat web-applications spring-data spring-annotations spring-data-cassandra

启动webapp时出现以下错误。我试图阅读Spring Data文档,但它们不完整。任何帮助表示赞赏。

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.UserDao controller.LoginController.userDao; nested exception is     org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDao] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 56 more

以下是我项目的详细信息:

**Structure**
src
 main
   java
    bean
        Person.java
    configuration
        ApplicationConfiguration
        CassandraConfiguration    
    controller
        LoginController
    dao
        UserDao

            contextClass             org.springframework.web.context.support.AnnotationConfigWebApplicationContext         

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>configuration.ApplicationConfiguration</param-value>
    </context-param>



    <servlet>
        <servlet-name>applicationContext</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>configuration</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>applicationContext</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/log4j.xml</param-value>
    </context-param>

ApplicationConfiguration.java

@Configuration
@ComponentScan(basePackages = "controller, dao")
@EnableWebMvc

public class ApplicationConfiguration {

private static final Logger LOG = LoggerFactory.getLogger(ApplicationConfiguration.class);

    @Bean
    public User user() {
        return new User();
    }
    @Autowired
    private CassandraOperations cassandraOps;

    @Bean
    TilesViewResolver viewResolver(){
        TilesViewResolver viewResolver = new TilesViewResolver();
        return viewResolver;
    }

    @Bean
    TilesConfigurer tilesConfigurer(){
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions("WEB-INF/tiles-definitions.xml");
        tilesConfigurer.setPreparerFactoryClass(org.springframework.web.servlet.view.tiles3.SpringBeanPreparerFactory
            .class);
        return tilesConfigurer;
    }
}

CassandraConfiguration.java

@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = { "dao" })
public class CassandraConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(CassandraConfiguration.class);
    @Autowired
    private Environment env;

    @Bean
    public CassandraClusterFactoryBean cluster() {

        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
        cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));

        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(env.getProperty("cassandra.keyspace"));
        session.setConverter(converter());
        session.setSchemaAction(SchemaAction.NONE);

        return session;
    }

    public CassandraOperations cassandraTemplate() throws Exception {
        return new CassandraTemplate(session().getObject());
    }

的LoginController

@Controller
public class LoginController {
    private final static Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = Constants.WEBAPP_CONTEXT + "/login.do", method = RequestMethod.GET)
    public ModelAndView login() {
        userDao.addUser();
        return new ModelAndView("login-tile");
    }
   }

userDAO的     @Repository(&#34; userDAO的&#34)     公共课UserDao {

    public static final String TIME_BUCKET = "2014-01-01";

    @Autowired
    private CassandraOperations cassandraOps;

    public void addUser() {
        cassandraOps.insert(new Person("1234567890", "David", 40));
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在@Bean课程的CassandraOperations定义中添加CassandraConfiguration注释。否则它不会被实例化为bean,而Spring也无法自动装配它。

喜欢这个样本:

@Bean <---
public CassandraOperations cassandraTemplate() throws Exception {
    return new CassandraTemplate(session().getObject());
}