Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';即使依赖关系由Spring容器管理,也不能使用autowire服务_Java_Spring_Spring Mvc - Fatal编程技术网

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
请添加配置文件,可能是组件扫描问题