Java 访问@Configuration类中的@Bean时出现BeanCreationException
我正在尝试访问我的另一个类中的@Bean类,但我一直得到BeanCreationException 这是我的配置文件和所有@Bean类 CassandraConfig.classJava 访问@Configuration类中的@Bean时出现BeanCreationException,java,spring,Java,Spring,我正在尝试访问我的另一个类中的@Bean类,但我一直得到BeanCreationException 这是我的配置文件和所有@Bean类 CassandraConfig.class package com.spring.cassandra.daos; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import or
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);
}
}
}
我需要访问CassandraOperations cassandraTemplate()bean,以便使用模板查询cassandra DB,但在访问bean类时,我遇到了这个错误
我们应该只在main()方法中使用ApplicationContext吗?我在
publicstaticvoidmain(){}
中看到了许多使用ApplicationContext的例子,如果是这样的话,在spring中使用main方法怎么可能呢?我对必须做什么感到困惑。请解释。java.lang.Error:未解决的编译问题
-您需要先编译一个有效的java程序(这可能是编译的,因为Eclipse编译器允许您编译标准编译器无法编译的程序)。如果我删除CassandraTransaction.class中的JavaConfigApplicationContext
,并且我的应用程序运行正常,则不会出现编译错误。你还说这是一个编译问题吗?根据例外情况,它看起来像是编译问题。正如您所说,“如果我在CassandraTransaction.class中删除JavaConfigApplicationContext,我看不到编译错误”。当你把它放回去的时候,你看到错误了吗?没有爱资哈尔。当我撤消这些行时,我没有看到这样的编译错误。运行良好。使用JavaConfigApplicationContext访问@Bean的方式是否正确?
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: