Java Can';即使依赖关系由Spring容器管理,也不能使用autowire服务
我一直在修改我的类,以避免Java Can';即使依赖关系由Spring容器管理,也不能使用autowire服务,java,spring,spring-mvc,Java,Spring,Spring Mvc,我一直在修改我的类,以避免StudentService(让Spring的容器管理)上的new操作符出现在我的控制器中。我需要学生字段中注入了StudentService,但没有注入学生,而是注入了一个异常,表示: 未定义[com.Student]类型的合格bean:预期为单个匹配bean,但找到2:studentService,ss 我想把这个程序打印出来 姓名:布拉沃 学生: package com; public interface Student { public String g
StudentService
(让Spring的容器管理)上的new
操作符出现在我的控制器中。我需要学生代码>字段中注入了StudentService
,但没有注入学生
,而是注入了一个异常,表示:
未定义[com.Student]类型的合格bean:预期为单个匹配bean,但找到2:studentService,ss
我想把这个程序打印出来
姓名:布拉沃
学生:
package com;
public interface Student {
public String getN();
}
学生服务:
package com;
import org.springframework.stereotype.Service;
@Service
public class StudentService implements Student{
String name;
@Override
public String getN() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
控制器
package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TheController {
@Autowired
private ApplicationContext appContext;
@Autowired
Student student;
@RequestMapping("/")
public String aMethod() {
StudentService ss = (StudentService) appContext.getBean("ss");
ss.setName("bravo");
System.out.println("NAME : " + student.getN());
// while "NAME : " + ss.getN() will work, of course
return "";
}
}
调度器servlet
...
<context:component-scan base-package="com" />
...
<bean id="ss" class="com.StudentService" />
...
而这个(在控制器中)
没有例外,但它会产生
名称:空
如果我去掉@Service
并将StudentService
加载为
,然后通过应用程序上下文手动加载它,那么它就会工作
我是说,@服务
怎么不能自动连线让我们看看aMethod
@RequestMapping("/")
public String aMethod() {
StudentService ss = (StudentService) appContext.getBean("ss");
ss.setName("bravo");
System.out.println("NAME : " + student.getN());
// while "NAME : " + ss.getN() will work, of course
return "";
}
在这个方法中,您正在创建一个名为ss
的StudentService
对象。就在下面,调用setName
interface方法,将StudentService
name设置为bravo。但是,当调用student.getN()
时,您正在检索另一个对象的名称
要解决此问题,您可以检索由StudentService
管理的Student
对象:
@Service
public class StudentService {
@Autowired
Student student;
public Student getStudent() {
return student;
}
public void setName() {
this.student.setName(Alfred);
}
}
但出于特殊性考虑,我建议将两者分离,只使用您的学生的setName()
,而不使用服务。让我们看看aMethod
@RequestMapping("/")
public String aMethod() {
StudentService ss = (StudentService) appContext.getBean("ss");
ss.setName("bravo");
System.out.println("NAME : " + student.getN());
// while "NAME : " + ss.getN() will work, of course
return "";
}
在这个方法中,您正在创建一个名为ss
的StudentService
对象。就在下面,调用setName
interface方法,将StudentService
name设置为bravo。但是,当调用student.getN()
时,您正在检索另一个对象的名称
要解决此问题,您可以检索由StudentService
管理的Student
对象:
@Service
public class StudentService {
@Autowired
Student student;
public Student getStudent() {
return student;
}
public void setName() {
this.student.setName(Alfred);
}
}
但出于特殊性考虑,我建议将两者分离,只使用学生的setName()
,而不使用服务。Spring知道两种以上的方法来声明“bean”(它可以用来注入的对象)。一个是xml:
<bean id="ss" class="com.StudentService" />
以及@Component
和其他一些组件将导致spring将附加到的类视为bean。名称将从类名派生:它只是小写。它还将new
该类型的实例,并处理所有依赖项。XML和@注释配置的bean都可以在应用程序上下文中使用
在您的例子中,您以两种不同的方式告诉spring做同样的事情:您希望将com.StudentService
作为bean提供,一次通过xml显式命名为“ss”
,一次在java中隐式命名为“StudentService”
现在,当您在控制器中请求与类型Student
匹配的bean时,如下所示
@Autowired
Student student;
它有两个同样优秀的候选人,但无法决定注入哪一个。这是您当前的例外情况
为了解决这个问题,您可以只声明1个bean(最好),或者通过提供名称来限定您所指的bean。比如说
@Autowired
@Qualifier("ss")
Student student;
这几乎就是你以后要做的
StudentService ss = (StudentService) appContext.getBean("ss");
当你把两者结合起来的时候
@Autowired
@Qualifier("st")
Student student;
...
StudentService ss = (StudentService) appContext.getBean("ss");
ss.setName("bravo");
System.out.println("NAME : " + student.getN());
现在,您有了两个独立的bean/实例StudentService
,它们都是“单例”(wrt它们的bean名称)。设置其中一个的名称,然后读取另一个的名称。预期结果是“st”
bean的名称仍然为空。Spring知道两种以上的方法来声明“bean”(它可以用来注入的对象)。一个是xml:
<bean id="ss" class="com.StudentService" />
以及@Component
和其他一些组件将导致spring将附加到的类视为bean。名称将从类名派生:它只是小写。它还将new
该类型的实例,并处理所有依赖项。XML和@注释配置的bean都可以在应用程序上下文中使用
在您的例子中,您以两种不同的方式告诉spring做同样的事情:您希望将com.StudentService
作为bean提供,一次通过xml显式命名为“ss”
,一次在java中隐式命名为“StudentService”
现在,当您在控制器中请求与类型Student
匹配的bean时,如下所示
@Autowired
Student student;
它有两个同样优秀的候选人,但无法决定注入哪一个。这是您当前的例外情况
为了解决这个问题,您可以只声明1个bean(最好),或者通过提供名称来限定您所指的bean。比如说
@Autowired
@Qualifier("ss")
Student student;
这几乎就是你以后要做的
StudentService ss = (StudentService) appContext.getBean("ss");
当你把两者结合起来的时候
@Autowired
@Qualifier("st")
Student student;
...
StudentService ss = (StudentService) appContext.getBean("ss");
ss.setName("bravo");
System.out.println("NAME : " + student.getN());
现在,您有了两个独立的bean/实例StudentService
,它们都是“单例”(wrt它们的bean名称)。设置其中一个的名称,然后读取另一个的名称。预期结果是“st”
bean的名称仍然为空。HI的可能重复如何限定它?它们是类的StudentService
,xml的ss
。您可以进行基于xml的配置,也可以进行基于java的配置@Service
使其成为bean如果服务bean是通过xml声明的,那么它将起作用,实际上我需要找到的是,如果这个@Autowired
字段通过简单的@Autowired
注释就知道StudentService,因为StudentService
是用@Service
注释的,并且对于合格的bean部分,我不知道如何限定它们,因为一个是class
,而另一个是xml
请添加配置文件,可能是组件扫描问题