Spring 创建对象的问题,不同构造方法创建对象,使用工厂类类获取对象、单例或多例、延迟创建问题

创建实体类Student

package star.july.b_ioc;
public class Student {
          private String name;
          private int age;
          public Student() {
                   System.out.println("执行了Student构造方法");
          }
          public Student(String name, int age) {
                   System.out.println("调用了有参构造方法");
                   this.name = name;
                   this.age = age;
          }
          @Override
          public String toString() {
                   return "Student [name=" + name + ", age=" + age + "]";
          }
          
          
}

再创建工厂类,

package star.july.b_ioc;
public class StudentFactory {
          
          public StudentFactory() {
//                 new Student();  
                   System.out.println("调用StudentFactory构造方法");
          }
          
          public boolean setHaha(){
                   System.out.println("哈哈");
                   return true;
          }
          
          public Student getStudent(){
                   System.out.println("进入了StudentFactory的getStudent()方法");
                   return new Student();
          }
          
          public static Student getStu(){
                   System.out.println("调用静态方法");
                   return new Student();
          }
}


配置Spring的xml,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"
         default-lazy-init="true"
        >
<!--         <beanms default-lazy-init="true"/> -->
          <!-- 配置一个javabean
                   id:对象的唯一标记
                   class:需要创建的对象
                    -->
                    <!-- 使用无参构造方法 -->
        <!-- <bean id = "students" class="star.july.b_ioc.Student">
        </bean>  -->
        <!--
          有参方法
          index:构造方法的参数索引,从0开始
          value:需要传递的参数值
          type:参数类型
         -->
      <!--   <bean id ="students" class="star.july.b_ioc.Student">
          <constructor-arg index="0"
                   value="小火"
                   type="String"
          >
                   </constructor-arg>
          <constructor-arg index="1"
                   value="20"
                   type="int"
          >
                   </constructor-arg>
        </bean> -->
       
        <!-- 先创建工厂类的成员方法获取对象 -->
          <!-- 1、先创建工厂类对象 -->
        <bean  id="factory" class="star.july.b_ioc.StudentFactory" >
        </bean>
                   <!-- 2、调用工厂类的成员方法
                             factory-bean:使用哪个工厂类的对象
                             factory-method:使用工厂类的哪个方法
                   -->       
                    <bean id="invocation" factory-bean="factory" factory-method="setHaha" ></bean>
                   
                   
                   <!-- 调用工厂类的静态方法 -->
                   <bean  id="invokeStatic"  class="star.july.b_ioc.StudentFactory" factory-method="getStu"  ></bean>
                   
                   <!-- 对象单例子
                             单例:singleton(默认)
                             多例:prototype
                    -->
                   <bean  id="s1"  class="star.july.b_ioc.StudentFactory"  scope="prototype" ></bean>
                   
                   <!--
                             对象的延迟创建问题
                             lazy-init:是否延迟加载(创建)对象
                                      false:在初始化工厂的时候(调用getBean()方法
                                      true:在调用getBean()的时候创建对象
                             注意:该方法只能用在单例子
                    -->
                   <bean  id="s"  class="star.july.b_ioc.StudentFactory"  scope="singleton" lazy-init="true" ></bean>
</beans>


最后分别测试

package star.july.b_ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
          public static void main(String[] args) {
                   ApplicationContext ac = new ClassPathXmlApplicationContext("/star/july/b_ioc/applicationContext.xml");
//                Student s = (Student)ac.getBean("students");
                   StudentFactory s = (StudentFactory) ac.getBean("s");
                   //调用静态方法
//                StudentFactory s = (StudentFactory) ac.getBean("invokeStatic");
          }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值