我应该永远不要在Spring Boot项目上使用'new'关键字吗? [英] Should i never use 'new' keyword on a spring boot project?

查看:245
本文介绍了我应该永远不要在Spring Boot项目上使用'new'关键字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot Rest API,最终我在这里和那里都使用了 new 关键字。



我想知道,当我在程序中使用new关键字时,我做错了什么吗?如果绝对禁止在实际项目中使用new关键字。



如果答案是肯定的,我应该使用 @component 注释我编写的每个类,以便我可以实例化一个对象使用 @autowired



如果答案是否定的,我们何时才能打破该规则?

解决方案

您可以在spring应用程序中使用new关键字创建对象。



但是这些对象



由于它们不是Spring托管的,因此任何嵌套的依赖级别(例如Service类)

因此,如果您尝试在服务类中调用方法,则可能会失败

  @Service 
公共类GreetingService {
@Autowired
。私人的GreetingRepository greetingRepository;
public String greet(String userid){
return greetingRepository.greet(userid);
}
}

@RestController
公共类GreetingController {
@Autowired
私人GreetingService greetingService;
@RequestMapping( / greeting)
public String greeting(@RequestParam(value = name,defaultValue = World)字符串名称){
return String.format( Hello %s,greetingService.greet(name));
}
@RequestMapping( / greeting2)
public String greeting2(@RequestParam(value = name,defaultValue = World)字符串名称){
GreetingService newGreetingService = new GreetingService();
return String.format( Hello%s,newGreetingService.greet(name));
}
}

在上述示例中, /问候将起作用,但是 / greeting2 将失败,因为嵌套的依存关系未解决。



因此,如果要对对象进行弹簧管理,则必须对其进行自动接线。
一般来说,对于视图层pojos和自定义bean配置,您将使用 new 关键字。


I'm working on Spring Boot Rest API, and I did end up using the new keyword here and there.

I'm wondering, did I do something wrong when I used the new keyword for my program. And if it is absolutely forbidden to use new keyword on a real project.

If the answer is yes should i annotate each class i wrote with @component annotation so i can instantiate an object using @autowired.

If the answer is no when can we break that rule ?

解决方案

You can create objects using the new keyword in a spring application.

But these objects would be outside the scope of the Spring Application Context and hence are not spring managed.

Since these are not spring managed, any nested levels of dependency (such as your Service class having a reference to your Repository class etc) will not be resolved.

So if you try to invoke a method in your service class, you might end up getting a NullPointer for the repository.

@Service
public class GreetingService {
    @Autowired
    private GreetingRepository greetingRepository;
    public String greet(String userid) {
       return greetingRepository.greet(userid); 
    }
}

@RestController
public class GreetingController {
    @Autowired
    private GreetingService greetingService;
    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s", greetingService.greet(name));
    }
    @RequestMapping("/greeting2")
    public String greeting2(@RequestParam(value = "name", defaultValue = "World") String name) {
      GreetingService newGreetingService = new GreetingService();
      return String.format("Hello %s", newGreetingService.greet(name));
    }
}

In the above example /greeting will work but /greeting2 will fail because the nested dependencies are not resolved.

So if you want your object to be spring managed, then you have to Autowire them. Generally speaking, for view layer pojos and custom bean configurations, you will use the new keyword.

这篇关于我应该永远不要在Spring Boot项目上使用'new'关键字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆