两个原因导致Spring @Autowired注入的组件为空

本文分析了Spring @Autowired组件为空的两个原因:手动实例化和未使用@Component注解。通过避免手动创建对象并确保所有需要自动注入的类都正确标记,可以防止NullPointerException。提供了解决方案,即在Controller中使用@Autowired自动注入Service。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Controller

public class Controller {

@GetMapping(“/example”)

public String example() {

MyService my = new MyService();

my.doStuff();

}

}

@Service

public class MyService() {

@Autowired

MyRepository repo;

public void doStuff() {

repo.findByName( “steve” );

}

}

@Repository

public interface MyRepository extends CrudRepository<My, Long> {

List findByName( String name );

}

你好,2005呼叫并要求返回他们的代码。好吧,IoC就像是街上的帅小伙子一样,如果你使用的是Spring(自动注入),则需要一直使用它。这是Controller, Service 和 Repository的代码片段,它们将导致NullPointerException。

@Controller

public class Controller {

@GetMapping(“/example”)

public String example() {

MyService my = new MyService();

my.doStuff();

}

}

@Service

public class MyService() {

@Autowired

MyRepository repo;

public void doStuff() {

repo.findByName( “steve” );

}

}

当它尝试访问MyRepository自动连接的存储库时,这将在Service中引发NullPointerException,这不是因为Repository的连接有任何问题,而是因为你使用MyService my = new MyService()手动实例化了MyService。要解决此问题,需要自动注入MyService:

@Controller

public class Controller {

@Autowired

MyService service;

@GetMapping(“/example”)

public String example() {

service.doStuff();

}

}

@Service

public class MyService() {

@Autowired

MyRepository repo;

public void doStuff() {

repo.findByName( “steve” );

}

}

@Repository

public interface MyRepository extends CrudRepository<My, Long> {

List findByName( String name );

}

[](()你忘记在某个类使用组件注解或者它的扩展注解


Spring使用组件扫描来查找需要自动注入并放入到IoC容器中的类。基本上,Spring将扫描项目的类路径(或你指定的路径),找到所有@Component注解的类并将其用于自动装配。因此,如果你忘记注解一个类,则该类将不能自动注入,当你尝试使用它时,将得到一个空的实例,从而导致NullPointerException。

@ Service,@ Repository和@C 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ontroller都是@Component特殊情景下的子注解,因此要自动注入的任何类都必须使用其中之一进行注释。否则,自动注入将导致实例为空:

public class MyService {

public void doStuff() {

}

}

这样的是没有问题的:

@Service

public class MyService {

public void doStuff() {

}

}


本文仅是阅读英文文档练手,原文如下:

[](()原文

====================================================================

Two reasons why your Spring @Autowired component is null

The Spring framework makes heavy use of Inversion of Control (IoC) to let you inject classes without having to worry about their scope, lifetime or cleanup.

A common error people hit is when they autowire a class and when they try to call a method on it find that it is null and they get a NullPointerException. So why didn’t Spring auto-wire your class for you? Here’s two possible reasons:

[](()YOU INSTANTIATED THE A CLASS MANUALLY


Hi, 2005 called and asked for their code back. Yeah, OK, IoC is like the cool kid on the block and if you are using Spring then you need to be using it all the time. Here’s a code snippet of a Controller, Service and Repository that will result in a NullPointerException.

@Controller

public class Controller {

@GetMapping(“/example”)

public String example() {

MyService my = new MyService();

my.doStuff();

}

}

@Service

public class MyService() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值