spring_IOC
Spring
IOC简介
耦合与内聚
- 耦合(Coupling):代码书写过程中所使用技术的结合紧密度,用于衡量软件中各个模块之间的互联程度
- 内聚(Coheision):代码书写过程中单个模块内部各组成部分之间的联系,用于衡量软件中各个功能模块内部的功能联系
- 程序书写的目标:高内聚,低耦合
- 就是同一个模块内的各个元素之间要高度紧密,但是各个模块之间的相互依存度却不要那么紧密
工厂模式发展史:应用程序UserServiceImpl->工厂UserDaoFactory->配置resource.xml->资源UserDaoImpl(配置与资源耦合)
- 变更实现无须修改调用方源代码,也无需修改工厂代码,仅需要修改配置文件
IoC
- IoC(Inversion Of Control)控制反转,Spring反向控制应用程序所以需要使用的外部资源。
- Spring控制的资源全部放置在Spring容器中,该容器称为IoC容器
- 不需要管对象的创建和释放,spring来完成
入门案例
- 模拟三层架构中表现层调用业务层功能
- 表现层:UserApp模拟UserServlet(使用main方法模拟)
- 业务层:UserService
- 入门案例步骤:
- 加载spring
- 创建资源
- 配置资源
- 使用资源
//sercice接口
package ls.service;
public interface UserService {
public void save();
}
package ls.service.impl;
import ls.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("user service running.......");
}
}
//service实现类
import ls.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//启动类
public class UserApp {
public static void main(String[] args) {
//2.加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//3.获取资源
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
<!-- 配置文件 -->
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.创建spring控制的资源 -->
<bean id="userService" class="ls.service.impl.UserServiceImpl"/>
</beans>
Ioc配置(XML)格式
bean
- bean的scope设置为singleton 创建对象是单例的,使用ctx.getBean()使用的是一个对象
- bean的scope设置为prototype创建对象是非单例的,使用ctx.getBean()使用的不是一个对象
- 当创建对象是单例时,在加载配置文件的时候创建对象,当创建对象非单例时,在getBean时创建对象
bean的生命周期
package ls.service.impl;
import ls.service.UserService;
public class UserServiceImpl implements UserService {
public UserServiceImpl(){
System.out.println("constructor is running");
}
public void init(){
System.out.println( "init...");
}
public void destroy(){
System.out.println( "destroy...");
}
@Override
public void save() {
System.out.println("user service running.......");
}
}
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.创建spring控制的资源 -->
<bean id="userService" class="ls.service.impl.UserServiceImpl"/>
<!-- 初始化和销毁时执行方法 -->
<bean id="userService3" scope="singleton" init-method="init" destroy-method="destroy"
class="ls.service.impl.UserServiceImpl"/>
</beans>
- bean的生命周期和此bean是否为单例相关,如果是单例的则只执行一次init或destroy,反之,执行多次。
bean对象的创建方式(工厂)
DI
- DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入
依赖注入的两种方式
set注入(主流)
引用类型和非引用类型注入:Integer,String也按照非引用类型注入
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.创建spring控制的资源 -->
<!-- <bean id="userService" class="ls.service.impl.UserServiceImpl"/>-->
<!-- 初始化和销毁时执行方法 -->
<bean id="userService" scope="singleton" init-method="init" destroy-method="destroy"
class="ls.service.impl.UserServiceImpl">
<!-- 3.将要注入的引用类型的变量通过property属性进行注入,对应的name是要注入的变量名,使用ref属性生命要注入bean的id-->
<property name="userDao" ref="userDao"/>
<property name="num" value="5"/>
</bean>
<!-- 将要注入的资源生命为bean -->
<bean id="userDao" class="ls.dao.impl.UserDaoImpl"/>
</beans>
package ls.service.impl;
import ls.dao.UserDao;
import ls.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
private int num;
//1.对需要进行注入的变量添加set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setNum(int num) {
this.num = num;
}
public UserServiceImpl(){
System.out.println("constructor is running");
}
public void init(){
System.out.println( "init...");
}
public void destroy(){
System.out.println( "destroy...");
}
@Override
public void save() {
userDao.save();
System.out.println("user service running.......");
}
}
构造方法注入
- 在UserServiceImpl中添加构造方法
- 在配置文件中添加
name对应属性名,vaule/ref代表非引用或引用类型的值,type表示类型,index表示索引,索引从0开始
集合类型数据注入
- 集合类型数据注入
<!-- List集合类型注入数据 -->
<property name="myList">
<list>
<value>itheima</value>
<value>6666</value>
<ref bean="userService"/>
<baan class="com.itheima.service.ApplyService"/>
</list>
</property>
<!-- Properties类型注入数据-->
<property name="myProps">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
<!-- 数组类型注入数据-->
<property name="myArray">
<array>
<value>itheima</value>
<value>555</value>
<ref bean="userService"/>
<bean class="com.itheima.service.ApplyService"/>
</array>
</property>
<!-- Set集合类型注入数据-->
<property name="mySet">
<set>
<value>itheima</value>
<value>555</value>
<ref bean="userService"/>
<bean class="com.itheima.service.ApplyService"/>
</set>
</property>
<!-- Map集合类型注入数据-->
<property name="myMap">
<map>
<entry key="name" value-ref="itheima"/>
<entry key="fame" value-ref="666"/>
<entry key="userService">
<ref bean="userService"></ref>
</entry>
<entry key="applyService">
<bean class="applyService"></bean>
</entry>
</map>
</property>
使用p命名空间简化配置
SpEL
properties文件
<!-- 1.加载context命名空间的支持-->
<!-- xmlns:context="http://www.springframework.org/schema/context"-->
<!-- 2.加载配置文件-->
<context:property-placeholder location="classpath:*.properties"/>
<bean id="userDao" class="com.itherima.dao.impl.UserDaoImpl">
<property name="userName" value="${username}"/>
<property name="password" value="${pwd}"/>
</bean>
团队开发
<import resource="applicationContext-user.xml"/>
<import resource="applicationContext-book.xml"/>
- Spring容器加载多个配置文件
new ClassPathXmlApplicationContext("config1.xml","config2.xml");
applicationContext
第三方资源配置
注解开发
常用注解
//applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
https://www.springframework.org/schema/beans/spring-context.xsd
">
<!--启动注解驱动,指定对应扫描的路径,也就是资源所在的包-->
<context:component-scan base-package="包名"/>
</beans>
AOP
- 面向切面编程,是一种编程范式。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异