我已经创建了这段代码并且它对我有用,但是当我运行程序时它会突然抛出错误。
使用名称' adminController'创建bean时出错:通过字段" supplierDaoImpl'表示不满意的依赖关系。
@Component
@Entity
public class Supplier implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private String sId;
@NotBlank(message = "Please enter Supplier Name!")
private String supplierName;
// SupplierDao
public interface SupplierDao {
boolean insertSupplier(Supplier supplier);
boolean updateSupplier(Supplier supplier);
boolean deleteSupplier(Supplier supplier);
Supplier findBySupplierId(String sid);
List<Supplier> retrieve();
}
//SupplierDaoImpl
@Repository
@Service
public class SupplierDaoImpl implements SupplierDao {
@Autowired
private SessionFactory sessionFactory;
@Autowired
public SupplierDaoImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public boolean insertSupplier(Supplier supplier) {
try {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.saveOrUpdate(supplier);
session.getTransaction().commit();
return true;
}catch(Exception exception) {
return false;
}
}
//HibernateConfig
@Configuration
@ComponentScan("com.backend.*")
@EnableTransactionManagement
public class HibernateConfig
{
private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/emart";
private final static String DATABASE_DRIVER = "org.h2.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
private final static String DATABASE_USERNAME = "sa";
private final static String DATABASE_PASSWORD = "";
@Autowired
@Bean(name="dataSource")
public DataSource getH2Data()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
System.out.println("H2 Connected");
return dataSource;
}
private Properties getHiber()
{
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
System.out.println("Tables created");
return properties;
}
@Autowired
@Bean(name="sessionFactory")
public SessionFactory getHiberSession(DataSource dataSource)
{
LocalSessionFactoryBuilder localSessionFactoryBuilder = new LocalSessionFactoryBuilder(dataSource);
localSessionFactoryBuilder.addProperties(getHiber());
localSessionFactoryBuilder.addAnnotatedClass(User.class);
localSessionFactoryBuilder.addAnnotatedClass(Supplier.class);
localSessionFactoryBuilder.addAnnotatedClass(Category.class);
localSessionFactoryBuilder.addAnnotatedClass(Products.class);
return localSessionFactoryBuilder.buildSessionFactory();
}
@Autowired
@Bean(name="supplierDaoImpl")
public SupplierDaoImpl getSupplierData(SessionFactory sessionFactory)
{
return new SupplierDaoImpl(sessionFactory);
}
//AdminController
@Controller
@RequestMapping("/admin")
public class AdminController
{
@Autowired
SupplierDaoImpl supplierDaoImpl;
//Dispatcher Servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context- 3.0.xsd">
<context:annotation-config/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.emart.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="300000000" >
</property></bean>
<mvc:view-controller path="/" view-name="index" />
<mvc:resources location="/assets/" mapping="/webapp/assets/**" cache-period="31556926"/>
</beans>
答案 0 :(得分:0)
尝试删除&#34; getSupplierData&#34;来自HibernateConfig类的方法,它会导致与&#34; SupplierDaoImpl&#34;用&#34; @ Service&#34;注释的类注释