JAVA全系列 教程
3762个小节阅读:7094.9k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
作用:用于创建对象,放入Spring容器,相当于<bean id="" class="">
位置:类上方
注意:
要在配置文件中配置扫描的包,扫描到该注解才能生效。
xxxxxxxxxx
<context:component-scan base-package="com.itbaizhan"></context:component-scan>
@Component
注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。xxxxxxxxxx
// 此时bean的id为studentDaoImpl
@Component
public class StudentDaoImpl implements StudentDao{
public Student findById(int id) {
// 模拟根据id查询学生
return new Student(1,"百战程序员","北京");
}
}
// 此时bean的id为studentDao
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
public Student findById(int id) {
// 模拟根据id查询学生
return new Student(1,"百战程序员","北京");
}
}
1. 在spring中,@Component
注解代替的是标签
A <beans>
B <bean>
C <context>
D <property>
2. 在spring中,@Component
注解中的value属性值表示
A bean对象的id
B bean对象的类型
C bean对象的依赖范围
D bean对象的创建策略
1=>B 2=>A