当前位置: 首页 > 知识库问答 >
问题:

创建Spring bean时出错

洪光霁
2023-03-14

我使用的是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
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";

    }
}

服务实现

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实现

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>
<?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>

共有1个答案

郭恩
2023-03-14

如果发现了我的错误,问题是配置中没有任何东西可以实例化我的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的帮助

 类似资料:
  • 有没有办法在刷新Spring上下文后找出bean创建顺序(或至少依赖顺序)? 只要简单的bean定义顺序就可以了。我不想要他们注册的顺序。我想要创建它们的顺序(依赖关系树)

  • 问题内容: 我有一个关于Eclipse,Wicket,Spring,Hibernate的项目。一切正常,除了:当我尝试 服务变量为空?在我使用此构造的任何其他地方,“ service”都不为null且运行良好。请帮我解决这个问题。 问题答案: @SpringBean仅在Component的任何子类中起作用。 您需要在构造函数中执行以下操作 小门1.4 小门1.5+ 参见“通用IDataProvid

  • 我有一个带有Spring Data JPA的Web应用程序。我为一些实体创建了扩展JpaRepository的存储库。但是,当我尝试部署应用程序时,会遇到以下错误。不知道为什么会抛出错误 应用程序错误日志: pom.xml 应用程序上下文.xml persistence.xml JpaRepostory接口

  • 我试图编译一个非常简单的程序,将包含3个用户的简单表保存到http://localhost/phpmyadmin,以清空名为,users ' '的数据库,但它仍然显示异常,您可以看到。 1个异常org.springframework.beans.factory。BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure

  • 当我启动Weblogic时(使用jar:hibernate-core-4.3.6.final.jar和hibernate-jpa-2.1-api-1.0.0.final.jar),遇到以下错误信息: 无法自动连接字段:private org.hibernate.sessionFactory com.nscorp.lars.shopleveling.core.dao.impl.Dataloaddao

  • 我在SpringBoot应用程序中创建HighHendRestClient bean时遇到一个错误。我已经做了一个测试'app',在那里我检查了我可以实例化我想要的对象,然后进行我想要的调用,我现在正在做一个新的应用程序的婴儿步骤。 就我所能看到的(我还没有用它做太多...) 当我添加它时(最初我传入了RestClient bean,但现在我临时创建了一个本地对象,以便更清晰) 我得到这个java