zoukankan      html  css  js  c++  java
  • Spring 一二事(2)

    静态工厂方法及实例工厂的使用:

    applicationContext.xml:

     1    <!-- factory-method 是指调用静态工厂方法 -->
     2     <bean id="helloWorld2" class="com.lee.spring002.createobject.method.HelloWorldFactory"
     3         factory-method="getInstance"></bean>
     4 
     5     <!-- 实例工厂 -->
     6     <bean id="helloWorldFactory"
     7         class="com.lee.spring002.createobject.method.HelloWorldFactory2"></bean>
     8     <!-- factory-bean 是一个工厂bean -->
     9     <bean id="helloWorld3" factory-bean="helloWorldFactory"
    10         factory-method="getInstance"></bean>

    HelloWorldFactory.java

     1 package com.lee.spring002.createobject.method;
     2 
     3 import com.lee.spring001.createobject.HelloWorld;
     4 
     5 public class HelloWorldFactory {
     6 
     7     public static HelloWorld getInstance() {
     8         return new HelloWorld();
     9     }
    10 }

    HelloWorldFactory2.java

     1 package com.lee.spring002.createobject.method;
     2 
     3 import com.lee.spring001.createobject.HelloWorld;
     4 
     5 public class HelloWorldFactory2 {
     6 
     7     public HelloWorld getInstance() {
     8         return new HelloWorld();
     9     }
    10 }

    测试:

     1     @Test
     2     public void testHelloWorld_StaticFactory() {
     3 
     4         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     5         
     6         HelloWorld hello = (HelloWorld)context.getBean("helloWorld2");
     7         hello.hello();
     8     }
     9     
    10     @Test
    11     public void testHelloWorld_InstanceFactory() {
    12 
    13         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    14         
    15         HelloWorld hello = (HelloWorld)context.getBean("helloWorld3");
    16         hello.hello();
    17     }

     github地址:https://github.com/leechenxiang/maven-spring001-helloworld

  • 相关阅读:
    五种方式获取文件扩展名-转载未验证
    http状态代码-转载
    求两个时间的日期差-部分
    jsp相对路径解决方案
    OAUI前台设计(二)
    jsp生命周期
    OA办公页面设计
    Redis全局命令及数据结构
    Redis集群简介及部署
    sql中 in到底走不走索引
  • 原文地址:https://www.cnblogs.com/leechenxiang/p/5305288.html
Copyright © 2011-2022 走看看