我正在尝试访问另一个类中的@Bean类,但我继续获取BeanCreationException。
这是我的所有@Bean类的配置文件
CassandraConfig.class
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());
}
}
CassandraTransaction.class
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数据库,但是我在访问bean类时遇到了这个错误。
我们应该只在main()方法中使用ApplicationContext吗?我在public static void main(){}
中看到了很多使用ApplicationContext的例子,如果是这样的话怎么可能在spring中使用main方法呢?我对于必须做的事感到困惑。请解释一下。