我发现我的构造函数开始看起来像这样:
public MyClass(Container con, SomeClass1 obj1, SomeClass2, obj2.... )
Run Code Online (Sandbox Code Playgroud)
随着参数列表不断增加.由于"容器"是我的依赖注入容器,为什么我不能这样做:
public MyClass(Container con)
Run Code Online (Sandbox Code Playgroud)
每个班级?有什么缺点?如果我这样做,感觉我正在使用一个美化的静电.请分享您对IoC和依赖注入疯狂的看法.
c# java dependency-injection ioc-container inversion-of-control
我在一些帖子中读过,Spring MVC并Portlets建议不要进行现场注射.因为我试图得到一个所以我问自己我是否使用现场注射,我无法回答它.据我所知场注入是,如果你注入一个Bean与一个属性@Autowired是这样的:
CartController.java:
...
@Autowired
private Cart cart;
...
Run Code Online (Sandbox Code Playgroud)
BookshopConfiguartion.java:
@Configuration
public class BookShopConfiguration {
@Bean
public Cart cart(){
return new Cart();
}
//more configuration
Run Code Online (Sandbox Code Playgroud)
我Cart.java习惯于在购物车中存储和提供有关书籍的信息.
在我的研究期间,我读到了构造函数注入:
MyComponent.java:
...
public class MyComponent{
private Cart cart;
@Autowired
public MyComponent(Cart cart){
this.cart = cart;
}
...
Run Code Online (Sandbox Code Playgroud)
这两种注射的优点和缺点是什么?
编辑1:由于此问题被标记为此问题的重复,我检查了它.因为在问题和答案中都没有任何代码示例,我不清楚我是否正确猜测我正在使用哪种注射类型.
在Pro Spring 3 Book中,第4章 - 介绍IOC和DI在春天 - 第59页,在"Setter Injection vs. Constructor Injection"部分,一段说
包含Spring,提供了一种机制,用于确保在使用Setter Injection时定义所有依赖项,但是通过使用Constructor Injection,您可以以容器无关的方式声明对依赖项的要求"
你能解释一下例子吗?
我的团队正在转向Spring 3.0,有些人想要开始将所有内容都移到Annotations中.当我看到一个类似这样的方法的类时,我的肠道感觉非常糟糕(代码味道?):(只是一个例子 - 不是所有真正的注释)
@Transaction
@Method("GET")
@PathElement("time")
@PathElement("date")
@Autowired
@Secure("ROLE_ADMIN")
public void manage(@Qualifier('time')int time) {
...
}
Run Code Online (Sandbox Code Playgroud)
我只是落后于时代,还是这对所有人来说都是一个可怕的想法?而不是使用诸如继承和多态的OO概念,现在通过约定或通过注释来实现一切.我只是不喜欢它.必须重新编译所有代码来改变IMO配置的东西似乎是错误的.但它似乎是一切(特别是春天)的方式.我应该"克服它"还是应该推回并尝试尽可能地保留我们的代码作为注释?
anotate @Autowired与某个属性或在setter中执行此操作有什么区别?
据我所知他们都有相同的结果,但有没有理由使用一个而不是另一个?
更新(更简洁)
这有什么区别
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
@Autowired
private SpellChecker spellChecker;
public TextEditor() {
System.out.println("Inside TextEditor constructor." );
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
Run Code Online (Sandbox Code Playgroud) 我怀疑使用Spring将对象注入到类中.我在我的项目中使用了这种代码:
@Resource // or @Autowired even @Inject
private PersonRepository personRepository;
Run Code Online (Sandbox Code Playgroud)
然后在方法上正常使用它:
personRepository.save(p);
Run Code Online (Sandbox Code Playgroud)
否则我在Spring示例中找到了注入构造函数:
private final PersonRepository personRepository;
@Autowired
public PersonController(PersonRepository personRepository) {
this.personRepository = personRepository;
}
Run Code Online (Sandbox Code Playgroud)
那两个都是正确的?或者每个都有它的属性和用法?
什么时候在春天使用构造函数注入?
我听说,在使用组件之前绝对必须拥有依赖类的实例时,构造函数注入特别有用.
这是什么意思?任何机构都能用例子来解释.
使用结构注射我会得到什么好处?
什么是动态构造函数注入?
我是Spring Boot和依赖注入的初学者,我无法理解Spring Boot中基于构造函数的依赖注入。我上课的ParameterDate样子是这样的:
public class ParameterDate {
private Date parameterDateUnadjusted;
private Date parameterDateAdjusted;
private Date parameterDateAdded;
private Date parameterDateChanged;
}
Run Code Online (Sandbox Code Playgroud)
我还有另一个要使用的课程ParameterDate。通常我会使用
@Autowired
ParameterDate parameterDate;
Run Code Online (Sandbox Code Playgroud)
在需要的地方我只是用parameterDate。
我如何使用基于构造函数的注入来做到这一点?
我正在从 spring 属性文件中读取一个文件路径,如下所示,并映射到一个 java 类中
#file path
file.path =/root/ms_data/file/
Run Code Online (Sandbox Code Playgroud)
它被映射到这样的java类中
@Value("${file.path}")
private String fileDataPath;
Run Code Online (Sandbox Code Playgroud)
请告知我是否可以将其保留为最终我的意思是如下所示,其中我将 fileDataPath 保留为最后一个
@Value("${file.path}")
private final String fileDataPath;
Run Code Online (Sandbox Code Playgroud) 我了解到自动装配bean实际上不是一个好习惯。春季文档中也提到了这一点。我知道有两种类型的bean配置。一种是通过XML配置,另一种是通过Java配置类。如果我们想使用代码进行bean配置而不使用@autowired,我们该怎么做?似乎如果使用代码,我们仍然需要@autowired才能注入bean?
例如在下面,如果我们不想使用@Autowired,我们该怎么做?最佳实践应该是什么?
@Service
public class ClassA {
private ClassB classB;
@Autowired
public ClassA(ClassB classB) {
this.classB = classB;
}
}
Run Code Online (Sandbox Code Playgroud) java ×9
spring ×7
spring-boot ×2
annotations ×1
autowired ×1
c# ×1
coding-style ×1
portlet ×1
required ×1
spring-mvc ×1