所谓Spring应用上下文混合配置,指的是配置一部分在xml文件,一部分在java代码。这种方式不是很常见,常见的要么是纯xml文件配置,要么是纯java编程配置。纯java编程配置后续章节我们会详细讲解。关于这两种纯配置方式的比较,我们前面的章节已经提过。
package com.gxz;
import org.springframework.stereotype.Service;
@Service
public class StudentImp implements Student {
@Override
public String sayHi(String name) {
return "Hi," + name;
}
public StudentImp() {
super();
System.out.println("StudentImp()");
}
}
package com.gxz;
public interface Student {
String sayHi(String name);
}
package com.gxz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentController {
@Autowired
private Student student;
@ResponseBody
@RequestMapping("/")
public String sayHi() {
return "Hi!";
}
@ResponseBody
@RequestMapping(value="/say", params="name")
public String say(@RequestParam("name") String name) {
return student.sayHi(name);
}
public void setStudent(Student student) {
this.student = student;
}
public StudentController() {
super();
System.out.println("StudentController()");
}
}
package com.gxz;
import org.springframework.stereotype.Service;
@Service
public class ComputerStudent implements Student {
public String sayHi(String name) {
return "Hi, " + name + ".Happy to see again.";
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gxz.Student com.gxz.StudentController.student; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.gxz.Student] is defined: expected single matching bean but found 2: computerStudent,studentImp
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gxz.Student com.gxz.StudentController.student; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.gxz.Student] is defined: expected single matching bean but found 2: computerStudent,studentImp
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.gxz.Student] is defined: expected single matching bean but found 2: computerStudent,studentImp
若要解决这个异常,就必须告诉Spring,Spring在实例化bean StudentController时,为之注入依赖Student时明确指定是哪一个bean(computerStudent,studentImp二选一)。
@Autowired
@Qualifier("computerStudent")
private Student student;
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/rootContext.xml]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'studentImp' for bean class [com.gxz.StudentImp] conflicts with existing, non-compatible bean definition of same name and class [com.gxz.config.StudentImp]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'studentImp' for bean class [com.gxz.StudentImp] conflicts with existing, non-compatible bean definition of same name and class [com.gxz.config.StudentImp]
package com.gxz.config;
import org.springframework.stereotype.Service;
import com.gxz.Student;
@Service("configStudentImp")
public class StudentImp implements Student {
public String sayHi(String name) {
return "Hi, " + name + ".Happy to see again.";
}
}
private Student student;
@Autowired
public void setStudent(Student student) {
this.student = student;
}
private Student student;
@Autowired
public StudentController(Student student) {
super();
this.student = student;
System.out.println("StudentController() with argument.");
}
@Autowired
public StudentController(Student student) {
super();
this.student = student;
System.out.println("StudentController() with argument student.");
}
@Autowired
public StudentController( Teacher teacher) {
super();
this.teacher = teacher;
System.out.println("StudentController() with arguments teacher.");
}
org.springframework.beans.factory.BeanCreationException: Invalid autowire-marked constructor: public com.gxz.StudentController(com.gxz.Student). Found another constructor with 'required' Autowired annotation: public com.gxz.StudentController(com.gxz.Student,com.gxz.Teacher)