Announcement Announcement Module
Collapse
No announcement yet.
BeanCreationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BeanCreationException

    Hi,
    I am getting the following error , Please help me
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberEntryController' defined in ServletContext resource [/WEB-INF/reportingtool-servlet.xml]: Can't resolve reference to bean 'memberJdbc' while setting property 'insertMemberJdbc'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberJdbc' defined in ServletContext resource [/WEB-INF/reportingtool-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class [com.db.home.InsertMemberJdbc]: no default constructor found; nested exception is java.lang.NoSuchMethodException: com.db.home.InsertMemberJdbc.<init>()
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberJdbc' defined in ServletContext resource [/WEB-INF/reportingtool-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class [com.db.home.InsertMemberJdbc]: no default constructor found; nested exception is java.lang.NoSuchMethodException: com.db.home.InsertMemberJdbc.<init>()
    org.springframework.beans.FatalBeanException: Could not instantiate class [com.db.home.InsertMemberJdbc]: no default constructor found; nested exception is java.lang.NoSuchMethodException: com.db.home.InsertMemberJdbc.<init>()
    java.lang.NoSuchMethodException: com.db.home.InsertMemberJdbc.<init>()
    	at java.lang.Class.getConstructor0(Unknown Source)
    	at java.lang.Class.getDeclaredConstructor(Unknown Source)
    	at org.springframework.beans.BeanUtils.instantiateClass
    This is my MemberEntryController .java
    Code:
    public class MemberEntryController extends SimpleFormController {
    
    protected final Log logger = LogFactory.getLog(getClass());
        InsertMemberJdbc insertMember;
    	ProjectManagerJdbc jdbc;
    	public void setInsertMemberJdbc(InsertMemberJdbc jdbc)
    	{
    		this.insertMember=jdbc;
    	}
    	
    	public void setProjectManagerJdbc(ProjectManagerJdbc jdbc) {
    		this.jdbc = jdbc;
    	}
    
    	public ModelAndView onSubmit(Object command) throws ServletException {
    		TeamMember teamMember = new TeamMember();
    		int empNo=((TeamMember)command).getEmpNo();
    		String empName=((TeamMember)command).getEmpName();
    		String projectName = ((TeamMember)command).getProjectName();
    		int projectManagerId = ((TeamMember)command).getProjectManagerId();
    		int clusterManagerId = ((TeamMember)command).getClustureManagerId();
    		String location = ((TeamMember)command).getLocation();
    		String psaGrade = ((TeamMember)command).getPsaGrade();
    		insertMember.insert(empNo,empName,projectManagerId,clusterManagerId,location,psaGrade,projectName);
    		
    		logger.info("Success fully Inserted");
    		return new ModelAndView("home");
    	}
    
    	protected Object formBackingObject(HttpServletRequest request)
    			throws ServletException {
    		TeamMember teamMember = new TeamMember();
    		List list = new ArrayList();
    		try {
    			list = jdbc.getProjectManager();
    		} catch (Exception e) {
    			logger.error("Error in project MAnager  *******", e);
    			e.printStackTrace();
    		}
    		Iterator itr = list.iterator();
    		while(itr.hasNext()){
    			teamMember = (TeamMember)itr.next();
    			logger.info("ProjectManagerName---------"
    					+ teamMember.getProjectManagerName());
    			logger
    					.info("ProjectManagerID-----------"
    							+ teamMember.getProjectManagerId());
    		}
    		
    		return teamMember;
    	}
    }
    InsertMemberJdbc.java
    Code:
    public class InsertMemberJdbc extends SqlUpdate  {
    	protected final Log logger = LogFactory.getLog(getClass());	
       
    	public InsertMemberJdbc(DataSource ds)
    	{
    		setDataSource(ds);
    		logger.info("Before Insert");
    		setSql("insert into EMPLOYEE(EMP_NO,EMP_NAME,PROJECT_MANAGER,CLUSTER_MANAGER,LOCATION,PSA,PROJECT_NAME) values(?,?,?,?,?,?,?)");
    		logger.info("After Insert");
    		declareParameter(new SqlParameter(Types.NUMERIC));
    		declareParameter(new SqlParameter(Types.VARCHAR ));
    		declareParameter(new SqlParameter(Types.NUMERIC));
    		declareParameter(new SqlParameter(Types.NUMERIC));
    		declareParameter(new SqlParameter(Types.VARCHAR));
    		declareParameter(new SqlParameter(Types.VARCHAR));
    		declareParameter(new SqlParameter(Types.VARCHAR));
    		//declareParameter(new SqlParameter(Types.VARCHAR));
    	
    	}
    	public int insert(int emp_no,String emp_name,int project_manager,int cluster_manager,String location,String psa,String project_name)
    	{
    		Object[] params=new Object []{
    						new Integer(emp_no),
    						new String(emp_name),
    						new Integer(project_manager),
    						new Integer(cluster_manager),
    						new String(location),
    						new String(psa),
    						new String(project_name)};
    		
    		return update(params);
    	}
    and ---servlet.xml
    Code:
    <bean id="memberEntryController" class="com.home.MemberEntryController">
    		<property name="commandName"><value>teamMember</value></property>
            <property name="commandClass"><value>com.home.TeamMember</value></property>  
            <property name="formView"><value>NewMemberEntry</value></property> 
            <property name="successView"><value>NewMemberEntry.htm</value></property>  
             <property name="projectManagerJdbc">
            	<ref bean="managerJdbc"/>
            </property>
            <property name="insertMemberJdbc">
            	<ref bean="memberJdbc"/>
           	</property>
        </bean>
     <bean id="memberJdbc" class="com.db.home.InsertMemberJdbc">
        	<property name="dataSource">
    			<ref bean="dataSource"/>			    		
       		</property>
        </bean>

  • #2
    You specify to inject the datasource via a setter-method. However, your bean requires it to be specified in the constructor.
    Since no default constructor is defined, the bean cannot be instantiated.

    A solution would be to inject the datasource as constructor argument:

    Code:
    <bean id="memberJdbc" class="com.db.home.InsertMemberJdbc">
      <constructor-arg index="0">
        <ref bean="dataSource"/>                        
      </constructor-arg>
    </bean>

    Regards,
    Andreas

    Comment


    • #3
      Hi Andreas,
      I changed the servlet.xml to

      Code:
      <bean id="memberJdbc" class="com.db.home.InsertMemberJdbc">
        <constructor-arg index="0">
          <ref bean="dataSource"/>                        
        </constructor-arg>
      </bean>
      But it is not working. I am getting the below error after changing
      Code:
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberEntryController' defined in ServletContext resource [/WEB-INF/reportingtool-servlet.xml]: Can't resolve reference to bean 'memberJdbc' while setting property 'insertMemberJdbc'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberJdbc' defined in ServletContext resource [/WEB-INF/reportingtool-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: dataSource is required
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'memberJdbc' defined in ServletContext resource [/WEB-INF/reportingtool-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: dataSource is required
      org.springframework.dao.InvalidDataAccessApiUsageException: dataSource is required
      	at org.springframework.jdbc.object.RdbmsOperation.compile(RdbmsOperation.java:300)
      	at org.springframework.jdbc.object.RdbmsOperation.afterPropertiesSet(RdbmsOperation.java:281)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1059)

      Comment


      • #4
        Well, the configuration is ok. Based on your code I cannot explain this second error.

        Are you sure the invocation of setDataSource() in your constructor is not accidentially commented out? Maybe you could explicitly try to invoke super.setDataSource().

        If that does not help, I propose to debug the constructor to see what happens to the datasource.

        Regards,
        Andreas

        Comment


        • #5
          Hi,
          After invoked super.setDataSource(). After that it is working fine.
          Thanks for ur help.
          Can u please explain in detail why i should set explicitly.
          Regards
          Sasikanth

          Comment


          • #6
            I suspect you have the setter "setDataSource" redefined in your class, so that the datasource doesn't get set in the superclass. That explains why the "super" invocation works.

            Regards,
            Andreas

            Comment


            • #7
              Yes, suppouse if the setter "setDataSource" is not defined then i configured in servlet.xml as
              <constructor-arg index="0">
              <ref bean="dataSource"/>
              </constructor-arg>
              Then it works fine or not. This one is constuctor injection.

              If we set the datasource by setter method then it is setter Injection.
              I am very new to spring i.e the reason for lots of queries.
              Anyway thank you very much Andreas.

              Comment

              Working...
              X