Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 @Component and static factory method

If I'm writing a static factory method to create objects, how do I use the '@Component' annotation for that factory class and indicate (with some annotation) the static factory method which should be called to create beans of that class? Following is the pseudo-code of what I mean:

@Component
class MyStaticFactory
{
    @<some-annotation>
    public static MyObject getObject()
    {
        // code to create/return the instance
    }
}
like image 942
shrini1000 Avatar asked Mar 07 '12 09:03

shrini1000


People also ask

What is static factory method in Spring?

Static factory methods are those that can return the same object type that implements them. Static factory methods are more flexible with the return types as they can also return subtypes and primitives too. Static factory methods are used to encapsulate the object creation process.

Is @bean and @component same?

@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.

What is the difference between @configuration and @component in Spring boot?

3. How They Differ. The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications. Now, let's go through them in more detail.

Can we use @component and @service?

We can use @Component across the application to mark the beans as Spring's managed components. Spring will only pick up and register beans with @Component, and doesn't look for @Service and @Repository in general. @Service and @Repository are special cases of @Component.


2 Answers

I am afraid you can't do this currently. However it is pretty simple with Java Configuration:

@Configuration
public class Conf {

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

}

In this case MyStaticFactory does not require any Spring annotations. And of course you can use good ol' XML instead.

like image 147
Tomasz Nurkiewicz Avatar answered Sep 18 '22 16:09

Tomasz Nurkiewicz


You need to use the spring interface FactoryBean.

Interface to be implemented by objects used within a BeanFactory which are themselves factories. If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.

Implement the interface and declare a bean for it. For example :

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

Through @Component and component scanning, this class will be discovered. Spring will detect that it is a FactoryBean and expose the object you return from getObject as a bean (singleton if you specify it).

Alternatively, you can provide a @Bean or <bean> declaration for this FactoryBean class.

like image 43
ameen Avatar answered Sep 20 '22 16:09

ameen