关于Spring 中的 IoC
学习Java的Spring框架,有助于软件开发人员取得相关认证 #生活技巧# #工作学习技巧# #职业技能认证#
1 Spring
Java领域的第一框架,是行业标准和规范。Java初期使用EJB开发,但是这种方式非常繁重,不灵活,不便于维护和升级。经过20年的发展,Spring框架已经从最初取代EJB构建企业级开发的方式,发展成一套自有的生态体系。
Spring框架是指Spring Framework,Spring Framework是整套Spring全家桶所以产品的基石,所有组件产品都是基于Spring Framework,IoC容器。
Spring 有两大核心组件:
Ioc 控制反转Aop 面向切面编程 1.1 SSH框架structs2(实现MVC,客户端和服务器的交互)、Spring(构建项目)、Hibernate(持久层)
Spring MVC、Spring、Mybatis
2 Spring Ioc
控制反转(Inversion of Control),反转的是Java程序中对象的创建方式,传统开发是开发者需要的时候new一个对象出来,而控制反转不需要开发者手动new,而是交给IoC容器来创建,开发者可直接使用
1、传统开发方式
public class test01 { public static void main(String[] args) { //传统方式创建对象 Card card = new Card(); card.setCNO("sx010"); card.setNAME("宝乐"); card.setCLASS("c03"); System.out.println(card); 12345678
2、IoC
<bean id="card1" class="com.eleven.entity.Card"> <property name="CNO" value="sx011"></property> <property name="NAME" value="历飞雨"></property> <property name="CLASS" value="c03"></property> </bean> 12345
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); Card card1 = (Card) applicationContext.getBean("card1"); System.out.println(card1); 123456
2.1 IoC使用流程
将程序需要的对象在spring.xml配置文件中配置(两种配置方式)无参构造( property ):通过setter方法完成赋值,实体类必须有无参构造)
<bean id="card1" class="com.eleven.entity.Card"> <property name="CNO" value="sx011"></property> <property name="NAME" value="历飞雨"></property> <property name="CLASS" value="c03"></property> </bean> 12345
有参构造( constructor-arg ):实体类必须有有参构造
<bean id="card1" class="com.eleven.entity.Card"> <constructor-arg name="CLASS" value="C05"></constructor-arg> <constructor-arg name="CNO" value="sx004"></constructor-arg> <constructor-arg name="NAME" value="大宝乐"></constructor-arg> </bean> 12345 读取 spring.xml 文件 生成 ApplicationContext对象(IoC容器)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); 1
3.从ApplicationContext对象中获取需要的bean(两种获取方式)
通过id获取 (spring.xml中配置的id必须唯一)Card card1 = (Card) applicationContext.getBean("card"); 1 通过Class类型获取(必须保证配置文件中只有一个该类的bean)
Card card1 = (Card) applicationContext.getBean("Card.class"); 1
2.1 IoC的DI
bean之间的依赖注入叫做DI,IoC包含DI,IoC可以自动完成依赖注入,把一个bean注入到另一个bean中
1.传统开发方式
public static void main(String[] args) { Card card = new Card(); card.setNAME("王宝乐"); card.setCNO("sx001"); card.setCLASS("c04"); Student student = new Student(); student.setId(1); student.setName("土匪"); //传统依赖注入 DI card.setStudent(student); 12345678910111213
2.ioc方式 :使用 ref 完成依赖注入
<bean id="card" class="com.eleven.entity.Card"> <constructor-arg name="CLASS" value="C05"></constructor-arg> <constructor-arg name="CNO" value="sx004"></constructor-arg> <constructor-arg name="NAME" value="大宝乐"></constructor-arg> <constructor-arg name="student" ref="stu1"></constructor-arg> </bean> <bean id="stu1" class="com.eleven.entity.Student"> <property name="id" value="1"></property> <property name="name" value="吴亦凡"></property> </bean> 1234567891011
3.多bean依赖注入 使用 list 完成依赖注入
<bean id="stu1" class="com.eleven.entity.Student"> <property name="id" value="1"></property> <property name="name" value="吴亦凡"></property> </bean> <bean id="stu2" class="com.eleven.entity.Student"> <property name="id" value="2"></property> <property name="name" value="鹿晗"></property> </bean> <bean id="stu3" class="com.eleven.entity.Student"> <property name="id" value="3"></property> <property name="name" value="张艺兴"></property> </bean> <bean id="cla" class="com.eleven.entity.Class"> <property name="id" value="1"></property> <property name="name" value="一班"></property> <property name="students"> <list> <ref bean="stu1"></ref> <ref bean="stu2"></ref> <ref bean="stu3"></ref> </list> </property> </bean>
123456789101112131415161718192021222324网址:关于Spring 中的 IoC https://www.yuejiaxmz.com/news/view/760027
相关内容
Spring学习橄榄油适用于中式烹饪吗?欧丽薇兰携手IOC公布实验室提供权威用油指南
Spring中文件的上传下载
Spring Boot中使用Server
spring
基于Spring Boot的二手物品交易平台
基于SSM+Vue+MySQL的家教服务管理系统
基于Spring Boot + Vue + MySQL的公益旧物捐赠系统
基于Spring Boot+Vue的校园二手交易平台
@EnableRetry(proxyTargetClass = true) . spring