有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在@Configuration类中访问@Bean时出现java BeanCreationException

我正在尝试访问我的另一个类中的@Bean类,但我一直得到BeanCreationException

这是我的配置文件和所有@Bean类

卡桑德拉Config。阶级

package com.spring.cassandra.daos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;

@Configuration
@PropertySource(value = { "classpath:com/spring/cassandra/props/cassandra.properties" })
public class CassandraConfig {
    public CassandraConfig(){
        System.out.println("In Cassandra Config");
    }

    @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;
    }

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

卡桑德拉交易。阶级

package com.spring.cassandra.daos;

import java.util.List;

import org.springframework.cassandra.core.RowMapper;
import org.springframework.config.java.context.JavaConfigApplicationContext;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Component;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;

@Component
public class CassandraTransaction {

/*Error in this below two lines while trying to access the CassandraTemplate.class*/


    JavaConfigApplicationContext context = new JavaConfigApplicationContext(CassandraConfig.class);
    CassandraOperations cassandraOperations = (CassandraOperations) context.getBean(CassandraTemplate.class);

    private CassandraTransaction(){
        System.out.println("In Cassandra Transaction");
        getUsers();
    }

    public void getUsers(){
        System.out.println("Retreiving users");
        List<User> results = cassandraOperations.query("select * from users", new RowMapper<User>() {
            @Override
            public User mapRow(Row row, int rowNum) throws DriverException {
                User user = new User();
                user.setUsername(row.getString("username"));
                user.setName(row.getString("name"));
                user.setEmail(row.getString("email"));
                return user;
            }
        });

        for(User user : results){
            System.out.println(user);
        }
    }
}

控制台中的异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTransaction' defined in file [D:\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\CassandraApplication\WEB-INF\classes\com\spring\cassandra\daos\CassandraTransaction.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.spring.cassandra.daos.CassandraTransaction]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 

我需要访问CassandraOperations cassandraTemplate()bean,以便使用模板查询cassandra DB,但在访问bean类时,我遇到了这个错误

我们应该只在main()方法中使用ApplicationContext吗?我在public static void main(){}中看到许多使用ApplicationContext的示例,如果是这样的话,在spring中使用main方法怎么可能呢?我对必须做什么感到困惑。请解释一下


共 (0) 个答案