自动装配不在弹簧控制器中工作

时间:2015-10-20 05:41:15

标签: spring hibernate spring-mvc spring-annotations hibernate-annotations

我总体上谷歌,但无法弄清楚我的代码中有什么错,以及为什么它不会自动装配。

****员工控制员****

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rectrix.eob.master.BankMaster;
import com.rectrix.eob.service.BankMasterService;
import com.rectrix.eob.service.impl.BankMasterServiceImpl;

@Controller
@RequestMapping(value="/")
@Scope(value="request")
public class EmployeeController  {

    @Autowired
    BankMasterService bankMasterService;


    @Autowired
    BankMaster bankMaster;



    public EmployeeController() {
        super();
        // TODO Auto-generated constructor stub
    }
    static Logger log = Logger.getLogger(EmployeeController.class.getName());

    @RequestMapping(value="*")
    public @ResponseBody String bankData(HttpServletRequest request, HttpServletResponse response){
    BankMaster bankMaster = new BankMaster();
    bankMaster.setName("kotak");
        bankMasterService.saveBankName(bankMaster);
        return "i am in bank";
    }

}

BANK MASTER MODEL

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;    
import javax.persistence.Table;

@Entity
@Table(name="TB_BANK_MASTER")
public class BankMaster implements Serializable {

    @Id
    @GeneratedValue
    @Column(name="BANK_ID")
    private Long bankId;

    @Column(name="BANK_NAME")
    private String name;

public BankMaster() {
    // TODO Auto-generated constructor stub
}

    public Long getBankId() {
        return bankId;
    }

    public void setBankId(Long bankId) {
        this.bankId = bankId;
    }

    /**
     * get the bank name
     * @return String
     */
    public String getName() {
        return name;
    }

    /**
     * set the bank name    
     * @param bankName
     */
    public void setName(String name) {
        this.name = name;
    }
}

申请背景

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

    <context:annotation-config />
    <context:component-scan base-package="com.rectrix.eob"></context:component-scan>

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:system.properties" />
    </bean>
    <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    <bean
        class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

    <!-- MYSQL HIBERNATE CONFIGURATION  -->

    <jee:jndi-lookup jndi-name="${jndi.datasource}" id="dataSource"
        expected-type="javax.sql.DataSource"></jee:jndi-lookup>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.rectrix.eob" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>


    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"></bean>

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

    <task:annotation-driven />

</beans>

****错误我得到的是****

  

11:05:32,579错误   [org.apache.catalina.core.ContainerBase [jboss.web] [缺省主机]。[/ EOB-幅] [弹簧-MVC]]   (http-localhost / 127.0.0.1:8080-1)JBWEB000236:Servlet.service()for   servlet spring-mvc抛出异常:   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   找到[com.rectrix.eob.master.BankMaster]类型的限定bean   依赖:预计至少有1个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

请帮助我找到我面临的问题的解决方案。 提前致谢

0 个答案:

没有答案