我是Spring的初学者,试图学习使用XML和Java Config类配置Spring Bean的不同方法。在下面的演示程序中,我在Java Config类中配置了EmployeeService和EmployeeDAO bean,在XML文件中配置了Employee bean。然后,我尝试在XML配置中引用JavaConfig并在Main Class中使用此XML配置文件来获取Employee Service bean。执行以下程序时,我收到NoSuchBeanDefinitionException。有人可以帮我了解这里有什么问题吗。
DAOConfig.java 包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 包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
包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);
}
}
我正在接受以下例外:
例外:
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)
答案 0 :(得分:1)
就像描述here一样,您需要在XML中包含<context:annotation-config />
。没有这个标签,spring将忽略所有注释,并创建配置bean的实例,因为它是一个简单的bean。