Spring — Static variable Dependency Injection | Code Factory

Spring Tutorial Index Page: Link

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" />

Create Java Project

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="c" class="com.codeFactory.beans.Car" />

<!-- static variable DI -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
<property name="staticMethod" value="com.codeFactory.beans.Car.setCompanyName" />
<property name="arguments">
<list>
<value>JLR</value>
</list>
</property>
</bean>
</beans>
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Car {
private static String companyName;
public static void setCompanyName(String companyName) {
Car.companyName = companyName;
}
public static void print() {
System.out.println("Company Name " + companyName);
}
}
package com.codeFactory.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.beans.Car;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
Car t = (Car) context.getBean("c");
t.print();
}
}
Company Name JLR

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store