Spring使用-IoC容器
Spring的使用
1. Spring Core
IoC Container(重点)
AOP(重点)
Events
Resources
i18n
Validation
Data Binding
Type Conversion
SpEL
1.1 IoC Container-负责实例化、配置和组装 bean
容器通过读取配置元数据获取有关要实例化、配置和组装的对象的说明。
Spring IoC 容器使用一种配置元数据形式
Spring 容器中使用其他形式的元数据的信息:
基于注释的配置:Spring 2.5 引入了对基于注释的配置元数据的支持。
基于 Java 的配置:从 Spring 3.0 开始,Spring JavaConfig 项目提供的许多功能成为核心 Spring 框架的一部分。因此,您可以使用 Java 而不是 XML 文件在应用程序类外部定义 bean。
1.1.1 IoC容器配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
1.1.2实例化IOC容器
提供给构造函数的位置路径或路径是资源字符串,用于让容器从各种外部资源(如本地文件系统、Java 等)加载配置元数据。
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
1.1.3创建对象
- 使用无参构造创建对象
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring这些类都成为Bean
类型 变量名 = new 类型
此处的id等价于变量名,class等价于 new 类型
中间的property相当于给属性赋值-->
<bean id="student" class="cn.bleds.pojo.Student">
<!--创建一个hello对象,属性str的值为Spring-->
<property name="name" value="ka"/>
<property name="age" value="18"/>
</bean>
</beans>
<property name="age" value="18"/>
这样注入是使用空参构造方法创建实例,然后使用set注入的方式来给变量赋值(必须要有set方法,是)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="cn.bleds.pojo.Student">
<!--创建一个hello对象,属性str的值为Spring-->
<constructor-arg name="name" value="amos"/>
<constructor-arg name="age" value="15"/>
</bean>
</beans>
<constructor-arg name="name" value="amos"/>
这样的注入方式是使用有参构造函数创建实例,并赋值。
这样的注入好有其他两个方式
<constructor-arg type="java.lang.String" value="ka"/>
-根据参数的类型来注入
<constructor-arg index="0" value="ka"/>
-根据参数的索引来注入
1.1.4 依赖注入
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
下面的列表显示了一个示例:
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!