当前位置:网站首页>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 ;
边栏推荐
- JS to achieve full screen effect
- Explanation of C value type and reference type
- How to contribute to openharmony
- 2021年CS保研经历(六):系统填报 + 一些感想
- How to query express logistics and filter out no information doc No. to delete or copy
- Unity xchart3.0 basic usage quick start
- MySQL事务与MVCC如何实现的隔离级别
- MySQL converts some table names to uppercase
- redis可视化工具读取数据乱码问题解决
- 网络安全(6)
猜你喜欢

Behind 100000 visits...

HarmonyOS 3.0 发布!

怎样查询快递物流筛选出无信息单号删除或者复制

OpenCV入门基础学习

User identity identification and account system practice

Unity guidance system. Click the target object and prompt the text to change color to enter the next step

《LOL》从代码上来说最难的是哪个英雄?

node(二)

Unity3d空包打apk报错汇总

Opencv introductory basic learning
随机推荐
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
[Bert multi label text classification practice] I - overview of practical projects
vector实现
[centralized training] hcip cloud computing resource exchange post
附录2-一些简单的练习
pytest+allure生成测试报告
Behind 100000 visits...
WebAssembly 2022 问卷调查结果新鲜出炉
User identity identification and account system practice
深入浅出依赖注入及其在抖音直播中的应用
C# 值类型和引用类型讲解
How to realize the isolation level between MySQL transactions and mvcc
【C语言】扫雷(递归展开 + 标记功能)
Logistic regression of machine learning
核酸扫码登记体验有感(如何提高OCR的文字正确识别率)
Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)
Source code analysis of senparc.weixin.sample.mp
Sample is new and supported from API 8! Come and take it
How to customize the opportunity closing form in dynamics 365online
Flutter文本编辑器