Results 1 to 6 of 6

Thread: Annotation based factory method.

  1. #1
    Join Date
    Jul 2010
    Location
    India
    Posts
    19

    Angry 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. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    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.

  3. #3
    Join Date
    Jul 2010
    Location
    India
    Posts
    19

    Lightbulb 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.

  4. #4
    Join Date
    Dec 2012
    Posts
    1

    Default 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.

    Quote 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.

  5. #5
    Join Date
    Jul 2010
    Location
    India
    Posts
    19

    Default

    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) {
    
    	}
    
    }

  6. #6
    Join Date
    Jul 2010
    Location
    India
    Posts
    19

    Default

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •