Announcement Announcement Module
Collapse
No announcement yet.
Annotation based factory method. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation based factory method.

    I just want to create a singleton bean and have defined a public static method getInstance for it. I am able to create a bean by defining the corresponding defintion in XML config file, but what if I wish to use annotation based defintion?

    I could not locate any alternative to defining the factory-method (which incidentally takes a string argument)

    Code:
    <bean id="configReader " class="com.me.ReadConfig"
    		scope="singleton" factory-method="getConfiguration">
    		<constructor-arg value="${index.config}" />
    </bean>
    This works fine!

    But, when I define
    @Autowired
    public static getConfiguration(String info)

    It fails, any-one has any ideas?

  • #2
    there is no factory-method equivalent using annotations. However, you should ask yourself if you really need static factory method to produce your bean. Spring beans are singletons by default so if all you need is for your bean to be a singleton, just define your bean the standard way (by xml definition, using constructor or setter injection for the string property, or by component-scanning and the @Value annotation for the property pickup, or by java config @Bean). Then just use your created singleton in your code by @Inject-ing or @Autowire-ing it.

    Comment


    • #3
      Found a way.

      Indeed what you say is true, a simple scope would allow the restriction of singleton without actually having the static factory-method and employing setters for the same. It is more like I stumbled upon this while trying out things.

      By the way I did find the way of doing it by using @Autowired for the static factory-method and using @Value for the parameter.

      Comment


      • #4
        Question

        I am trying to do the same thing, can you please exactly how you achieved this factory-method using @Value with @Autowired annotation?

        Thanks.

        Originally posted by samarthya View Post
        Indeed what you say is true, a simple scope would allow the restriction of singleton without actually having the static factory-method and employing setters for the same. It is more like I stumbled upon this while trying out things.
        By the way I did find the way of doing it by using @Autowired for the static factory-method and using @Value for the parameter.

        Comment


        • #5
          I dont remember much about how I went about doing it, but it was something like

          Code:
          @Component("A")
          @Scope(BeanDefinition.SCOPE_SINGLETON)
          public class A {
             @Autowired
          	public synchronized static A getA(
          			@Value(value = "${index.dir}") String dirLocation) {
          
          	}
          
          }

          Comment


          • #6
            The value can have any value you want in this case this is information I was picking from a property file.

            Comment

            Working...
            X