Spring自动装配(Autowired)的类字段和方法之间的区别

huangapple go评论67阅读模式
英文:

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 &quot;time point&quot; 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 = &quot;2023-06-26&quot;;
        @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>



huangapple
  • 本文由 发表于 2023年6月26日 11:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定