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

 

posted @ 2020-03-29 20:32  wwww2  阅读(272)  评论(0编辑  收藏  举报