【问题标题】:Spring 3 @Component and static factory methodSpring 3 @Component 和静态工厂方法
【发布时间】:2012-03-24 19:47:16
【问题描述】:

如果我正在编写一个静态工厂方法来创建对象,我如何为该工厂类使用“@Component”注释并指示(带有一些注释)应该调用的静态工厂方法来创建该工厂的 bean班级?以下是我的意思的伪代码:

@Component
class MyStaticFactory
{
    @<some-annotation>
    public static MyObject getObject()
    {
        // code to create/return the instance
    }
}

【问题讨论】:

    标签: java spring annotations static-factory


    【解决方案1】:

    恐怕你目前不能这样做。但是使用 Java 配置非常简单:

    @Configuration
    public class Conf {
    
        @Bean
        public MyObject myObject() {
            return MyStaticFactory.getObject()
        }
    
    }
    

    在这种情况下,MyStaticFactory 不需要任何 Spring 注释。当然,您也可以使用良好的 XML。

    【讨论】:

      【解决方案2】:

      需要使用spring接口FactoryBean

      BeanFactory 中使用的对象实现的接口 本身就是工厂。如果一个bean实现了这个接口,它就是 用作对象暴露的工厂,而不是直接用作 bean 将自己暴露的实例。

      实现接口并为其声明一个bean。例如:

      @Component
      class MyStaticFactoryFactoryBean implements FactoryBean<MyStaticFactory>
      {
          public MyStaticFactory getObject()
              MyStaticFactory.getObject();
          }
          public Class<?> getObjectType() {
              return MyStaticFactory.class;
          }
          public boolean isSingleton() {
              return true;
          }
      }
      

      通过@Component和组件扫描,会发现这个类。 Spring 将检测到它是 FactoryBean 并将您从 getObject 返回的对象公开为 bean(如果您指定它,则为单例)。

      或者,您可以为此FactoryBean 类提供@Bean&lt;bean&gt; 声明。

      【讨论】:

        【解决方案3】:

        豆子:

         public class MyObject {
        
                private String a;
        
                public MyObject(String a) {
                    this.a = a;
                }
        
                @Override
                public String toString() {
                    return a;
                }
            }
        

        FactoryBean:

        @Component
        public class MyStaticFactory implements FactoryBean<MyObject> {
        
        
            @Override
            public MyObject getObject() throws Exception {
                return new MyObject("StaticFactory");
            }
        
            @Override
            public Class<?> getObjectType() {
                return MyObject.class;
            }
        
            @Override
            public boolean isSingleton() {
                return true;
            }
        
        }
        

        用途:

        @Component
        public class SomeClass{
        
        
            @Autowired
            MyObject myObject;
        }
        

        【讨论】:

          【解决方案4】:

          Spring boot:静态工厂方法:

          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.boot.CommandLineRunner;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;
          import org.springframework.stereotype.Component;
          
          enum ParsersConst {
              bofa, jpm, wellsforgo
          }
          
          interface Parser {
              String readFromFile(String file);
          }
          
          class JPM implements Parser {
              @Override
              public String readFromFile(String file) {
                  System.out.println("From JPM Parser");
                  return "JPM";
              }
          }
          
          class Bofa implements Parser {
              @Override
              public String readFromFile(String file) {
                  System.out.println("From Bofa Parser");
                  return "BOFA";
              }
          }
          
          class WellsForgo implements Parser {
              @Override
              public String readFromFile(String file) {
                  System.out.println("From Wellsforgo Parser");
                  return "WellsForgo";
              }
          }
          
          
          class ParserCreator {
          
              public static Parser createParser(ParsersConst parsConst) {
                  if (ParsersConst.bofa.equals(parsConst)) {
                      return new Bofa();
                  } else if (ParsersConst.jpm.equals(parsConst)) {
                      return new JPM();
                  } else if (ParsersConst.wellsforgo.equals(parsConst)) {
                      return new WellsForgo();
                  }
                  throw new IllegalArgumentException("Unknown Parser");
              }
          }
          @Configuration
          class ParserConfig{
              @Bean
              public Parser bofa() {
                  return ParserCreator.createParser(ParsersConst.bofa);
              }
              @Bean
              public Parser wellsforgo() {
                  return ParserCreator.createParser(ParsersConst.wellsforgo);
              }
              @Bean
              public Parser jpm() {
                  return ParserCreator.createParser(ParsersConst.jpm);
              }
          }
          
          @Component
          public class StaticFacotryDemo implements CommandLineRunner{
              @Autowired
              private ApplicationContext context;
              @Override
              public void run(String... args) throws Exception {
          
                      Parser parser = (Parser) context.getBean(ParsersConst.jpm.toString());
                      System.out.println(parser);
                      System.out.println(parser.readFromFile("jan_stmt.pdf"));
          
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多