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