当前位置:网站首页>My problem solving record 1: the @component annotation is used on the class. If you want to use the methods in this class, you can't directly new, but should use @autowired for injection, otherwise an
My problem solving record 1: the @component annotation is used on the class. If you want to use the methods in this class, you can't directly new, but should use @autowired for injection, otherwise an
2022-07-29 09:43:00 【? abc!】
I haven't used it for a long time springboot,spring Some annotations of are not clear about how to use , Therefore, the current problem appears
Use Springbooot Creating a thread pool , And use thread pool , Created a util class , It is found that other methods cannot be used directly new Methods , Later, I found that I put this util Class is used. @Component, Can't be used directly new One way ( Such as xxxUtils p = new xxxUtils()), Instead, it should be used in the methods that use the tool class @Autowired This goes to injection bean
class @Component annotation , Want to use the methods in this class , You can't just new, Instead, use # @Autowired For injection , Otherwise, an error will be reported ( Such as null pointer exception, etc )
@Component
Is a meta-annotation , Other class annotations can be annotated , The function is to tell spring The current class is a Bean object , And then let spring Automatically create and manage this class
- Appoint bean The name of , Default to class name initial lowercase :@Component
- Appoint bean The name of ,@Component(“university1”)
@Autowired
Indicates whether a property needs dependency injection , Can be used in attribute 、 In the ordinary way 、 In terms of construction method . In the annotations required Property default is true, If no object can be injected into attributes , Then an exception will be reported ;
Add to an attribute,spring Meeting from ioc Found in the container bean Objects are injected into attributes , If you find more than one of this type Bean object , Then, according to the name of the attribute, select from multiple Bean Confirm one of the objects ;Written in set() On the way, stay spring According to the parameter type of the method ioc Found this type of Bean Object is injected into the row parameters of the method , And automatically reflect and call the method ( By @Autowired The method of decoration must be implemented ), So it is generally used in set In the method 、 No need for ordinary methods ;Used in construction methods: Formal parameters according to the construction method 、 The name of the parameter , from ioc Found this type of Bean object , Inject into the formal parameters of the construction method , And execute the method ;
边栏推荐
- OpenCV入门基础学习
- Pyqt5 rapid development and practice 6.1 three dimensions of good software & 6.2 layout management in pyqt5 & 6.3 absolute location layout of pyqt5
- i.MX6ULL驱动开发 | 32 - 手动编写一个虚拟网卡设备
- Unity3d hodgepodge
- Detailed explanation: what is the GPS Beidou time service server?
- mysql 数据库 期末复习题库
- 我的问题解决记录1:类上使用了@Component注解,想要使用这个类中的方法,便不能直接new,而应该使用# @Autowired进行注入,否则会报错(如空指针异常等)
- node(二)
- Pytest+allure generate test report
- C语言的传参方式(int x)(int *x)(int &x)
猜你喜欢

Custom configuration

【C语言】扫雷(递归展开 + 标记功能)

《LOL》从代码上来说最难的是哪个英雄?
![[Bert multi label text classification practice] I - overview of practical projects](/img/47/3e6cf5d49e41a03ea7f9b6eb295e1d.png)
[Bert multi label text classification practice] I - overview of practical projects

Problems and solutions of introducing redis cache

Mysql database final review question bank

SiC功率半导体产业高峰论坛成功举办

Source code analysis of senparc.weixin.sample.mp

浅谈契约测试

Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)
随机推荐
Vector implementation
QoS quality of service five traffic shaping of QoS boundary behavior
Implementation and verification logic of complex expression input component
【集中培训】HCIP-Cloud Computing 资源交流帖
How to customize the opportunity closing form in dynamics 365online
机器学习之线性回归(最小二乘法手写+sklearn实现)
Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
redis可视化工具读取数据乱码问题解决
Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)
Study and exploration of Redux API implementation of Redux
PyQt5快速开发与实战 6.4 QBoxLayout(框布局)
Basic operations of OpenCV image processing
[C language] Sanzi chess (intelligent chess playing + blocking players)
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
高智伟:数据管理赋能交通行业数字化转型
最新翻译的官方PyTorch简易入门教程(PyTorch1.0版本)
怎么样的框架对于开发者是友好的?
[centralized training] hcip cloud computing resource exchange post
Network security (6)
Zhongang Mining: four steps for sustainable and healthy development of fluorite industry