英文:
Spring autowired difference between class field and method
问题
以下是翻译好的部分:
"我是Spring框架的新手,正在尝试弄清楚在类的字段和类的方法上使用Spring的@Autowired注解之间的区别,就像下面的IUtil和IUserDao一样:
@Controller
@RequestMapping("/user")
public class UserController{
@Autowired
private IUtil util;
private IUserDao userDao;
@Autowired
public UserController(IUserDao dao){
this.userDao = dao;
}
...
...
...
}"
"IUtil是通过字段注入的,而IUserDao是通过构造函数注入的,它们之间有什么区别?
提前感谢。"
英文:
I'm new to Spring framework, and try to figure out what is the difference between spring autowired at class's field and class's method, like below's IUtil and IUserDao:
> @Controller
> @RequestMapping("/user")
> public class UserController{
>
> @Autowired
> private IUtil util;
>
> private IUserDao userDao;
>
> @Autowired
> public UserController(IUserDao dao){
> this.userDao = dao;
> }
>
> ...
> ...
> ...
> }
IUtil is inject by field, and IUserDao is inject by constructor, what's the difference between them?
Thanks in advance.
答案1
得分: 0
"注射的时间点" 不同;
考虑以下代码:
public class Index {
public static void main(String[] args) {
ApplicationContext AcContext = new AnnotationConfigApplicationContext(MyConfigration.class);
}
}
@Configuration
class MyConfigration {
@Bean
public UserController userController() { return new UserController(); }
@Bean
public IUtil util() { return new Util(); }
@Bean
public IUserDao userDao() { return new UserDao(); }
}
interface IUtil {
public String getDate();
}
interface IUserDao {}
class Util implements IUtil {
public String date = "2023-06-26";
@Override
public String getDate() {
return date;
}
}
class UserDao implements IUserDao {}
class UserController {
@Autowired
private IUtil util;
private String date;
public UserController(){
this.date = util.getDate();
}
}
思考一下,执行这段代码,会发生什么?
答案是在 `this.date = util.getDate();` 处会发生空指针异常。
因为在通过类的属性进行注入时,是在类实例化之后进行的。
回到你的示例,在你的UserController初始化中,你可以对userDao做一些操作,但在初始化时不能使用util。
<details>
<summary>英文:</summary>
The "time point" of injection is different;
Consider the following code:
public class Index {
public static void main(String[] args) {
ApplicationContext AcContext = new AnnotationConfigApplicationContext(MyConfigration.class);
}
}
@Configuration
class MyConfigration {
@Bean
public UserController userController() { return new UserController(); }
@Bean
public IUtil util() { return new Util(); }
@Bean
public IUserDao userDao() { return new UserDao(); }
}
interface IUtil {
public String getDate();
}
interface IUserDao {}
class Util implements IUtil {
public String date = "2023-06-26";
@Override
public String getDate() {
return date;
}
}
class UserDao implements IUserDao {}
class UserController {
@Autowired
private IUtil util;
private String date;
public UserController(){
this.date = util.getDate();
}
}
Think about it, execute this code, what will happen?
The answer is that a null pointer exception will occur when `this.date = util.getDate();`
Because when injecting through the properties of the class, it is after the class is instantiated.
Going back to your example, in your example, in your UserController initialization, you can do something with userDao, but in initialization you cannot use util
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论