zoukankan      html  css  js  c++  java
  • Bean的实例化

    一.构造器实例化

          One.class类:

    public class One {
        public void say(){
            System.out.println("我是构造器实例化");
        }
    }

     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-4.3.xsd">
        <bean id="one" class="cn.chauncey.ioc.One"/>
    </beans>

      Test.class测试类:

    public class OneTest {
        public static void main(String[] args) {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            One one = (One) applicationContext.getBean("one");
            one.say();
        }
    }

      运行结果: 

    二.静态工厂方式实例化

    Two.class接口:

    public interface Two {
         public void say();
    }

    TwoImp接口的实现类:

    public class TwoImpl implements Two {
        @Override
        public void say() {
            System.out.println("我是静态工厂实例化");
        }
    }

    MyBeanFactory.class类:

    public class MyBeanFactory {
        public static TwoImpl createBean(){
            return new TwoImpl();
        }
    }

    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-4.3.xsd">
        <bean id="twoImpl" class="cn.chauncey.ioc.MyBeanFactory" factory-method="createBean"/>
    </beans>

    TwoTest.class测试类:

    public class TwoTest {
        public static void main(String[] args) {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            TwoImpl two = (TwoImpl) applicationContext.getBean("twoImpl");
            two.say();
    
        }
    }

    运行结果:

    三.实例工厂方式实例化

    Three.class类:

    public class Three {
       private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    MyBeanFactory.class类:

    public class MyBeanFactory {
        public Three createBean(){
            return new Three();
        }
    }

    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-4.3.xsd">
        <bean id="myBeanFactory" class="cn.chauncey.ioc.MyBeanFactory"/>
        <bean id="three" factory-bean="myBeanFactory" factory-method="createBean">
            <property name="name" value="我是实例工厂方式实例化"/>
        </bean>
    </beans>

    ThreeTest.class测试类:

    public class ThreeTest {
        public static void main(String[] args) {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            Three three = (Three) applicationContext.getBean("three");
            System.out.println(three.getName());
        }
    }

    运行效果:

  • 相关阅读:
    实时控制软件设计第一周作业-汽车ABS软件系统案例分析
    团队项目·冰球模拟器——任务间通信、数据共享等设计
    团队项目·冰球模拟器——cmake 自动化构建系统的配置文件的编写
    团队项目·冰球模拟器——文件结构设计
    团队项目·冰球模拟器——插值算法接口设计
    第四周作业
    第三周作业、实时操作系统µC/OS介绍及其它内容
    第二周作业、停车场门禁控制系统状态机
    Open Dynamics Engine for Linux 安装笔记
    第一周作业、典型实时控制系统案例分析
  • 原文地址:https://www.cnblogs.com/chaunceyji/p/10452492.html
Copyright © 2011-2022 走看看