Dependency Injection for Static method
-------------------------------------------------
package com.StatVariableDI;
public class Car {
private static String carname;
public static void setCarname(String carname) {
Car.carname = carname;
}
public static void printCarname(){
System.out.println(" Car Name :"+ carname);
}
}
-------------------------------------------------
package com.StatVariableDI;
public class Car {
private static String carname;
public static void setCarname(String carname) {
Car.carname = carname;
}
public static void printCarname(){
System.out.println(" Car Name :"+ carname);
}
}
---------------------------------------
package com.StatVariableDI;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
//MethodInvokingFactoryBean
ApplicationContext a=new ClassPathXmlApplicationContext("ap_StatVariableDI.xml");
Car c=(Car)a.getBean("c");
c.printCarname();
}
}
---------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="com.StatVariableDI.Car.setCarname"></property>
<property name="arguments" >
<list>
<value>Mustang</value>
</list>
</property>
</bean>
<bean id="c" class="com.StatVariableDI.Car" >
<!-- <property name="carname"> good</property> -->
</bean>
</beans>
Comments
Post a Comment