user3244519
user3244519

Reputation: 681

Getting NoSuchBeanDefinitionException while Referencing JavaConfig in XML configuration

I am beginner in Spring and trying to learn different ways of configuring Spring beans using XML and Java Config classses. In below demo program, i have configured EmployeeService and EmployeeDAO beans in Java Config classes and Employee bean in XML file. And then i am trying to Reference JavaConfig in XML configuration and use this XML configuration file in Main Class to get the Employee Service bean. I am getting NoSuchBeanDefinitionException while executing the below program. Can someone please help me to understand what is wrong here.

DAOConfig.java package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.demo.dao.EmployeeDAO;

@Configuration
public class DAOConfig {

    @Bean
    public EmployeeDAO getEmployeeDAO(){
        return new EmployeeDAO();
    }

}

ServiceConfig.java package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.demo.dao.EmployeeDAO;
import com.demo.service.EmployeeService;

@Configuration
public class ServiceConfig {

    @Bean(name="myEmployeeService")
    public EmployeeService getEmployeeService(EmployeeDAO empDAO){
        EmployeeService empService = new EmployeeService();
        empService.setEmpDAO(empDAO);
        return empService;
    }
}

MainConfig.java

package com.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ DAOConfig.class, ServiceConfig.class })
public class MainConfig {

}

EmployeeService.java:

package com.demo.service;

import com.demo.dao.EmployeeDAO;

public class EmployeeService {

    private EmployeeDAO empDAO;

    public void setEmpDAO(EmployeeDAO empDAO) {
        this.empDAO = empDAO;
    }

    public void insertEmployee() {
        System.out.println("insertEmployee called..");
        empDAO.insertEmployeeDetails();
    }

    public void updateEmployee() {
        System.out.println("updateEmployee called..");
        empDAO.updateEmployeeDetails();
    }

    public void deleteEmployee() {
        System.out.println("deleteEmployee called..");
        empDAO.deleteEmployeeDetails();
    }

}

EmployeeDAO.java

    package com.demo.dao;

public class EmployeeDAO {

    public void insertEmployeeDetails(){
        System.out.println("insertEmployeeDetails called..");
    }

    public void updateEmployeeDetails(){
        System.out.println("updateEmployeeDetails called..");
    }

    public void deleteEmployeeDetails(){
        System.out.println("deleteEmployeeDetails called..");
    }

}

Employee.java:

package com.demo.dto;

public class Employee {

    private String name;
    private String id;
    private String salary;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + ", salary=" + salary + "]";
    }
}

mixAppContext.xml:

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean class="com.demo.config.MainConfig" />

    <bean id="employeeObj" class="com.demo.dto.Employee">
        <property name="name" value="ABC"></property>
        <property name="id" value="123"></property>
        <property name="salary" value="10000"></property>
    </bean>

</beans>   

JavaConfigInXmlMixDemo.java:

package com.demo.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.dto.Employee;
import com.demo.service.EmployeeService;

public class JavaConfigInXmlMixDemo {

    public static void main(String args[]){
        //ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        ApplicationContext context = new ClassPathXmlApplicationContext("mixAppContext.xml");

        EmployeeService service =     (EmployeeService) context.getBean("myEmployeeService");
        Employee employee = (Employee) context.getBean("employeeObj");

        service.insertEmployee();
        service.updateEmployee();
        service.deleteEmployee();

        System.out.println(employee);
    }

}

I am getting below exception:

Exception:

INFO: Loading XML bean definitions from class path resource [mixAppContext.xml]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myEmployeeService' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:701)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
    at com.demo.main.JavaConfigInXmlMixDemo.main(JavaConfigInXmlMixDemo.java:15)

Upvotes: 1

Views: 301

Answers (1)

aggredi
aggredi

Reputation: 426

Like described here you need include <context:annotation-config /> in your xml. Without this tag spring will ignore all annotation and create instance of your configuration bean as it's a simple bean.

Upvotes: 1

Related Questions