Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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 @Autowired表示不建议现场注入_Java_Spring Boot_Javabeans_Autowired - Fatal编程技术网

Java @Autowired表示不建议现场注入

Java @Autowired表示不建议现场注入,java,spring-boot,javabeans,autowired,Java,Spring Boot,Javabeans,Autowired,有人能告诉我为什么@autowired说不推荐字段注入,而TextWriter对象“text”也说它不能自动连接,因为有多个TextWriter类型的bean 有人能告诉我为什么@autowired说现场注射不是 推荐 出于设计原因。将bean直接注入字段会隐藏您的依赖项并鼓励糟糕的设计: 类API public/protected成员存在时未指定它们。 如果没有反射或Spring容器(对我来说最重要的部分),就无法进行单元测试 您可以通过声明潜在的许多注入字段来完成。这可能会使您的类与其他类有

有人能告诉我为什么@autowired说不推荐字段注入,而TextWriter对象“text”也说它不能自动连接,因为有多个TextWriter类型的bean

有人能告诉我为什么@autowired说现场注射不是 推荐

出于设计原因。将bean直接注入字段会隐藏您的依赖项并鼓励糟糕的设计:

类API public/protected成员存在时未指定它们。 如果没有反射或Spring容器(对我来说最重要的部分),就无法进行单元测试 您可以通过声明潜在的许多注入字段来完成。这可能会使您的类与其他类有很强的耦合,而您并没有真正意识到这一点。 通常情况下,如果字段很少,则无需使用@Autowired对构造函数进行注释,否则应使用setter。 这两种方法都没有上面提到的所有缺点

有人能告诉我为什么@autowired说现场注射不是 推荐

出于设计原因。将bean直接注入字段会隐藏您的依赖项并鼓励糟糕的设计:

类API public/protected成员存在时未指定它们。 如果没有反射或Spring容器(对我来说最重要的部分),就无法进行单元测试 您可以通过声明潜在的许多注入字段来完成。这可能会使您的类与其他类有很强的耦合,而您并没有真正意识到这一点。 通常情况下,如果字段很少,则无需使用@Autowired对构造函数进行注释,否则应使用setter。
这两种方法都没有上面提到的所有缺点。

我假设Spring没有将TextWriter类视为bean。可能TextWriter在另一个包中。例如,在“模型”包中。如果您使用Spring Boot,那么这将帮助您:

@SpringBootApplication(scanBasePackages={"com.programwithwaqas.restservice.models"})
我建议您阅读注释。请注意注释@ComponentScan

这是在您不使用Spring Boot的情况下

@ComponentScan(basePackages={"com.programwithwaqas.restservice.models"})

我假设Spring不将TextWriter类视为bean。可能TextWriter在另一个包中。例如,在“模型”包中。如果您使用Spring Boot,那么这将帮助您:

@SpringBootApplication(scanBasePackages={"com.programwithwaqas.restservice.models"})
我建议您阅读注释。请注意注释@ComponentScan

这是在您不使用Spring Boot的情况下

@ComponentScan(basePackages={"com.programwithwaqas.restservice.models"})

请在这里添加您的代码不要使用链接,因为以后链接可能会消失,问题不再有用。加上代码太小了是的,请把你的代码作为文本而不是图像放在问题中请在这里添加你的代码不要使用链接,因为以后链接可能会消失,问题不再有用。加上代码太小了是的,请把你的代码作为文本而不是图像放在问题中虽然tho-OP没有显示实际的错误消息,但听起来似乎有几个bean实现了TextWriter接口,而不是没有。虽然tho-OP没有显示实际的错误消息,听起来似乎有几个bean实现了TextWriter接口,而不是一个都没有。所以您通过构造函数注入了Spring类调用的服务??!!java公共类MyClass{private Service1 Service1;private Service2 Service2;private Service3 Service3;private Service4 Service4;private Service5 Service5;private Service6 Service6;公共MyClassService1 Service1,Service2 Service2,Service3 Service3,Service4 Service4,Service5 Service5,Service6 Service6{this.service1=service1;this.service2=service2;this.service3=service3;this.service4=service4;this.service5=service5;this.service6=service6;}@Marc Le Bihan从不。请仔细阅读:一般情况下,应该支持构造函数注入,如果字段较少,则无需使用@Autowired对构造函数进行注释,否则应使用setter。因此,您可以通过构造函数注入Spring类调用的服务???java公共类MyClass{private Service1 Service1;private Service2 Service2;private Service3 Service3;private Service4 Service4;private Service5 Service5;private Service6 Service6;public MyClassService1 Service1,Service2 Service2,Service3 Service4 Service4,Service5 Service5,Service6 Service6{this.service1=service1;this.service2=service2;this.service3=service3;this.service4=service4;this.service5=service5;this.service6=service6;}@Marc Le Bihan从未使用过。请仔细阅读:一般情况下,应该支持构造函数注入,如果字段较少,则无需使用@Autowired对构造函数进行注释,否则应使用setter。