• 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--生命周期

  • 相关阅读:
    第十章、数据库运行维护与优化
    第九章、安全管理
    第八章、数据库后台编程技术
    第七章、高级数据库查询
    第六章、数据库及数据库对象
    第五章、UML与数据库应用系统
    第四章、数据库应用系统功能设计与实施
    struts2标签debug
    SSH框架 more than one namespace has been specificed without a prefix
    Http Status 404
  • 原文地址:https://www.cnblogs.com/wwww2/p/12593331.html
Copyright © 2020-2023  润新知