0
我試圖讓Spring MVC中的項目卡桑德拉,試圖在控制器來顯示數據,控制器:錯誤在Spring MVC創建豆卡桑德拉項目
package com.pji.cassandraInSpringMvc;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.pji.cassandraInSpringMvc.entity.Employee;
import com.pji.cassandraInSpringMvc.service.EmployeeService;
@Controller
public class HomeController {
@Autowired
EmployeeService employeeService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
List<Employee> employees = new ArrayList<Employee>();
employees = employeeService.getAllEmployees();
for (Employee employee : employees) {
System.out.println("ID: " + employee.getId());
System.out.println("NAME: " + employee.getName());
System.out.println("SALARY: " + employee.getSalary());
}
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
}
EmployeeDAOImpl:
package com.pji.cassandraInSpringMvc.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.pji.cassandraInSpringMvc.entity.Employee;
import com.pji.cassandraInSpringMvc.util.MyCassandraTemplate;
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
@Autowired
private MyCassandraTemplate myCassandraTemplate;
@Override
public Employee createEmployee(Employee employee) {
return myCassandraTemplate.create(employee);
}
@Override
public Employee getEmployee(int id) {
return myCassandraTemplate.findById(id, Employee.class);
}
@Override
public Employee updateEmployee(Employee employee) {
return myCassandraTemplate.update(employee, Employee.class);
}
@Override
public void deleteEmployee(int id) {
myCassandraTemplate.deleteById(id, Employee.class);
}
@Override
public List<Employee> getAllEmployees() {
return myCassandraTemplate.findAll(Employee.class);
}
}
EmployeeServiceImpl:
package com.pji.cassandraInSpringMvc.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pji.cassandraInSpringMvc.dao.EmployeeDAO;
import com.pji.cassandraInSpringMvc.entity.Employee;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDAO employeeDAO;
public EmployeeServiceImpl() {
super();
}
@Override
public Employee createEmployee(Employee employee) {
return employeeDAO.createEmployee(employee);
}
@Override
public Employee getEmployee(int id) {
return employeeDAO.getEmployee(id);
}
@Override
public Employee updateEmployee(Employee employee) {
return employeeDAO.updateEmployee(employee);
}
@Override
public void deleteEmployee(int id) {
employeeDAO.deleteEmployee(id);
}
@Override
public List<Employee> getAllEmployees() {
return employeeDAO.getAllEmployees();
}
}
實體:
package com.pji.cassandraInSpringMvc.entity;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;
@Table("employee")
public class Employee {
@PrimaryKey("id")
private long id;
@Column("name")
private String name;
/*@Column
private int age; */
@Column(value ="salary")
private float salary;
/**
* Default Constructor
*/
public Employee() {
super();
}
/**
* Parameterized Constructor
* @param id
* @param name
* @param age
* @param salary
*/
public Employee(long id, String name, float salary) {
super();
this.id = id;
this.name = name;
//this.age = age;
this.salary = salary;
}
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
/*public int getAge() {
return age;
}*/
/**
* @param age the age to set
*/
/*public void setAge(int age) {
this.age = age;
}*/
/**
* @return the salary
*/
public float getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(float salary) {
this.salary = salary;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age="
+ ", salary=" + salary + "]";
}
}
CassandraUtil:
package com.pji.cassandraInSpringMvc.util;
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:cassandra.properties" })
public class CassandraUtil {
private static final String KEYSPACE = "cassandra.keyspace";
private static final String CONTACTPOINTS = "cassandra.contactpoints";
private static final String PORT = "cassandra.port";
@Autowired
private Environment environment;
public CassandraUtil() {
System.out.println("CassandraUtil()");
}
private String getKeyspaceName() {
return environment.getProperty(KEYSPACE);
}
private String getContactPoints() {
return environment.getProperty(CONTACTPOINTS);
}
private int getPortNumber() {
return Integer.parseInt(environment.getProperty(PORT));
}
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(getContactPoints());
cluster.setPort(getPortNumber());
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean cassandraSessionFactoryBean = new CassandraSessionFactoryBean();
cassandraSessionFactoryBean.setCluster(cluster().getObject());
cassandraSessionFactoryBean.setKeyspaceName(getKeyspaceName());
cassandraSessionFactoryBean.setConverter(converter());
cassandraSessionFactoryBean.setSchemaAction(SchemaAction.NONE);
return cassandraSessionFactoryBean;
}
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
}
MyCassandraTemplate:在XML
package com.pji.cassandraInSpringMvc.util;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;
@Repository
public class MyCassandraTemplate {
@Autowired
private CassandraOperations cassandraTemplate;
public MyCassandraTemplate() {
System.out.println("MyCassandraTemplate()");
}
public <T> T create(T entity) {
return cassandraTemplate.insert(entity);
}
public <T> void createList(List<T> entities) {
cassandraTemplate.insert(entities);
}
public <T> T update(T entity) {
return (T) cassandraTemplate.update(entity);
}
public <T> void updateList(List<T> entities) {
cassandraTemplate.update(entities);
}
public <T> T update(T entity, Class<T> claz) {
return (T) cassandraTemplate.update(entity);
}
public <T> T findById(Object id, Class<T> claz) {
return cassandraTemplate.selectOneById(claz, id);
}
public <T> void deleteById(Object id, Class<T> claz) {
cassandraTemplate.deleteById(claz, id);
}
public void delete(Object entity) {
cassandraTemplate.delete(entity);
}
public <T> void delete(List<T> entities) {
cassandraTemplate.delete(entities);
}
public <T> void deleteAll(Class<T> claz) {
cassandraTemplate.deleteAll(claz);
}
public <T> List<T> findAll(Class<T> claz) {
return (List<T>) cassandraTemplate.selectAll(claz);
}
public <T> List<T> findAll(List<Object> ids, Class<T> claz) {
return cassandraTemplate.selectBySimpleIds(claz, ids);
}
public <T> void truncate(Class<T> claz) {
cassandraTemplate.truncate(claz.getName());
}
public <T> long getCount(Class<T> claz) {
return cassandraTemplate.count(claz);
}
public <T> boolean exists(Object id, Class<T> claz) {
return cassandraTemplate.exists(claz, id);
}
}
應用上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.pji.cassandraInSpringMvc" />
這是我的錯誤的程序運行的時候:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.pji.cassandraInSpringMvc.util.MyCassandraTemplate com.pji.cassandraInSpringMvc.dao.EmployeeDAOImpl.myCassandraTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myCassandraTemplate': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.cassandra.core.CassandraOperations com.pji.cassandraInSpringMvc.util.MyCassandraTemplate.cassandraTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTemplate' defined in class path resource [com/pji/cassandraInSpringMvc/util/CassandraUtil.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.cassandra.core.CassandraOperations com.pji.cassandraInSpringMvc.util.CassandraUtil.cassandraTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/pji/cassandraInSpringMvc/util/CassandraUtil.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: io/netty/util/concurrent/EventExecutor
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.pji.cassandraInSpringMvc.util.MyCassandraTemplate com.pji.cassandraInSpringMvc.dao.EmployeeDAOImpl.myCassandraTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myCassandraTemplate': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.cassandra.core.CassandraOperations com.pji.cassandraInSpringMvc.util.MyCassandraTemplate.cassandraTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTemplate' defined in class path resource [com/pji/cassandraInSpringMvc/util/CassandraUtil.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.cassandra.core.CassandraOperations com.pji.cassandraInSpringMvc.util.CassandraUtil.cassandraTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/pji/cassandraInSpringMvc/util/CassandraUtil.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: io/netty/util/concurrent/EventExecutor
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
任何人可以幫助我,請