1

我有这个问题,我正在使用 Spring、Hibernate、JPA 和无服务器框架。当我从控制台调用类时,出现此错误“

"errorMessage": "No bean named 'wizardDao' is defined",
    "errorType": "org.springframework.beans.factory.NoSuchBeanDefinitionException",
    "stackTrace": [
        "org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:694)",
        "org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)",
        "org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)",
        "org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)",
        "org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)",
        "com.serverless.dao.Handler.handleRequest(Handler.java:30)",
        "com.serverless.dao.Handler.handleRequest(Handler.java:17)"

但是如果我用我的主类证明 Eclipse 中的类,那就没有问题了。我不知道出了什么问题这是我的 applicationContext

<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:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <context:component-scan base-package="com.serverless" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="Things-serverless" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <task:scheduler id="taskScheduler" />
    <task:executor id="taskExecutor" pool-size="1" />
    <task:annotation-driven executor="taskExecutor"
        scheduler="taskScheduler" />
    <!-- <bean id="serviceBean" class="com.serverless.test" /> -->

</beans>

这是我的道课

@Component
public class WizardDao extends ForwardedMessageDao {
    private static final long serialVersionUID = 5302145657830590489L;

    /**
     * Get all wizards from data base.
     * 
     * @param
     * @return List of wizards
     */
    @SuppressWarnings("unchecked")
    public List<Wizard> getAllWizards() {
        Query query = getEmf().createQuery("FROM Wizard");
        System.out.println("aqui ");
        return (List<Wizard>) query.getResultList();
}
}

这是我的处理程序类

@Service
@Transactional
public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {

    private static final Logger LOG = Logger.getLogger(Handler.class);

    @Autowired
    private WizardDao wizardDao;

    @Override
    public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
        BasicConfigurator.configure();
        Map<String, Wizard> response = new HashMap<>();
        Response responseBody = new Response("start", response);
        try {
            wizardDao = Application.getBean(WizardDao.class);
            List<Wizard> listWizard = wizardDao.getAllWizards();
            for (Wizard wizard : listWizard) {
                response.put("" + wizard.getIdWizard(), wizard);
            }
            responseBody.setInput(response);
        } catch (Exception e) {
            responseBody.setMessage("Fail: " + e.getMessage());
        }
        LOG.info("received: " + input);
        return ApiGatewayResponse.builder().setStatusCode(200).setObjectBody(responseBody)
                .setHeaders(Collections.singletonMap("fur", "this sheep")).build();
    }
}

这是我的 entityManager 类

public class EntityManagerDao implements Serializable {
    private static final long serialVersionUID = -6969245506190987187L;

    @PersistenceContext(unitName = "Things-serverless")
    private EntityManager em;

    /**
     * @return the em
     */
    public EntityManager getEmf() {
        return em;
    }

}
4

0 回答 0