本文介绍了创建Spring bean时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Spring 3.1.4
控制器类
package com.demo.scheduler.controller;
import java.util.Map;
import com.demo.scheduler.service.SchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
公共类SchedulerController {
Private SchedulerService服务;
@Autowired
public SchedulerController(SchedulerService服务){
this.service = service;
$ b @RequestMapping(value =/ tasklist,method = RequestMethod.GET)
public String listTask(Map< String,Object> model){
model.put(task,service.getTask());
返回tasklist;
$ b $ / code $ / pre
服务实现
package com.demo.scheduler.service;
import com.demo.scheduler.TaskConfig;
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class SchedulerServiceImpl实现SchedulerService {
@Autowired
public SchedulerDAO schedulerDAO ;
@Override
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
public List< TaskConfig> getTask(){
return schedulerDAO.getTask();
}
}
DAO实现
package com.demo.scheduler.dao;
import com.demo.scheduler.TaskConfig;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class HibernateDAO实现SchedulerDAO {
@Autowired
private SessionFactory sessionFactory;
public HibernateDAO(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@SuppressWarnings(unchecked)
@Override
public List< TaskConfig> getTask(){
return sessionFactory.getCurrentSession()。createCriteria(
TaskConfig.class).list();
}
}
web.xml
<?xml version =1.0encoding =UTF-8?>
xmlns =http://java.sun.com/xml/ns/j2eexmlns:xsi =http:/ /www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns /j2ee/web-app_2_4.xsd\">
< display-name> demoscheduler< / display-name>
< welcome-file-list>
< welcome-file> index.html< / welcome-file>
< welcome-file> index.htm< / welcome-file>
< welcome-file> index.jsp< / welcome-file>
< welcome-file> default.html< / welcome-file>
< welcome-file> default.htm< / welcome-file>
< welcome-file> default.jsp< / welcome-file>
< / welcome-file-list>
< servlet>
< servlet-name> demo< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet
< / servlet-class>< / servlet>
< servlet-mapping>
< servlet-name> demo< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>< session-config>
< session-timeout> 60< / session-timeout>
< / session-config>
< / web-app>
demo-servlet.xml
<?xml version =1.0encoding =UTF-8?>
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :context =http://www.springframework.org/schema/context
xmlns:tx =http://www.springframework.org/schema/tx
xsi:schemaLocation = http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework。 org / schema / context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd\">
< context:component-scan base-package =com.demo.scheduler/>
< context:annotation-config />
< tx:annotation-driven />
< bean id =jspViewResolver
class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =viewClass
value =org.springframework.web.servlet.view.JstlView/>
< property name =prefixvalue =/ WEB-INF / jsp //>
< property name =suffixvalue =。jsp/>
< / bean>
< bean id =propertyConfigurer
class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =location>
<值> /WEB-INF/resources/hibernate.properties
< /值>
< / property>
< / bean>
< bean class =org.springframework.orm.hibernate3.LocalSessionFactoryBean
id =sessionFactory>
< property name =dataSourceref =dataSource>
< / property>
< property name =configLocations>
< list>
<值> /WEB-INF/resources/schedulerTask.hbm.xml
< /值>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {hibernate.dialect}< / prop>
< prop key =hibernate.show_sql> $ {hibernate.show_sql}< / prop>
< prop key =hibernate.format_sql> $ {hibernate.format_sql}< / prop>
< prop key =hibernate.connection.provider_class> $ {hibernate.connection.provider_class}
< / prop>
< prop key =hibernate.c3p0.acquire_increment> $ {hibernate.c3p0.acquire_increment}
< / prop>
< prop key =hibernate.c3p0.idle_test_period> $ {hibernate.c3p0.idle_test_period}
< / prop>
< prop key =hibernate.c3p0.max_size> $ {hibernate.c3p0.max_size}
< / prop>
< prop key =hibernate.c3p0.max_statements> $ {hibernate.c3p0.max_statements}
< / prop>
< prop key =hibernate.c3p0.min_size> $ {hibernate.c3p0.min_size}
< / prop>
< prop key =hibernate.c3p0.timeout> $ {hibernate.c3p0.timeout}
< / prop>
< /道具>
< / property>
< / bean>
< bean class =org.springframework.jdbc.datasource.DriverManagerDataSource
id =dataSource>
< property name =driverClassNamevalue =$ {hibernate.connection.driver_class}/>
< property name =urlvalue =$ {hibernate.connection.url}/>
< property name =usernamevalue =$ {hibernate.connection.username}/>
< property name =passwordvalue =$ {hibernate.connection.password}/>
< / bean>
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< / beans>
更新代码后,出现以下错误:
解决方案如果发现我的错误。问题是该配置中没有任何东西来实例化我的DAO bean。所以,我在应用程序上下文中修改了我的context:component-scan元素,以获取我的DAO。
< context:component-scan base-package =com.demo.scheduler>
< context:include-filter type =annotation
expression =org.springframework.stereotype.Service/>
< context:include-filter type =annotation
expression =org.springframework.stereotype.Repository/>
< context:exclude-filter type =annotation
expression =org.springframework.stereotype.Controller/>
< / context:component-scan>
感谢您的帮助@ M.Deinum和@SotiriosDelimanolis
I'm using Spring 3.1.4
Controller class
package com.demo.scheduler.controller;
import java.util.Map;
import com.demo.scheduler.service.SchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class SchedulerController {
private SchedulerService service;
@Autowired
public SchedulerController(SchedulerService service) {
this.service = service;
}
@RequestMapping(value = "/tasklist",method = RequestMethod.GET)
public String listTask(Map<String, Object> model) {
model.put("task", service.getTask());
return "tasklist";
}
}
Service Implementation
package com.demo.scheduler.service;
import com.demo.scheduler.TaskConfig;
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class SchedulerServiceImpl implements SchedulerService {
@Autowired
public SchedulerDAO schedulerDAO;
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public List<TaskConfig> getTask() {
return schedulerDAO.getTask();
}
}
DAO Implementation
package com.demo.scheduler.dao;
import com.demo.scheduler.TaskConfig;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class HibernateDAO implements SchedulerDAO {
@Autowired
private SessionFactory sessionFactory;
public HibernateDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
@Override
public List<TaskConfig> getTask() {
return sessionFactory.getCurrentSession().createCriteria(
TaskConfig.class).list();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>demoscheduler</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class></servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping><session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
demo-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.demo.scheduler" />
<context:annotation-config />
<tx:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/resources/hibernate.properties
</value>
</property>
</bean>
<bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
id="sessionFactory">
<property name="dataSource" ref="dataSource">
</property>
<property name="configLocations">
<list>
<value>/WEB-INF/resources/schedulerTask.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.connection.provider_class">${hibernate.connection.provider_class}
</prop>
<prop key="hibernate.c3p0.acquire_increment">${hibernate.c3p0.acquire_increment}
</prop>
<prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}
</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}
</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}
</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}
</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}
</prop>
</props>
</property>
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
After updating the code, I getting the following error:
解决方案 If found out my error.The problem is that there is nothing in that configuration to instantiate my DAO bean. So, I modified my "context:component-scan" element in application context to also pick up my DAO.
<context:component-scan base-package="com.demo.scheduler">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
Thanks for the help @M.Deinum and @SotiriosDelimanolis
这篇关于创建Spring bean时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!