Spring 学习之 二

您所在的位置:网站首页 spring创建对象的几种方式 Spring 学习之 二

Spring 学习之 二

2024-05-25 04:45| 来源: 网络整理| 查看: 265

    最近在系统的学习Spring,现在就Spring的一些知识进行总结。

    我们知道Spring是一个开放源代码的设计层面的框架,他主要解决的是业务逻辑层与其他各层之间松耦合的问题。

    Spring 有三个核心:

    1.IOC 控制反转

我们以前在A对象中创建B对象时,都是直接在A对象内部使用new B() 的方法创建对象,这样下去,A和B有很强的的耦合性。那么我们用到了Spring ,我们可以把创建对象和管理对象的控制前交给了Spring , 这样就叫做控制反转,也就说让Spring 去管理和创建对象。

    2.DI 依赖注入

Spring在使用构造器方法和set注入扥方式创建对象时,总会将属性自动设置为所需要的值的思想就是依赖注入的思想

    3.AOP 面向切面编程

我们知道OOP是面向对象编程,就是把世界万物都抽象成一个搞对象。

AOP是对OOP的扩展和延伸,就是把一些对象共有的部分抽象成切面,然后对该切面进行一些权限控制、事务管理、日志管理等共性的操作的思想就是AOP思想。

    那么如果使用Spring创建对象有哪几种方式呢?

    有三种:

构造器方法静态工厂方法实力工厂方法

前提条件:

1.创建一个web项目

2.引入Spring的jar包

    commons-io-2.4.jar    commons-logging.jar    spring-beans-3.2.8.RELEASE.jar    spring-context-3.2.8.RELEASE.jar    spring-core-3.2.8.RELEASE.jar    spring-expression-3.2.8.RELEASE.jar

3.在src下创建applicationContext.xml文件

    一.构造器方法

先创建一个Person类

package com.xljy.test.spring01; public class Person { private String name; private int id; public String getName() { System.out.println("getName"); return name; } public void setName(String name) { System.out.println("setName"); this.name = name; } public int getId() { System.out.println("getId"); return id; } public void setId(int id) { System.out.println("setId"); this.id = id; } public String toString(){ return "该person为id="+id+", name="+name+""; } } 这里没有写构造方法,在java中如果没有写构造方法,会默认有一个构造方法,如果已经写了一个无参构造方法,就不会默认创建无参构造方法。

所以此处我没有写任何一个构造方法,当然会默认创建一个无参的构造方法,下面我们就是用无参的构造方法来创建对象

下面是applicationContext.xml中的内容:

最简单的applicationContext.xml的写法,我们在此处声明了person bean

下面是测试类

package com.xljy.test.spring01; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPersion01 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) context.getBean("person"); System.out.println(person.toString()); } }

这里我们使用spring通过无参构造器来创建Person对象,toString()输出的结果为:

该person为id=0, name=null

看穿件对象时默认将属性设为初始值,int类型的初始值为0, String和引用类型的初始值为 null. 这就是依赖注入 DI的思想了;

并且在这里观察到setName()方法和setId()方法中的输出语句都没有输出,说明现在使用无参构造器的时候还没有用到set方法

假如我们在applicationContext.xml 配置文件的 person bean中加入参数设置,如下:

测试方法不变,运行后的结果为:

setNamesetId

该person为id=125, name=LiuChunfu

说明对象在创建的时候调用了set方法。这是使用无参构造函数创建对象的方式,下面我在Person中再假如无参构造函数看看:

其他的不变,输出结果为:

Person的无参构造函数setNamesetId

该person为id=125, name=LiuChunfu

又验证了我们所想:Spring通过无参构造器创建对象时,如果有参数设置就会调用set方法。

下面我们来看看使用有参构造器是一个什么样的情形。

我们在Person类中增加了有参构造方法,其他的不变,Person如下:

package com.xljy.test.spring01; public class Person { private String name; private int id; public Person(){ System.out.println("Person的无参构造函数"); } public Person(String name, int id){ System.out.println("Person类的有参构造方法"); this.name = name; this.id = id; } public String getName() { System.out.println("getName"); return name; } public void setName(String name) { System.out.println("setName"); this.name = name; } public int getId() { System.out.println("getId"); return id; } public void setId(int id) { System.out.println("setId"); this.id = id; } public String toString(){ return "该person为id="+id+", name="+name+""; } }

其他的都不变,直接运行测试类得到的结果还是:

Person的无参构造函数setNamesetId

该person为id=125, name=LiuChunfu

说明还是通过无参构造器来创建对象,那么如何通过有参构造器来创建对象呢?

修改applicationContext.xml文件如下:

其他的不变,运行测试类:

Person类的有参构造方法该person为id=123, name=LiuChunfu

这回直接运行有参构造方法了,没有使用set()方法。

二。静态工厂方法创建对象

新建一个静态工厂类方法 StaticPersonFactory

package com.xljy.test.spring01; public class StaticPersonFactory { public static Person2 createPerson(){ return new Person2(); } public static Person2 createPerson(String name, int id){ return new Person2(name, id); } }

这里既写了无参构造器,也写了有参构造器,方便看输出,又定义了一个Person2类,如下:

package com.xljy.test.spring01; public class Person2 { private String name; private int id; public Person2(){ System.out.println("Person2的无参构造函数"); } public Person2(String name, int id){ System.out.println("Person2类的有参构造方法"); this.name = name; this.id = id; } public String getName() { System.out.println("Person2---getName"); return name; } public void setName(String name) { System.out.println("Person2--setName"); this.name = name; } public int getId() { System.out.println("Person2---getId"); return id; } public void setId(int id) { System.out.println("Person2--setId"); this.id = id; } public String toString(){ return "该Person2--person为id="+id+", name="+name+""; } }

applicationContext.xml 中如下:

这里定义了person2的bean, 使用的静态工厂方法创建Person2类,我们来看一下,测试类:

package com.xljy.test.spring01; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPersion01 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // Person person = (Person) context.getBean("person"); // System.out.println(person.toString()); } }

我们把后面两句注释掉,只加载配置文件,看输出:

Person类的有参构造方法

Person2的无参构造函数

说明和Spring在初始化的时候配置文件中的两个bean都被创建了,静态工厂方法也默认使用了无参构造方法。

三。实例工厂方法创建对象

首先创建实例工厂类方法:PersonFactory, 如下:

package com.xljy.test.spring01; public class PersonFactory { public Person3 createPerson(){ return new Person3(); } }

然后创建一个Person3类

package com.xljy.test.spring01; public class Person3 { private String name; private int id; public Person3(){ System.out.println("Person3的无参构造函数"); } public Person3(String name, int id){ System.out.println("Person3类的有参构造方法"); this.name = name; this.id = id; } public String getName() { System.out.println("Person3--getName"); return name; } public void setName(String name) { System.out.println("Person3--setName"); this.name = name; } public int getId() { System.out.println("Person3-getId"); return id; } public void setId(int id) { System.out.println("Person3--setId"); this.id = id; } public String toString(){ return "该Person3--person为id="+id+", name="+name+""; } }

定义applicationContext.xml 

           

   

如下:

如此,测试类:

package com.xljy.test.spring01; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPersion01 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // Person person = (Person) context.getBean("person"); // System.out.println(person.toString()); } }

还是把后两句注释,我们来看初始化spring容器时,输出如下:

Person类的有参构造方法Person2的无参构造函数

Person3的无参构造函数

结果显示:

Person类是通过构造器的方式创建的

Person2类是通过静态工厂方法创建的

Person3类是通过实力工厂方法创建的

最后总结一下,spring创建对象的三种方式:

构造器

静态工厂方法

实例工厂方法



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3