zoukankan      html  css  js  c++  java
  • Spring:基于配置文件的创建对象的各种方式

    在Spring3.0之前,Spring主要创建对象的方法是基于配置文件的,即在配置文件中为对象进行注册,并且可以在配置文件当中为对象的字段或者称之为属性值进行赋值,接下来首先介绍基于配置文件的创建对象的方式。

    1.1 基于配置文件的无参数构造函数的对象创建

    package bjtu.wellhold.testSpring;
    
    //当类当中没有任何构造函数的时候,默认就是一个无参数的public的构造函数会被生成,如果一旦有任何一个构造函数
    //则该默认的构造函数不会被生成。
    public class Person {
    
        private String name;
        private Integer id;
    
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", id=" + id + "]";
        }
    
    }

    这里有一点需要特别强调的,在只有无参数构造函数的情况下(或默认不写),Spring在通过setter方式进行注入的时候,无论是否添加属性的注入,Spring都会去寻找无参数的构造函数,通过这个无参数构造函数去进行注入,而在该Person的Pojo类当中,没有显式声明任何构造函数(有参和无参的),在java当中JVM会默认为这个类生成一个无参数的构造函数,所以该类对于Spring是可用的。

    <?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">
    
       <bean class="bjtu.wellhold.testSpring.Person" id="person">
           <property name="name" value="LiuChunfu"></property>
           <property name="id" value="125"></property>
       </bean>
    
    </beans>

    这是Spring的配置文件,通过Pojo类当中的setter方法为对象的属性进行了注入,并且创建了该Pojo对象的实例保存在Spring的IOC容器当中,等待其他类或者代码块进行调用。

    package bjtu.wellhold.testSpring;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class testUnit {
    
        public static void main(String[] args) {
            
            ApplicationContext cfg=new ClassPathXmlApplicationContext("Config.xml");
            Person person=(Person)cfg.getBean("person");
            System.out.println(person);
        }
    }

    这是测试代码块,可以看到打印出来的person的具体信息。

    1.2 基于配置文件的带参数构造函数的对象创建

    package bjtu.wellhold.testSpring;
    
    public class Person {
    
        private String name;
        private Integer id;
        
        public Person(String name, Integer id) {
            super();
            this.name = name;
            this.id = id;
        }
        
        @Override
        public String toString() {
            return "Person [name=" + name + ", id=" + id + "]";
        }
    
    }

    代码当中提供了一个有参数的构造方法。

    <?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">
    
        <bean class="bjtu.wellhold.testSpring.Person" id="person">
            <constructor-arg name="id" value="123"></constructor-arg>
            <constructor-arg name="name" value="LiuChunfu"></constructor-arg>
        </bean>
    
    </beans>

    而在spring的配置文件当中,则是通过标签<constructor-arg>去为对象注入属性值,其中name字段对应的是pojo类构造方法当中的形参名称。

    测试代码与1.1一样,此处就不重复上代码了。

     

     2.1 基于配置文件的静态工厂方法的仅有无参数构造函数的对象创建

    package bjtu.wellhold.testSpring;
    
    
    public class Person {
    
        private String name;
        private Integer id;
    
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", id=" + id + "]";
        }
    
    }

    在该pojo类当中,仅有默认不写的无参数构造函数,所以我们要对其进行属性值注入,仅能通过setter的方法进行注入,即和1.1的形式类似,但是这里我们是通过工厂的静态方法的形式去进行的,所以这里我们还需要一个工厂类

    package bjtu.wellhold.testSpring;
    
    public class PersonStaticFactory {
    
        public static Person createPerson() {
            return new Person();
        }
    
    }

    这个工厂类仅有一个方法,就是creatPerson,即返回一个Person对象的实例,而这个实例化的方法调用的是Person类当中默认的无参数构造函数,那么这时候我们来思考一个问题,能为这个Person实例注入属性值嘛?答案是肯定的,即与1.1当中的方法类似,通过标签<property name="xxxx" value="yyy"></property>去通过Setter去进行注入即可。配置文件如下:

    <?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">
    
     <bean id="person" class="bjtu.wellhold.testSpring.PersonStaticFactory" factory-method="createPerson">
        <property name="id" value="123"></property>
        <property name="name" value="wellhold"></property>
     </bean>
    
    </beans>

    这里要简单解释一下,与1.1不同,这里是通过PersonStaticFacotory这个工厂类当中的CreatePerson方法去进行Person实例化的,在1.1当中则是通过Spring直接去实例化Person类的,方式是不同的。但是属性值注入的方式则是一样的。

    测试模块的代码与1.1相同,这里不在重复。

     2.2 基于配置文件的静态工厂方法的有参数构造函数的对象创建

    package bjtu.wellhold.testSpring;
    
    public class Person {
    
        private String name;
        private Integer id;
        
        public Person(String name, Integer id) {
            super();
            this.name = name;
            this.id = id;
        }
        
        public Person()
        {
            
        }
        
        @Override
        public String toString() {
            return "Person [name=" + name + ", id=" + id + "]";
        }
    
    }

    这个Pojo类当中含有两个构造函数,一个是全参的构造函数,一个是无参的构造函数,我们就可以通过构造函数去为Person的实例进行属性值注入,所以这里不再需要setter方法,并且我们的工厂类当中,应该也对应两个构造函数的createPerson的方法,代码如下:

    package bjtu.wellhold.testSpring;
    
    public class PersonStaticFactory {
    
        public static Person createPerson() {
            return new Person();
        }
        
        public static Person createPerson(Integer id,String name){
            return new Person(name,id);
        }
    
    }

    通过配置文件当中的<constructor-arg name="xxxx" value="yyyy"><constructor-arg>标签去进行属性值的注入,代码如下:

     <bean id="person" class="bjtu.wellhold.testSpring.PersonStaticFactory" factory-method="createPerson">
            <constructor-arg name="id" value="123"></constructor-arg>
            <constructor-arg name="name" value="LiuChunfu"></constructor-arg>
     </bean>

    这里要额外的提醒一点,在使用标签<constructor-arg name="xxxx" value="yyyy"><constructor-arg>的时候,要与工厂类当中所有的静态方法的形参对应,即要调用无参数的createPerson,则无需使用该标签,要调用全参数的createPerson则需要将所有的参数都填上,否则会报错误。

     3.1 基于配置文件的工厂方法的无参数构造函数的对象创建

    使用工厂方法与使用工厂静态方法的唯一的区别就在于,因为工厂方法没有设置成静态的,所以再使用工厂之前,需要通过Spring去创建一个工厂的实例,才可以去调用该工厂实例的方法,创建工厂的实例相信从1.1当中就可以知道如何去创建,之后再通过已经被Spring管理起来的工厂实例去创建Person实例即可,其他的和工厂静态方式没有什么不同。

    package bjtu.wellhold.testSpring;
    
    public class Person {
    
        private String name;
        private Integer id;
        
        public Person(String name, Integer id) {
            super();
            this.name = name;
            this.id = id;
        }
        
        public Person()
        {
            
        }
        
        @Override
        public String toString() {
            return "Person [name=" + name + ", id=" + id + "]";
        }
    
    }
    package bjtu.wellhold.testSpring;
    
    public class PersonStaticFactory {
    
        public static Person createPerson() {
            return new Person();
        }
        
        public static Person createPerson(Integer id,String name){
            return new Person(name,id);
        }
    
    }
    <?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">
    
     <bean id="personFactory" class="bjtu.wellhold.testSpring.PersonFactory"></bean>
    
     <bean id="person" factory-bean="personFactory" factory-method="createPerson">
            <constructor-arg name="id" value="123"></constructor-arg>
            <constructor-arg name="name" value="wellhold"></constructor-arg>
     </bean>
    
    </beans>

    注意,与使用静态方法不同的一点在于,在生成Person实例的时候,使用的标签从class变成了factory-bean,这里要注意一下。除了使用构造函数进行属性注入,也可以使用setter去进行属性注入,这里就不在重复了,形式可以参考章节2当中的内容去自行进行尝试。

    本文主要参考了:http://www.cnblogs.com/LiuChunfu/p/5574383.html,并且通过自己的一些实践,转换成自己的思路和语言表述并记录。

  • 相关阅读:
    渐入效果
    单一元素颜色渐变
    JS中同名函数有效执行顺序
    jquery多重条件选择器
    Oracle表空间常用查询
    jquery如何获取span的文本?
    相关名词浅析
    html拼接字符串中特殊字符(‘ “ 等的转义问题)
    今日头条2017校园招聘、暑假实习内推邀请码
    [转]宏定义和函数调用的区别
  • 原文地址:https://www.cnblogs.com/WellHold/p/6635516.html
Copyright © 2011-2022 走看看