bean实例化与设置注入

实验目的:

实验目的:

  1. 理解bean装配set方法装配的原理
  2. 掌握静态工厂和实例工厂实例化方法

 

 

实验场地及仪器、设备和材料:

场地:实验楼五楼教室

应用软件:eclipse javaee

实验训练内容(包括实验原理和操作步骤):

    

1定义一个学生类Student,在类中定义(id,name,required(必修课,用list实现),选修课(用map实现,可以录入专业选修,公共选修课)。

  1. 请用静态工厂实例化、设置注入完成对Student实例的装配。
  2. 请用实例工厂实例化,设置注入完成对Student实例的装配。

1,静态方法注入:

实体类Student.Java

package cn.huel.edu.lzz;

 

import java.util.List;

import java.util.Map;

 

public class Student {

               private String id;

               private String name;

               private List<String> required;//必修

               private Map<String , String > elective;//选修

              

 

               public String getId() {

                               return id;

               }

               public void setId(String id) {

                               this.id = id;

               }

               public String getName() {

                               return name;

               }

               public void setName(String name) {

                               this.name = name;

               }

               public List<String> getRequired() {

                               return required;

               }

               public void setRequired(List<String> required) {

                               this.required = required;

               }

               public Map<String, String> getElective() {

                               return elective;

               }

               public void setElective(Map<String, String> elective) {

                               this.elective = elective;

               }

               @Override

               public String toString() {

                               return "Student [id=" + id + ", name=" + name + ", required=" + required + ", Elective=" + elective + "]";

               }

               public Student(String id, String name, List<String> required, Map<String, String> elective) {

                               super();

                               this.id = id;

                               this.name = name;

                               this.required = required;

                               this.elective = elective;

               }

               public Student() {

                               super();

               }

              

              

              

}

StudentStaticFactory.java

package cn.huel.edu.lzz;

 

public class StudentStaticFactory {

  private static Student student = null;

 

  public static Student getStudent() {

    if (student==null) {

      student = new Student();

    }

    return student;

  }

  public void tostring() {

   

    System.out.println("静态工厂方法");

  }

}

StaticFactory.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="staticfactory" class="cn.huel.edu.lzz.StudentStaticFactory" factory-method="getStudent">

    <property name="name" value="王五"></property>

    <property name="id" value="0001"></property>

    <property name="required">

    <list>

    <value>东方不败</value>

    <value>任盈盈</value>

    <value>风清扬</value>

    </list>

    </property>

    <property name="elective">

    <map>

    <entry key="令狐冲" value="独孤九剑"></entry>

    <entry key="任我行" value="吸星大法"></entry>

    </map>

    </property>

  </bean>

</beans>

StaticBeanTest.java

package cn.huel.edu.lzz;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class StaticBeanFactoryTest {

  /**

   * 静态工厂实例化

   * @param args

   */

 public static void main(String[] args) {

  BeanFactory beanFactory = new ClassPathXmlApplicationContext("cn/huel/edu/lzz/StaticFactory.xml");

  Student student = beanFactory.getBean("staticfactory",Student.class);

  System.out.println(student);

 }

}

 

测试结果:

 

2,实例化方法注入:

StudentExampleFactory.java

package cn.huel.edu.examplefactory;

 

import cn.huel.edu.lzz.Student;

 

public class StudentExampleFactory {

  private static Student student = null;

  /**

   * 不使用静态方法

   * @return

   */

  public  Student getStudent() {

    if (student==null) {

      student = new Student();

    }

    return student;

  }

  public void tostring() {

   

    System.out.println("静态工厂方法");

  }

}

ExampleFactory.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="examplefactory" class="cn.huel.edu.examplefactory.StudentExampleFactory"></bean>

 

  <bean id="student" factory-bean="examplefactory"  factory-method="getStudent">

    <property name="name" value="王五"></property>

    <property name="id" value="0001"></property>

    <property name="required">

    <list>

    <value>东方不败</value>

    <value>任盈盈</value>

   

    </list>

    </property>

    <property name="elective">

    <map>

    <entry key="令狐冲" value="独孤九剑"></entry>

    <entry key="任我行" value="吸星大法"></entry>

    </map>

    </property>

  </bean>

 

 

 

</beans>

ExampleFactoryTest.java

package cn.huel.edu.examplefactory;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import cn.huel.edu.lzz.Student;

 

public class ExampleFactoryTest {

               /**

                * 实例化方法注入

                * @param args

                */

 public static void main(String[] args) {

               BeanFactory beanFactory = new ClassPathXmlApplicationContext("cn/huel/edu/examplefactory/ExampleFactory.xml");

               Student student = beanFactory.getBean(Student.class);

               System.out.println(student);

 }

}

测试结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值