• Spring-创建对象的三种方式


     

                                                spring创建对象的三种方式

    1.通过无参构造函数

    2.静态工厂(了解)

    3.实例工厂(了解)

     -----------------------------------------------------------------

    准备:导包

               

    ------------------------------------------------------------------

    通过无构造函数

          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/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
      <bean id="p" class="com.Person"></bean>
    </beans>
    View Code

        Person.java

    package com;
    
    public class Person {
    
        public void say() {
            System.out.println("hello world....");
        }
      
    }
    View Code

       Test.java

    package com;
    
    import org.junit.Test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        //方法一:无参构造函数
        @Test
        public void test01() {
            //启动spring容器,读取src下applicationContext.xml文件
           ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
           //调用spring容器创建对象
           Person p = (Person) ac.getBean("p");
           p.say();
        }
    
        
    }
    View Code

    静态工厂

          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/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
            
      <bean id="f" class="com.factory.BeanFactory" factory-method="getS"></bean>
     
    </beans>
    View Code

         Persion.java

    package com;
    
    public class Person {
        public void say() {
            System.out.println("hello world....");
        }
      
    }
    View Code

        BeanFactory.java

    package com.factory;
    
    import com.Person;
    
    public class BeanFactory {
        public static Person getInstance() {
            return new Person();
        }
    }
    View Code

       Test.java

    package com;
    
    import org.junit.Test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        //方法二:静态工厂创建对象
        @Test
        public void test02() {
            //启动spring容器,读取src下的applicationContext.xml文件
           ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
           Person h = (Person) ac.getBean("f");
           h.say();
        }
        
    }
    View Code

                实例工厂(了解)

          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/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
            
     <bean id="factory" class="com.factory.BeanFactory"></bean>
     <bean id="p" factory-bean="factory" factory-method="getInstance"></bean>
    </beans>
    View Code

       Person.java

    package com;
    
    public class Person {
    
        public void say() {
            System.out.println("hello world....");
        }
      
    }
    View Code

    BeanFactory.java

    package com.factory;
    
    import com.Person;
    
    public class BeanFactory {
        
        public Person getInstance() {
            return new Person();
        }
    }
    View Code

    Test.java

    package com;
    
    import org.junit.Test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.factory.BeanFactory;
    
    public class Test{
        //方法三:通过实例化工厂来创建对象
        @Test
        public void test03() {
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            BeanFactory factory = (BeanFactory) ac.getBean("factory");
            Person p = (Person)ac.getBean("p");
            System.out.println(factory);
            System.out.println(p);
        }
        
    }
    View Code

          

                                                                                                                                                        下一篇:spring--生命周期

  • 相关阅读:
    win32: 静态控件(Static)
    malloc() 和 calloc()有啥区别
    win32: WM_PAINT 实现双缓冲缓图
    char 与 unsigned char的本质区别
    iconv: iconv_open(pToCharset, pFromCharset); 的附加参数//IGNORE
    c语言: 生成随机数
    深圳市住房公积金管理中心
    利用latex制作个人简历
    分布式系统概念与设计中文版(第三版)
    数据结构-红黑树
  • 原文地址:https://www.cnblogs.com/wwww2/p/12593331.html
Copyright © 2020-2023  润新知