Spring-data cassandra tomcat webapp -无法自动连接cassandra operati



启动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

下面是我的项目的细节:

<>之前* * * *结构src主要java豆将位于配置ApplicationConfigurationCassandraConfiguration控制器LoginController刀UserDao之前

contextClassorg.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@("userDao")公共类UserDao {

    public static final String TIME_BUCKET = "2014-01-01";
    @Autowired
    private CassandraOperations cassandraOps;
    public void addUser() {
        cassandraOps.insert(new Person("1234567890", "David", 40));
    }
}

您需要在CassandraConfiguration类的CassandraOperations定义上放置@Bean注释。否则它不会被实例化为bean,而且Spring也不能自动装配它。

像这样的示例:

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

相关内容

  • 没有找到相关文章

最新更新