IOC:Inversion of Control 控制反转,是一种设计原则,spring 中通过DI(dependency Injection)来具体实现。
比如原本对象的实例化,是通过程序主动New出来,IOC中的对象实例交给Spring框架来实例化,程序使用时直接通过spring获取即可。
1、IOC容器实例化对象的方式--构造方法
实体类:
package com.text.entity;public class Student {private String name;private int age;public Student() {System.out.println("无参构造方法:" + this.hashCode());}public Student(String name, int age) {System.out.println("有参构造方法:" + this.hashCode());this.name = name;this.age = age;}
}
applicationContext.xml:
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="s1" class="com.text.entity.Student"></bean><bean id="s2" class="com.text.entity.Student"><constructor-arg name="name" value="张三"/><constructor-arg name="age" value="18"/></bean>
</beans>
测试类:运行后输出
无参构造方法:6018
有参构造方法:6018
null
张三
package com.text.entity;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {String configLocation = "classpath:applicationContext.xml";//spring IOC容器ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);Student s1 = context.getBean("s1", Student.class);Student s2 = context.getBean("s2", Student.class);System.out.println(s1.getName());System.out.println(s2.getName());}
}
其中:ClassPathXmlApplicationContext 加载的路径文件classpath:applicationContext.xml称作路径表达式,具体形式参考如下表格示例
对于本项目的classpath 就是下图target/classes文件夹:
2、对象依赖注入(DI)
示例:将课程对象注入到学生对象中
配置文件:applicationContext.xml
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="course" class="com.text.entity.Course"><constructor-arg name="name" value="语文"/></bean><bean id="student" class="com.text.entity.Student"><property name="name" value="张三"/><property name="age" value="20"/><property name="course" ref="course"/></bean>
</beans>
实体类:
package com.text.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Course {private String name;//课程名称
}
package com.text.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {private String name;private int age;private Course course;
}
测试类:
package com.text.entity;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {String configLocation = "classpath:applicationContext.xml";//spring IOC容器ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);Course course = context.getBean("course",Course.class);Student student = context.getBean("student",Student.class);System.out.println(student);//Student(name=张三, age=20, course=Course(name=语文))System.out.println(course);//Course(name=语文)System.out.println(student.getCourse() == course); //true}
}
3、查询IOC容器对象示例详情
通过ApplicationContext.getBeanDefinitionNames 方法获取IOC容器中的已经实例化的对象名
package com.text.entity;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {String configLocation = "classpath:applicationContext.xml";//spring IOC容器ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);Course course = context.getBean("course",Course.class);String[] beanDefinitionNames = context.getBeanDefinitionNames();for(String beanDefinitionName:beanDefinitionNames) {System.out.print(beanDefinitionName + "\t");//course student }}
}