首页 > 其他分享 >spring中的IOC说明

spring中的IOC说明

时间:2024-05-12 17:12:16浏览次数:18  
标签:return name Car spring 说明 car IOC public 注入

  1. 什么是IOC?

    • 控制反转,把对象创建和对象之间的调用过程交给spring进行管理,为了降低耦合度
  2. 两种IOC接口

    • BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
    • ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用,加载配置文件时候就会把在配置文件对象进行创建。常用。
  3. 创建ApplicationContext接口实例通常采用ClassPathXmlApplicationContext创建

    //初始化Spring容器,加载配置文件
    ApplicationContext applicationContext =
             new ClassPathXmlApplicationContext("bean.xml");
    //2、通过容器获取userDao实例
    UserDao userDao = (UserDao) applicationContext.getBean("userDao");
    
  4. spring配置文件的编写

    // Spring文件创建Bean
    <?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">
    
        <!--让指定类配置给Spring,让Spring创建其对象的实例-->
        <bean id = "userDao" class="com.test.springTest.UserDaoImpl"/>
    </beans>
    
  5. 依赖注入

  • DI(依赖注入)与控制反转(IOC)的具体实现,实现IOC思想需要DI做支持,就是在spring这个容器中,你需要将一些类交给spring容器进行管理,然后在你需要的时候,不是自己去定义,而是直接向spring容器索取,当spring容器知道你的需求之后,就会去它所管理的组件中进行查找,然后直接给你所需要的组件。依赖:bean对像的创建依赖与容器,注入:bean对象的所有属性,由容器来注入。
  1. 依赖注入的两种方式:set注入,构造方法注入

    参考:https://blog.csdn.net/cold___play/article/details/100059134

    • 构造函数注入

      • user的有参构造
    package com.test.bean;
    
    public class User {
    	
    	private String name;
    	private Integer age;
    	private Car car;
    	
    	public User(String name, Car car) {
    		System.out.println("User(String name, Car car)!!");
    		this.name = name;
    		this.car = car;
    	}
    	
    	public User(Car car,String name) {
    		System.out.println("User(Car car,String name)!!");
    		this.name = name;
    		this.car = car;
    	}
    	
    	public User(Integer name, Car car) {
    		System.out.println("User(Integer name, Car car)!!");
    		this.name = name+"";
    		this.car = car;
    	}
    	
    	public Car getCar() {
    		return car;
    	}
    	public void setCar(Car car) {
    		this.car = car;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	@Override
    	public String toString() {
    		return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
    	}
    }
    
    • applicationContext.xml配置:index:用于确定参数的位置,type:用于确定参数的类型,这两个属性可以完全确定一个构造函数。
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    		xmlns="http://www.springframework.org/schema/beans"
    		xmlns:p="http://www.springframework.org/schema/p"
    		 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    
    	<!-- 将car对象配置到容器中 -->
    	<bean name="car" class="com.test.bean.Car" >
    		<property name="name" value="保时捷" ></property>
    		<property name="color" value="红色" ></property>
    	</bean>
    
    	<!-- 构造函数注入 -->
       <bean name="user2" class="com.test.bean.User" >
    	<!-- name属性: 构造函数的参数名
    		index属性: 构造函数的参数索引
    		type属性: 构造函数的参数类型  -->
    	  <constructor-arg name="name" value="24" index="0" type="java.lang.Integer"></constructor-arg>
    	  <constructor-arg name="car" ref="car" index="1" ></constructor-arg>
      </bean> 
    </beans>
    
    • set方法注入

      • 每一个实体类为其属性写上相应的set,get方法

      • applicationContext.xml中进行配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    		xmlns="http://www.springframework.org/schema/beans"
    		xmlns:p="http://www.springframework.org/schema/p"
    		 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    
    	<!-- set方式注入: -->
    	<bean  name="user" class="com.test.bean.User" >
    		<!--值类型注入: 为User对象中名为name的属性注入tom作为值 -->
    		<property name="name" value="tom" ></property>
    		<property name="age"  value="18" ></property>
    		<!-- 引用类型注入: 为car属性注入下方配置的car对象 -->
    		<property name="car"  ref="car" ></property>
    	</bean>
    	
    	<!-- 将car对象配置到容器中 -->
    	<bean name="car" class="pers.zhang.bean.Car" >
    		<property name="name" value="保时捷" ></property>
    		<property name="color" value="红色" ></property>
    	</bean>
    </beans>
    
    • 复杂类型的注入

      • 创建一个CollectionBean类:
    package com.test.bean;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    public class CollectionBean {
    	private Object[] arr;//数组类型注入
    	private List list;//list/set 类型注入
    	private Map map;//map类型注入
    	private Properties prop;//properties类型注入
    	
    	public Object[] getArr() {
    		return arr;
    	}
    	public void setArr(Object[] arr) {
    		this.arr = arr;
    	}
    	public List getList() {
    		return list;
    	}
    	public void setList(List list) {
    		this.list = list;
    	}
    	public Map getMap() {
    		return map;
    	}
    	public void setMap(Map map) {
    		this.map = map;
    	}
    	public Properties getProp() {
    		return prop;
    	}
    	public void setProp(Properties prop) {
    		this.prop = prop;
    	}
    	@Override
    	public String toString() {
    		return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", prop=" + prop
    				+ "]";
    	}
    }
    
    • applicationContext.xml中进行配置:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    		xmlns="http://www.springframework.org/schema/beans"
    		xmlns:p="http://www.springframework.org/schema/p"
    		 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    
        <!-- set方式注入: -->
    	<bean  name="user" class="com.test.bean.User" >
    		<!--值类型注入: 为User对象中名为name的属性注入tom作为值 -->
    		<property name="name" value="tom" ></property>
    		<property name="age"  value="18" ></property>
    		<!-- 引用类型注入: 为car属性注入下方配置的car对象 -->
    		<property name="car"  ref="car" ></property>
    	</bean>
    
        <bean name="car" class="pers.zhang.bean.Car" >
    		<property name="name" value="保时捷" ></property>
    		<property name="color" value="红色" ></property>
    	</bean>
    
        <!-- 复杂类型注入 -->
        <bean name="cb" class="com.test.CollectionBean" >
            <!-- 数组类型注入 -->
            <property name="arr">
                <!-- 元素顺序与注入顺序一致 -->
    		    <array>
    			    <value>tom</value>
    			    <value>jerry</value>
    			    <ref bean="user" />
    		    </array>
    	    </property>
        </bean>
        <bean name="cb" class="com.test.CollectionBean" >
            <!-- list类型注入 -->
            <property name="list"  >
    		    <list>
    			    <value>jack</value>
    			    <value>rose</value>
    			    <ref bean="user" />
    		    </list>
    	    </property>
        </bean>
        <bean name="cb" class="com.test.CollectionBean" >
           <!-- map类型注入 -->
    	    <property name="map"  >
    		    <map>
    			    <!-- 键为字符串,值为字符串 -->
    			    <entry key="url" value="jdbc:mysql ></entry>
    			    <!-- 键为字符串,值为对象 -->
    			    <entry key="user" value-ref="user"  ></entry>
    			    <!-- 键为对象,值为对象 -->
    			    <entry key-ref="user2" value-ref="user2"  ></entry>
    		    </map> 
    	    </property>
        </bean>
        <bean name="cb" class="com.test.CollectionBean" >
      		  <!-- prperties 类型注入 -->
    			<property name="prop"  >
    				<props>
    					<prop key="driverClass">com.jdbc.mysql.Driver</prop>
    					<prop key="userName">root</prop>
    					<prop key="password">1234</prop>
    				</props>
    			</property>
    	</bean>
    </beans>
    
    • 扩展注入方式

      • p命名空间注入:走set方法
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    		xmlns="http://www.springframework.org/schema/beans"
    		xmlns:p="http://www.springframework.org/schema/p"
    		 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    
    	<!-- 将car对象配置到容器中 -->
    	<bean name="car" class="com.test.bean.Car" >
    		<property name="name" value="保时捷" ></property>
    		<property name="color" value="红色" ></property>
    	</bean>
    
    <!-- p名称空间注入, 走set方法
    	1.导入P名称空间  xmlns:p="http://www.springframework.org/schema/p"
    	2.使用p:属性完成注入
    		|-值类型: p:属性名="值"
    		|-对象类型: p:属性名-ref="bean名称"
     -->
    	<bean  name="user" class="com.test.bean.User" p:name="Jack" p:age="20" p:car-ref="car"  ></bean>
    </beans>
    
    • c命名空间注入:走构造器方法

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      		xmlns="http://www.springframework.org/schema/beans"
      		xmlns:c="http://www.springframework.org/schema/c"
      		 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
      
      	<!-- 将car对象配置到容器中 -->
      	<bean name="car" class="com.test.bean.Car" >
      		<property name="name" value="保时捷" ></property>
      		<property name="color" value="红色" ></property>
      	</bean>
      
      <!-- c名称空间注入, 走构造方法,要给定有参和无参的构造方法
      	导入c名称空间  xmlns:c="http://www.springframework.org/schema/c"
       -->
      	<bean  name="user" class="com.test.bean.User" c:name="Jack" c:age="20" ></bean>
      </beans>
      
        
    

标签:return,name,Car,spring,说明,car,IOC,public,注入
From: https://www.cnblogs.com/hytip/p/18187941

相关文章

  • WinForm使用IOC控制程序
    新建WinForm程序1、添加Nuget包Microsoft.Extensions.DependencyInjection2、改变启动项internalstaticclassProgram{///<summary>///应用程序的主入口点。///</summary>[STAThread]staticvoidMain(){//创建服务容......
  • spring简介
    Spring是一个轻量级Java开发框架,最根本的使命是解决企业级应用开发的复杂性,即简化Java开发。Spring为企业级开发提供了丰富的功能,但是这些功能的底层都依赖于它的两个核心特性,也就是控制反转(IOC)和面向切面编程(aspect-orientedprogramming,AOP)。spring框架包含的功能核......
  • Spring常见注解
    Spring常见注解注解说明@Component使用在类上用于实例化Bean@Controller使用在类上用于实例化Controller@Service使用在类上用于实例化Service@Repository使用在类上用于实例化Repository@Autowired使用在字段上用于根据类型依赖注入@Qualifier结......
  • Springboot自动配置原理
    在SpringBoot项目中的引导类上有一个注解@SpringBootApplication,这个注解是对三个注解进行了封装,分别是:@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan其中@EnableAutoConfiguration是实现自动化配置的核心注解。该注解通过@Import注解导入对应的配......
  • SpringBoot速记
    本篇以SpringBoot快速构建一个基础项目,在构建的同时记录相关的知识。常见的架构图: 其中,config中可以引入第三方的jar包controller中存放控制类一个简单的例子如下: mapper中存放对数据库的操作接口 pojo中是实体对象类,常与数据表对应 service中存放服务类:......
  • SpringBoot3集成WebSocket
    标签:WebSocket,Session,Postman。一、简介WebSocket通过一个TCP连接在客户端和服务器之间建立一个全双工、双向的通信通道,使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据,在WebSocket的API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创......
  • 智能工作流:Spring AI高效批量化提示访问方案
    基于SpringAI搭建系统,依靠线程池\负载均衡等技术进行请求优化,用于解决科研&开发过程中对GPT接口进行批量化接口请求中出现的问题。github地址:https://github.com/linkcao/springai-wave大语言模型接口以OpenAI的GPT3.5为例,JDK版本为17,其他依赖版本可见仓库pom.xml拟解决的问题......
  • while(cin >> x)的说明
    while循环中的cin在学习C++的过程中,C++PreimerPlus中经常出现while(cin>>x),为什么可以这么写?cin是一个输入流对象,cin>>x单独使用时,返回结果也是一个cin对象(修改过的)。>>是运算符的重载,其函数原型istream&operator>>(istream&is,typenamee);,其中typename可以是char*,i......
  • Spring MVC执行流程
    视图执行流程用户发送出请求到前端控制器DispatcherServlet。DispatcherServlet收到请求调用HandlerMapping(处理器映射器)。HandlerMapping找到具体的处理器,生成处理器对象及处理器拦截器(如果有),再一起返回给DispatcherServlet。DispatcherServlet调用HandlerAdapter(处理......
  • Spring bean循环依赖
    Spring循环引用循环依赖其实就是循环引用,也就是两个或两个以上的bean互相持有对方,最终形成闭环。比如A依赖于B,B依赖于A。循环依赖在spring中是允许存在,spring框架依据三级缓存已经解决了大部分的循环依赖。一级缓存:单例池,缓存已经经历了完整的生命周期,已经初始化完成的bean对......