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

  • 相关阅读:
    机器学习:特征选择方法简介
    VS快捷键
    非常适用的Sourceinsight插件,提高效率【强力推荐】
    Linux静态库和共享库
    C/C++ 位域知识小结
    __BEGIN_DECLS 和 __END_DECLS
    C语言可变参数va_list
    mac的terminal快捷键
    linux进程、线程与cpu的亲和性(affinity)
    C++ Singleton (单例) 模式最优实现
  • 原文地址:https://www.cnblogs.com/leechenxiang/p/5305288.html
Copyright © 2011-2022 走看看