JAVA全系列 教程
3762个小节阅读:7092.6k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
注解配置和xml配置对于Spring的IOC要实现的功能都是一样的,只是配置的形式不一样。
创建一个新的Spring项目。
编写pojo,dao,service类。
编写空的配置文件,Spring配置文件需要添加context约束,才能配置支持注解。
xxxxxxxxxx
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
使用@Component
,将对象放入Spring容器管理。
作用:用于创建对象,放入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