第一种方案:IOC反转的第一种方式:在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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!--<bean id="填对象名" class="要创建对象的类的完整包名"></bean>--> <!--java注解的方式创建类的对象,该对象不写在这个主配置文件中 写在了要创建对象的类的上面,需要在运行的时候,主配置文件要找到所有使用注解方式创建的对象 然后动态的加载进主配置文件--> <!--//狗编号//狗姓名//狗年龄//狗皮肤的色彩//狗的身高//狗的重量--> <bean id="dog" class="com.spring.entity.Dog"> <property name="id" value="1002"></property> <property name="name" value="我是Spring框架的对象小狗"></property> <property name="age" value="9"></property> <property name="color" value="黑色"></property> <property name="height" value="120"></property> <property name="weight" value="11"></property> <property name="x" value="狗的run"></property> </bean>
/** * 第二种方式Spring创建对象与赋值 Spring框架 利用Bean管理数据 */ @Test public void SpringDog() { System.out.println("第二种方式Spring创建对象与赋值 Spring框架 利用Bean管理数据"); ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Dog dog = ac.getBean("dog", Dog.class); System.out.println("利用Spring文件赋值"); System.out.println("狗的编号" + dog.getId() + "狗的姓名" + dog.getName() + "狗的年龄" + dog.getAge() + "狗的色彩" + dog.getColor() + "狗的身高" + dog.getHeight() + "狗的体重" + dog.getWeight() + "调用Animal的独有属性" + dog.getX()); }
运行结果:
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=50076:E:\IDAE2020\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\out\production\Spring401;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\hamcrest-core-1.3.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\junit-4.13.2.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\spring-aop-5.0.2.RELEASE.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\spring-beans-5.0.2.RELEASE.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\spring-context-5.0.2.RELEASE.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\spring-core-5.0.2.RELEASE.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\spring-expression-5.0.2.RELEASE.jar;E:\桌面\2022年Sping架包\2022上课代码\Spring401\web\WEB-INF\lib\spring-jcl-5.0.2.RELEASE.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.spring.test.AnimalTest,SpringDog 第二种方式Spring创建对象与赋值 Spring框架 利用Bean管理数据 四月 24, 2022 6:38:57 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@21a06946: startup date [Sun Apr 24 18:38:57 CST 2022]; root of context hierarchy 四月 24, 2022 6:38:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] Animal的无惨构造 狗的无惨构造 Animal的无惨构造 Animal的无惨构造 这是Car的属性创建对象 利用Spring文件赋值 狗的编号1002狗的姓名我是Spring框架的对象小狗狗的年龄9狗的色彩黑色狗的身高120狗的体重11调用Animal的独有属性狗的run Process finished with exit code 0
第二种方案:IOC反转的第一种方式:applicationContext.xml文件加上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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!--<bean id="填对象名" class="要创建对象的类的完整包名"></bean>--> <!--java注解的方式创建类的对象,该对象不写在这个主配置文件中 写在了要创建对象的类的上面,需要在运行的时候,主配置文件要找到所有使用注解方式创建的对象 然后动态的加载进主配置文件--> <!--第三种方式Spring IOC框架注解的方式创建对象与赋值--> <!--指定所有Sping文件带有注解方式的对象所存在的包的完整路径--> <context:component-scan base-package="com.spring.entity"></context:component-scan> </beans>
package com.spring.entity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 一个标准类的封装 */ @Component(value = "animal") public class Animal { @Value("1003") private int id; @Value("这里调用的是Car对象的内容小喵喵") private String name; @Value("20") private String age; @Value("白色") private String color; @Value("120") private int height; @Value("23") private int weight; @Value("X的未知数") private Object x; //@Autowired:该注解用于将引用数据类型的对象赋值给该类型的变量 //自动写入,前提条件是该变量名称一定要和对象名称一致 //当变量名和对象名不一致,那么需要使用@Qualifier(value = "对象名")和@Autowired注解一起使用 //@Resource(name = "对象名")的用法等价于@Qualifier(value = "对象名")和@Autowired注解一起使用 @Autowired private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Animal() { System.out.println("Animal的无惨构造"); } public Animal(int id, String name, String age, String color, int height, int weight, Object x) { this.id = id; this.name = name; this.age = age; this.color = color; this.height = height; this.weight = weight; this.x = x; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public Object getX() { return x; } public void setX(Object x) { this.x = x; } @Override public String toString() { return "Animal{" + "id=" + id + ", name='" + name + '\'' + ", age='" + age + '\'' + ", color='" + color + '\'' + ", height=" + height + ", weight=" + weight + ", x=" + x + '}'; } }
/** * 第三种方式Spring IOC框架注解的方式创建对象与赋值 */ @Test public void SpringIocCar(){ // 运行时获得主配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Car car=ac.getBean("car",Car.class); System.out.println(car); System.out.println(car.getName()); } @Test public void SpringAnimalStudennt(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Animal animal=ac.getBean("animal",Animal.class); System.out.println(animal.getAge()); System.out.println(animal.getId()); System.out.println(animal.getName()); //调用 System.out.println(animal.getStudent().getName()); System.out.println(animal.getStudent().getAge()); }
运行结果:
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=51687:E:\IDAE2020\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\SSM\2022年Spring\Spring401\out\production\Spring401;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\hamcrest-core-1.3.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\junit-4.13.2.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-aop-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-beans-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-context-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-core-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-expression-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-jcl-5.0.2.RELEASE.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.spring.test.AnimalTest,SpringIocCar 四月 24, 2022 8:07:06 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a92922: startup date [Sun Apr 24 20:07:06 CST 2022]; root of context hierarchy 四月 24, 2022 8:07:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] Animal的无惨构造 狗的无惨构造 Animal的无惨构造 Animal的无惨构造 这是Car的属性创建对象 Animal{id=1003, name='这里调用的是Car对象的内容小喵喵', age='20', color='白色', height=120, weight=23, x=X的未知数} 这里调用的是Car对象的内容小喵喵 Process finished with exit code 0
package com.spring.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * IOC注解的方式创建对象 * @author MZFAITHDREAM * MVC三层架构的使用 浏览器 控制层 逻辑处理层 数据访问层 数据库 * @Component:该注解用于创建普通类的对象 * @Controller:该注解用于创建控制层类的对象 * @Service:该注解用于创建业务逻辑层类的对象 * @Repository:该注解用于创建数据访问层类的对象 */ @Component(value = "car") public class Car extends Animal{ public Car() { System.out.println("这是Car的属性创建对象"); } public Car(int id, String name, String age, String color, int height, int weight, Object x) { super(id, name, age, color, height, weight, x); } @Override public int getId() { return super.getId(); } @Override public void setId(int id) { super.setId(id); } @Override public String getName() { return super.getName(); } @Override public void setName(String name) { super.setName(name); } @Override public String getAge() { return super.getAge(); } @Override public void setAge(String age) { super.setAge(age); } @Override public String getColor() { return super.getColor(); } @Override public void setColor(String color) { super.setColor(color); } @Override public int getHeight() { return super.getHeight(); } @Override public void setHeight(int height) { super.setHeight(height); } @Override public int getWeight() { return super.getWeight(); } @Override public void setWeight(int weight) { super.setWeight(weight); } @Override public Object getX() { return super.getX(); } @Override public void setX(Object x) { super.setX(x); } @Override public String toString() { return super.toString(); } }
package com.spring.entity; /** * 顺便复习下继承 */ public class Dog extends Animal{ private int id; //狗编号 private String name; //狗姓名 private String age; //狗年龄 private String color; //狗皮肤的色彩 private int height; //狗的身高 private int weight; //狗的重量 public Dog() { System.out.println("狗的无惨构造"); } public Dog(int id, String name, String age, String color, int height, int weight, Object x) { super(id, name, age, color, height, weight, x); } /** * @Override * @return * override是重写(覆盖)了一个方法,以实现不同的功能。一般是用于子类在继承父类时,重写(重新实现)父类中的方法。 * 重写(覆盖)的规则: */ @Override public int getId() { return super.getId(); } @Override public void setId(int id) { super.setId(id); } @Override public String getName() { return super.getName(); } @Override public void setName(String name) { super.setName(name); } @Override public String getAge() { return super.getAge(); } @Override public void setAge(String age) { super.setAge(age); } @Override public String getColor() { return super.getColor(); } @Override public void setColor(String color) { super.setColor(color); } @Override public int getHeight() { return super.getHeight(); } @Override public void setHeight(int height) { super.setHeight(height); } @Override public int getWeight() { return super.getWeight(); } @Override public void setWeight(int weight) { super.setWeight(weight); } @Override public Object getX() { return super.getX(); } @Override public void setX(Object x) { super.setX(x); } @Override public String toString() { return super.toString(); } }
package com.spring.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; /** * 2022/4/24 * IOC注解的方式 * @author MZFAITHDREAM * @Component:该注解用于创建普通类的对象 * @Controller:该注解用于创建控制层类的对象 * @Service:该注解用于创建业务逻辑层类的对象 * @Repository:该注解用于创建数据访问层类的对象 */ //value:该属性上定义的是对象名 //@Controller(value = "student") //@Service(value = "student") //@Repository(value = "student") @Component(value = "student") public class Student { //@Value:该注解用于给基本数据类型的全局变量进行赋值 @Value("小明") private String name; @Value("20") private int age; @Value("18166666666") private String phone; 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; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
package com.spring.entity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.annotation.Resources; @Component(value = "tea") public class Teacher { @Value("马云老师") private String name; @Value("60") private int age; //@Autowired:该注解用于将引用数据类型的对象赋值给该类型的变量 //自动写入,前提条件是该变量名称一定要和对象名称一致 //当变量名和对象名不一致,那么需要使用@Qualifier(value = "对象名")和@Autowired注解一起使用 //@Resource(name = "对象名")的用法等价于@Qualifier(value = "对象名")和@Autowired注解一起使用 @Autowired private Student student; 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; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
package com.spring.test; import com.spring.Utils.SpringUtil; import com.spring.entity.*; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AnimalTest { /** * 第一种方案java方式创建对象与赋值 */ @Test public void java() { System.out.println("第一种方案java方式创建对象与赋值"); Animal animal = new Animal(); animal.setId(1001); animal.setAge("2"); animal.setName("小鸟"); animal.setColor("red"); animal.setHeight(170); animal.setWeight(32); animal.setX("我是未知的属性"); System.out.println(animal); System.out.print("动物编号" + animal.getId() + "动物姓名" + animal.getName() + "动物年龄" + animal.getAge() + "动物的身高" + animal.getHeight() + "动物的体重" + animal.getWeight() + "动物的色彩" + animal.getColor() + "动物的未知属性" + animal.getX()); } /** * 第二种方式Spring创建对象与赋值 Spring框架 利用Bean管理数据 */ @Test public void SpringDog() { System.out.println("第二种方式Spring创建对象与赋值 Spring框架 利用Bean管理数据"); ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Dog dog = ac.getBean("dog", Dog.class); System.out.println("利用Spring文件赋值"); System.out.println("狗的编号" + dog.getId() + "狗的姓名" + dog.getName() + "狗的年龄" + dog.getAge() + "狗的色彩" + dog.getColor() + "狗的身高" + dog.getHeight() + "狗的体重" + dog.getWeight() + "调用Animal的独有属性" + dog.getX()); } /** * 第三种方式Spring IOC框架注解的方式创建对象与赋值 */ @Test public void SpringIocCar(){ // 运行时获得主配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Car car=ac.getBean("car",Car.class); System.out.println(car); System.out.println(car.getName()); } @Test public void SpringAnimalStudennt(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Animal animal=ac.getBean("animal",Animal.class); System.out.println(animal.getAge()); System.out.println(animal.getId()); System.out.println(animal.getName()); //调用 System.out.println(animal.getStudent().getName()); System.out.println(animal.getStudent().getAge()); } }
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=51757:E:\IDAE2020\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\SSM\2022年Spring\Spring401\out\production\Spring401;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\hamcrest-core-1.3.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\junit-4.13.2.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-aop-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-beans-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-context-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-core-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-expression-5.0.2.RELEASE.jar;D:\SSM\2022年Spring\Spring401\web\WEB-INF\lib\spring-jcl-5.0.2.RELEASE.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.spring.test.AnimalTest 四月 24, 2022 8:10:50 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3ecf72fd: startup date [Sun Apr 24 20:10:50 CST 2022]; root of context hierarchy 四月 24, 2022 8:10:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] Animal的无惨构造 狗的无惨构造 Animal的无惨构造 Animal的无惨构造 这是Car的属性创建对象 Animal{id=1003, name='这里调用的是Car对象的内容小喵喵', age='20', color='白色', height=120, weight=23, x=X的未知数} 这里调用的是Car对象的内容小喵喵 四月 24, 2022 8:10:51 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2928854b: startup date [Sun Apr 24 20:10:51 CST 2022]; root of context hierarchy 四月 24, 2022 8:10:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] Animal的无惨构造 狗的无惨构造 Animal的无惨构造 Animal的无惨构造 这是Car的属性创建对象 20 1003 这里调用的是Car对象的内容小喵喵 小明 20 第二种方式Spring创建对象与赋值 Spring框架 利用Bean管理数据 四月 24, 2022 8:10:51 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b5a5ed1: startup date [Sun Apr 24 20:10:51 CST 2022]; root of context hierarchy 四月 24, 2022 8:10:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] Animal的无惨构造 狗的无惨构造 Animal的无惨构造 Animal的无惨构造 这是Car的属性创建对象 利用Spring文件赋值 狗的编号1002狗的姓名我是Spring框架的对象小狗狗的年龄9狗的色彩黑色狗的身高120狗的体重11调用Animal的独有属性狗的run 第一种方案java方式创建对象与赋值 Animal的无惨构造 Animal{id=1001, name='小鸟', age='2', color='red', height=170, weight=32, x=我是未知的属性} 动物编号1001动物姓名小鸟动物年龄2动物的身高170动物的体重32动物的色彩red动物的未知属性我是未知的属性 Process finished with exit code 0
package com.spring.test; import com.spring.entity.Student; import com.spring.entity.Teacher; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void one(){ //方式一:使用java代码方式创建对象 //Student student=new Student(); //方式二:使用Spring框架的xml配置文件方式创建对象 //<!--<bean id="填对象名" class="要创建对象的类的完整包名"></bean>--> //运行时,获得主配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student=ac.getBean("student",Student.class); System.out.println(student); System.out.println(student.getName()); System.out.println(student.getAge()); System.out.println(student.getPhone()); } @Test public void two(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Teacher teacher=ac.getBean("tea",Teacher.class); System.out.println(teacher.getName()+"--"+teacher.getAge()); System.out.println(teacher.getStudent().getName()+ "--"+teacher.getStudent().getAge()+ "--"+teacher.getStudent().getPhone()); } }
package com.spring.test; import com.spring.entity.Student; import com.spring.entity.Teacher; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void one(){ //方式一:使用java代码方式创建对象 //Student student=new Student(); //方式二:使用Spring框架的xml配置文件方式创建对象 //<!--<bean id="填对象名" class="要创建对象的类的完整包名"></bean>--> //运行时,获得主配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Student student=ac.getBean("student",Student.class); System.out.println(student); System.out.println(student.getName()); System.out.println(student.getAge()); System.out.println(student.getPhone()); } @Test public void two(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Teacher teacher=ac.getBean("tea",Teacher.class); System.out.println(teacher.getName()+"--"+teacher.getAge()); System.out.println(teacher.getStudent().getName()+ "--"+teacher.getStudent().getAge()+ "--"+teacher.getStudent().getPhone()); } }
第三种方案:IOC注解反转的第三种方式:不使用配置文件的方式。
package com.spring.test; import com.spring.entity.Student; import com.spring.utils.SpringUtil; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.Date; public class SpringTest { @Test public void one(){ //1.加载主配置文件 ApplicationContext ac=new AnnotationConfigApplicationContext(SpringUtil.class); Student stu = ac.getBean("stu", Student.class); System.out.println(stu.getName()+"--"+stu.getAge()); //Student stu1 = ac.getBean("stu", Student.class); //Spring框架中获得同一个类的多个对象,默认为单例模式,实际是一个对象 System.out.println(stu); //System.out.println(stu1); //对数据的关闭 ((AnnotationConfigApplicationContext) ac).close(); } @Test public void two(){ ApplicationContext ac=new AnnotationConfigApplicationContext(SpringUtil.class); Student b = ac.getBean("b", Student.class); System.out.println(b.getName()+"--"+b.getAge()); Date d = ac.getBean("d", Date.class); System.out.println(d); } }
package com.spring.utils; import com.spring.entity.Student; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import java.util.Date; /** * 第四种方式 * 该类用于替代Spring框架的主配置文件 * @Configuration:该注解用于替代xml文件中的文件头 */ @Configuration @ComponentScan("com.spring.entity") public class SpringUtil { //间接获得Student对象 @Bean(value = "b") public Student getStudent(){ return new Student(); } @Bean(value = "d") @Scope(value = "prototype") public Date getDate(){ return new Date(); } }
package com.spring.test; import com.spring.entity.Student; import com.spring.utils.SpringUtil; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.Date; public class SpringTest { @Test public void one(){ //1.加载主配置文件 ApplicationContext ac=new AnnotationConfigApplicationContext(SpringUtil.class); Student stu = ac.getBean("stu", Student.class); System.out.println(stu.getName()+"--"+stu.getAge()); //Student stu1 = ac.getBean("stu", Student.class); //Spring框架中获得同一个类的多个对象,默认为单例模式,实际是一个对象 System.out.println(stu); //System.out.println(stu1); //对数据的关闭 ((AnnotationConfigApplicationContext) ac).close(); } @Test public void two(){ ApplicationContext ac=new AnnotationConfigApplicationContext(SpringUtil.class); Student b = ac.getBean("b", Student.class); System.out.println(b.getName()+"--"+b.getAge()); Date d = ac.getBean("d", Date.class); System.out.println(d); } }
运行结果:
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=50219:E:\IDAE2020\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\IDAE2020\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;E:\IDAE2020\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\Spring0405\out\production\Spring0405;D:\Spring0405\web\WEB-INF\lib\hamcrest-core-1.3.jar;D:\Spring0405\web\WEB-INF\lib\junit-4.13.2.jar;D:\Spring0405\web\WEB-INF\lib\spring-aop-5.0.2.RELEASE.jar;D:\Spring0405\web\WEB-INF\lib\spring-beans-5.0.2.RELEASE.jar;D:\Spring0405\web\WEB-INF\lib\spring-context-5.0.2.RELEASE.jar;D:\Spring0405\web\WEB-INF\lib\spring-core-5.0.2.RELEASE.jar;D:\Spring0405\web\WEB-INF\lib\spring-expression-5.0.2.RELEASE.jar;D:\Spring0405\web\WEB-INF\lib\spring-jcl-5.0.2.RELEASE.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.spring.test.SpringTest 四月 24, 2022 6:51:03 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@71e7a66b: startup date [Sun Apr 24 18:51:03 CST 2022]; root of context hierarchy 创建对象 加载初始化数据 创建对象 加载初始化数据 No PAINS No Gains--5 com.spring.entity.Student@72a7c7e0 四月 24, 2022 6:51:05 下午 org.springframework.context.support.AbstractApplicationContext doClose 信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@71e7a66b: startup date [Sun Apr 24 18:51:03 CST 2022]; root of context hierarchy 销毁对象,释放内存 销毁对象,释放内存 四月 24, 2022 6:51:05 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@34b7ac2f: startup date [Sun Apr 24 18:51:05 CST 2022]; root of context hierarchy 创建对象 加载初始化数据 创建对象 加载初始化数据 No PAINS No Gains--5 Sun Apr 24 18:51:05 CST 2022 Process finished with exit code 0
Spring框架还有很多模块。