【问题标题】:bean is not creatingbean 没有创建
【发布时间】:2021-11-09 11:02:56
【问题描述】:
       package com.filter.service;
       import java.util.ArrayList;
         import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.stereotype.Service;

     import com.filter.dao.UserDao;

   @Service
  public class ServiceClass {

@Autowired
private UserDao userDao;

private ArrayList data(int testId)
{
    return this.userDao.fetchData(testId);
}        

}

           package com.filter.dao;
           import java.util.ArrayList;
          import org.hibernate.Session;
         import org.hibernate.SessionFactory;
          import org.hibernate.query.Query;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.orm.hibernate5.HibernateTemplate;
    import org.springframework.stereotype.Repository;

    import com.filter.model.TeSectionQue;

    @Repository
  public class UserDao {


      @Autowired
      SessionFactory sF;

       @Autowired
      HibernateTemplate hibernateTemplate;

@SuppressWarnings("deprecation")
public ArrayList fetchData(int testId)
{
    Session session = sF.openSession();
    ArrayList <TeSectionQue> userData=null;
    System.out.println("print");
    try {
        String SQL_QUERY="from te_section_que as 0 where 
     0.test_no=?";
        Query<?> query= session.createQuery(SQL_QUERY);
        
        userData=(ArrayList<TeSectionQue>)query.list();
        
    }catch(IndexOutOfBoundsException e)
    {
        System.out.println(e);
        System.out.println("exception not available");
        return null;
    }
    return userData;
  }

 }



package com.filter.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 import com.filter.service.ServiceClass;

 @Controller
 public class MainController {

   @Autowired
  ServiceClass serviceClass;

   @Autowired(required=true)
  @Qualifier(value="ServiceClass")
  public void setServiceClass(ServiceClass serviceClass){
    this.serviceClass = serviceClass;
  }

  @RequestMapping("/")
  public String view()
  {
    System.out.println("Test");
    return "index";
    }
   @RequestMapping(value="/testId", method= RequestMethod.POST)
   public String fetch(HttpServletRequest req)
  {
    String testId=req.getParameter("search");
    System.out.println(testId);
    
    
    
    
    
    
    
    
       return "";
    }
  }



          package com.filter.model;

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

   @Entity
  @Table(name="wet_user_response")
   public class User {

   @Column(name = "sno", nullable = false)
  @GeneratedValue(strategy = GenerationType.AUTO)
   private int sno;
  @Column(name = "test_pattern", nullable = false)
  private String test_pattern;
  @Column(name = "test_pattern", nullable = false)
   private String tlevel;

   @Column(name = "result_view", nullable = false)
   private String result_view;

   @Column(name = "bexam_name", nullable = false)
    private String bexam_name;

@Column(name = "bcomp_code", nullable = false)
private String bcomp_code;

@Column(name = "test_domain1", nullable = false)
private String test_domain1;

@Column(name = "RQNO1", nullable = false)
private String RQNO1;

@Column(name = "RANS1", nullable = false)
private String RANS1;

@Column(name = "RSEC1", nullable = false)
private String RSEC1;

@Column(name = "RMAK1", nullable = false)
private String RMAK1;

@Column(name = "qtext1", nullable = false)
private String qtext1;

@Column(name = "SEL_SELECTION1", nullable = false)
private String SEL_SELECTION1;

@Column(name = "email", nullable = false)
private String email;

@Column(name = "name1", nullable = false)
private String name1;

@Column(name = "exam_name", nullable = false)
private String exam_name;

@Column(name = "TestStartTime", nullable = false)
private String TestStartTime;

 @Column(name = "RQNO", nullable = false)
private String RQNO;

@Column(name = "RSEC", nullable = false)
private String RSEC;

@Column(name = "RMAK", nullable = false)
private String RMAK;

@Column(name = "RANS", nullable = false)
private String RANS;

@Column(name = "UANS", nullable = false)
private String UANS;

@Column(name = "wheeboxID", nullable = false)
private int wheeboxID;

@Column(name = "perQustionTimer", nullable = false)
private String perQustionTimer;

@Column(name = "assign_by", nullable = false)
private int assign_by;

@Column(name = "test_id", nullable = false)
private int test_id;

@Column(name = "cuk_id", nullable = false)
private int cuk_id;

@Column(name = "key_sno", nullable = false)
private int key_sno;

@Column(name = "questionsFlagged", nullable = false)
private String questionsFlagged;


public int getSno() {
    return sno;
}
public void setSno(int sno) {
    this.sno = sno;
}
public String getTest_pattern() {
    return test_pattern;
}
public void setTest_pattern(String test_pattern) {
    this.test_pattern = test_pattern;
}
public String getTlevel() {
    return tlevel;
}
public void setTlevel(String tlevel) {
    this.tlevel = tlevel;
}
public String getResult_view() {
    return result_view;
}
public void setResult_view(String result_view) {
    this.result_view = result_view;
}
public String getBexam_name() {
    return bexam_name;
}
public void setBexam_name(String bexam_name) {
    this.bexam_name = bexam_name;
}
public String getBcomp_code() {
    return bcomp_code;
}
public void setBcomp_code(String bcomp_code) {
    this.bcomp_code = bcomp_code;
}
public String getTest_domain1() {
    return test_domain1;
}
public void setTest_domain1(String test_domain1) {
    this.test_domain1 = test_domain1;
}
public String getRQNO1() {
    return RQNO1;
}
public void setRQNO1(String rQNO1) {
    RQNO1 = rQNO1;
}
public String getRANS1() {
    return RANS1;
}
public void setRANS1(String rANS1) {
    RANS1 = rANS1;
}
public String getRSEC1() {
    return RSEC1;
}
public void setRSEC1(String rSEC1) {
    RSEC1 = rSEC1;
}
public String getRMAK1() {
    return RMAK1;
}
public void setRMAK1(String rMAK1) {
    RMAK1 = rMAK1;
}
public String getQtext1() {
    return qtext1;
}
public void setQtext1(String qtext1) {
    this.qtext1 = qtext1;
}
public String getSEL_SELECTION1() {
    return SEL_SELECTION1;
}
public void setSEL_SELECTION1(String sEL_SELECTION1) {
    SEL_SELECTION1 = sEL_SELECTION1;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getName1() {
    return name1;
}
public void setName1(String name1) {
    this.name1 = name1;
}
public String getExam_name() {
    return exam_name;
}
public void setExam_name(String exam_name) {
    this.exam_name = exam_name;
}
public String getTestStartTime() {
    return TestStartTime;
}
public void setTestStartTime(String testStartTime) {
    TestStartTime = testStartTime;
}
public String getRQNO() {
    return RQNO;
}
public void setRQNO(String rQNO) {
    RQNO = rQNO;
}
public String getRSEC() {
    return RSEC;
}
public void setRSEC(String rSEC) {
    RSEC = rSEC;
}
public String getRMAK() {
    return RMAK;
}
public void setRMAK(String rMAK) {
    RMAK = rMAK;
}
public String getRANS() {
    return RANS;
}
public void setRANS(String rANS) {
    RANS = rANS;
}
public String getUANS() {
    return UANS;
}
public void setUANS(String uANS) {
    UANS = uANS;
}
public int getWheeboxID() {
    return wheeboxID;
}
public void setWheeboxID(int wheeboxID) {
    this.wheeboxID = wheeboxID;
}
public String getPerQustionTimer() {
    return perQustionTimer;
}
public void setPerQustionTimer(String perQustionTimer) {
    this.perQustionTimer = perQustionTimer;
}
public int getAssign_by() {
    return assign_by;
}
public void setAssign_by(int assign_by) {
    this.assign_by = assign_by;
}
public int getTest_id() {
    return test_id;
}
public void setTest_id(int test_id) {
    this.test_id = test_id;
}
public int getCuk_id() {
    return cuk_id;
}
public void setCuk_id(int cuk_id) {
    this.cuk_id = cuk_id;
}
public int getKey_sno() {
    return key_sno;
}
public void setKey_sno(int key_sno) {
    this.key_sno = key_sno;
}
         public String getQuestionsFlagged() {
          return questionsFlagged;
}
     public void setQuestionsFlagged(String questionsFlagged) {
        this.questionsFlagged = questionsFlagged;
}

}

   <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans- 
             4.0.xsd
            http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context- 
            4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <tx:annotation-driven/>

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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    name="viewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

 <bean name="ds" 
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>  
    <property name="url" value="jdbc:mysql://localhost:3306/system11"></property>  
    <property name="username" value="root"></property>  
    <property name="password" value="root"></property>  
</bean> 


<bean
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
    name="factory">
    <property name="dataSource" ref="ds"></property>
    <property name="hibernateProperties">
        <props>
            <prop 
   
 key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hdm2ddl.auto">update</prop>

        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>
                com.filter.model.User
            </value>
            <value>
                com.filter.model.TeSectionQue
            </value>
            <value>com.filter.model.TeQuestionBank</value>
        </list>
    </property>
</bean>

<bean
    class="org.springframework.orm.hibernate5.HibernateTemplate"
    name="hibernateTemplate">
    <property name="sessionFactory" ref="sessionfactory"></property>
</bean>

<bean class="com.filter.model.User" name="userDao">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>

</bean>

<bean
    class="org.springframework.orm.hibernate5.HibernateTransactionManager"
    name="transactionManager">
    <property name="sessionFactory" ref="sessionfactory"></property> 
</bean>

错误消息======

2021 年 9 月 14 日下午 3:09:33 org.springframework.context.support.AbstractApplicationContext 刷新 警告:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“mainController”的bean时出错:通过字段“serviceClass”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.filter.service.ServiceClass”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 2021 年 9 月 14 日下午 3:09:33 org.springframework.web.servlet.FrameworkServlet initServletBean 严重:上下文初始化失败 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“mainController”的bean时出错:通过字段“serviceClass”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.filter.service.ServiceClass”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 在 org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) 在 org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) 在 org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) 在 org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) 在 org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) 在 org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) 在 javax.servlet.GenericServlet.init(GenericServlet.java:158) 在 org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1143) 在 org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1096) 在 org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:767) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:128)

【问题讨论】:

  • 嗨,请将您的问题格式化为可读并发布您的一些代码,例如这个userDao 类/接口
  • 请远程控制我的系统并解决这个问题...
  • 对不起,但这不是它的工作原理。您必须发布一个有意义的问题并提供代表您面临的问题的代码示例。好的问题会得到好的答案,如果您不编辑问题并添加所有必需的信息,您将不会得到任何答案。
  • 我问你的很简单,请编辑你的问题并粘贴UserDao代码。
  • 请看我的所有代码

标签: hibernate spring-mvc


【解决方案1】:

错误

SEVERE: Context initialization failed 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'mainController': Unsatisfied dependency expressed 
through field 'serviceClass'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type 'com.filter.service.ServiceClass' available: 
expected at least 1 bean which qualifies as autowire candidate. 

在您的 MainController 中,您正在“自动装配”ServiceClass

  • 这里(使用字段serviceClass):

     @Autowired
     ServiceClass serviceClass;
    
  • 这里(使用setter方法setServiceClass):

      @Autowired(required=true)
      @Qualifier(value="ServiceClass")
      public void setServiceClass(ServiceClass serviceClass){
          this.serviceClass = serviceClass;
      }
    

如果您不使用 @Qualifier(value="ServiceClass") 进行注释,它会起作用,但是最好完全删除 ServiceClass 的第二个“自动装配”。

换句话说,在你的MainController 类中只保留一个@Autowired

@Autowired
ServiceClass serviceClass;

并删除 setter setServiceClass 及其所有注释。

【讨论】:

  • 它没有工作。我试了很多次。
  • @suficst 它应该可以工作,除非您有其他错误,请编辑您的问题,添加修改后的MainController 类并添加新的错误堆栈跟踪
  • 解决问题
  • @suficst 不错!如果我的回答帮助您解决了您的问题,请考虑投票/检查是否正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 2011-10-21
相关资源
最近更新 更多