Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Spring Bean for factory method requiring MyClass.class parameter

I'm trying to inject a java.util.prefs.Preferences bean in to my master controller. The controller looks like:

@Controller
class MyController {
    @Autowired
    private Preferences preferences;
}

The application-context.xml file creates the bean for java.util.prefs.Preferences. It uses a factory method so I have the following entry for creating the bean:

<bean id="preferences" class="java.util.prefs.Preferences" factory-method="userNodeForPackage" />

Preferences.userNodeForPackage(param) takes for a parameter the class related to the Preference. In this case Spring needs to create the bean by performing the call:

Preferences.userNodeForPackage(MyController.class);

How can a class be passed in to a spring bean instantiated with a factory method? Thanks

Environment information:

Java 7
Spring 3.1
like image 767
Austin Haws Avatar asked Aug 26 '13 14:08

Austin Haws


People also ask

How do you get the BeanFactory reference in any class?

I can have my class implement BeanFactoryAware to get a reference to my beanfactory. Then I can do beanFactory. getBean("name"); to get access to a single bean.

Is it possible to initiate a non-static factory method to Spring?

Spring provides an option to inject dependency using factory-method along with factory-bean attributes in case of non-static factory methods.

How many ways we can inject bean in Spring?

As per Java Annotation Configuration, Dependency Injection can be performed in three different ways.


2 Answers

You can specify the constructor-arg element

<bean id="preferences" class="java.util.prefs.Preferences" factory-method="userNodeForPackage">
    <constructor-arg type="java.lang.Class" value="com.path.MyController" />
</bean>

This is explained in the official documentation here, section 5.4.1.

Arguments to the static factory method are supplied via elements, exactly the same as if a constructor had actually been used. The type of the class being returned by the factory method does not have to be of the same type as the class that contains the static factory method, although in this example it is. An instance (non-static) factory method would be used in an essentially identical fashion (aside from the use of the factory-bean attribute instead of the class attribute), so details will not be discussed here.

like image 78
Sotirios Delimanolis Avatar answered Oct 03 '22 19:10

Sotirios Delimanolis


Well I don't know the xml based configuration way but I can tell you how you can instantiate it via Configuration class.

@Configuration
public class Config {
    @Bean(name="preferences")
    public java.util.prefs.Preferences preferences() {
        // init
        return java.util.prefs.Preferences.userNodeForPackage(YourExpectedClass.class);
    }
}

P.S. :

You will need to add your configuration class/package for scanning either in web.xml if you are using complete annotation based approach [contextClass=org.springframework.web.context.support.AnnotationConfigWebApplicationContext] or in your config file as below :

<context:component-scan base-package="com.comp.prod.conf" />
like image 42
Nandkumar Tekale Avatar answered Oct 03 '22 17:10

Nandkumar Tekale