当前位置:网站首页>我的问题解决记录1:类上使用了@Component注解,想要使用这个类中的方法,便不能直接new,而应该使用# @Autowired进行注入,否则会报错(如空指针异常等)
我的问题解决记录1:类上使用了@Component注解,想要使用这个类中的方法,便不能直接new,而应该使用# @Autowired进行注入,否则会报错(如空指针异常等)
2022-07-29 09:37:00 【?abc!】
好久好久没有使用springboot,spring的一些注解都不清楚该怎么使用了,因此出现现在的这个问题
使用Springbooot创建线程池,并使用线程池,创建了一个util类,发现其他方法无法直接使用new的方法,后面发现我是将这个util类使用了@Component,不能直接使用new一个方法(如 xxxUtils p = new xxxUtils()),而应该要在使用该工具类的方法里面使用@Autowired这个去注入bean
类上使用了@Component注解,想要使用这个类中的方法,便不能直接new,而应该使用# @Autowired进行注入,否则会报错(如空指针异常等)
@Component
是一个元注解,可以注解其他类注解,功能是用来告诉spring当前类是一个Bean对象,然后让spring对该类进行自动创建并管理
- 指定bean的名称,默认为类名首字母小写:@Component
- 指定bean的名称,@Component(“university1”)
@Autowired
表示一个属性是否需要进行依赖注入,可以使用在属性、普通方法上、构造方法上。注解中的required属性默认是true,如果没有对象可以注入到属性,则会报出异常;
加在某个属性上,spring会从ioc容器中找到bean对象注入到属性上,如果找到多个该类型的Bean对象,则再根据属性的名字从多个Bean对象中确认一个;写在set()方法上,在spring会根据方法的参数类型从ioc容器中找到该类型的Bean对象注入到方法的行参中,并且自动反射调用该方法(被@Autowired修饰的方法一定会执行),所以一般使用在set方法中、普通方法不用;使用在构造方法中:根据构造方法的形参、形参名,从ioc容器中找到该类型的Bean对象,注入到构造方法的形参中,并且执行该方法;
边栏推荐
- div 水平排列
- 远程连接windows版本服务器redis的配置文件设置
- 浅谈契约测试
- 简述堆和栈的区别
- Shutter -- use camera (continuously updating)
- 怎样查询快递物流筛选出无信息单号删除或者复制
- [centralized training] hcip cloud computing resource exchange post
- Youboxun, the gold donor of the open atom open source foundation, joined hands with partners to help openharmony break the circle!
- Explanation of C value type and reference type
- NFA determination and DFA minimization based on C language
猜你喜欢

Behind 100000 visits...

远程连接windows版本服务器redis的配置文件设置

View port occupancy
![[苹果开发者账号]06 转让开发者账号后,开发者年费自动续费问题](/img/a7/12fd63f16ebca81a3dd2d1b97847d1.png)
[苹果开发者账号]06 转让开发者账号后,开发者年费自动续费问题

Sample is new and supported from API 8! Come and take it

开放原子开源基金会黄金捐赠人优博讯携手合作伙伴,助力OpenHarmony破圈!

Cloud native management practice: business led Devops continuous delivery system

云原生管理实践:业务引领的DevOps持续交付体系

Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?

i.MX6ULL驱动开发 | 32 - 手动编写一个虚拟网卡设备
随机推荐
Pytest+allure generate test report
怎么样的框架对于开发者是友好的?
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
Implementation of DFA string recognition based on C language simulation
Random number setting and reference between parameters
Senparc.Weixin.Sample.MP源码剖析
Use cpolar to publish raspberry pie web pages (improvement of cpolar function)
Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
redis可视化工具读取数据乱码问题解决
Behind 100000 visits...
dataframe. to_ Sql() inserts too many errors at one time
系统架构师学习
[centralized training] hcip cloud computing resource exchange post
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
Youboxun, the gold donor of the open atom open source foundation, joined hands with partners to help openharmony break the circle!
Which hero is the most difficult for lol in terms of code?
SiC Power Semiconductor Industry Summit Forum successfully held
Sublime Text3 设置不同文件不同缩进
乱打日志的男孩运气怎么样我不知道,加班肯定很多
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展