spring中依赖注入的原理

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 03:54:42

spring中依赖注入的原理
spring中依赖注入的原理

spring中依赖注入的原理
用反射,给你举个简单的例子吧
为student实例,注入nameString
public class reflect
{
public static void main(String[] args)
{
student s = new student();
Class cl = s.getClass();
try
{
java.lang.reflect.Method m = cl.getMethod("setNameString", new Class[]{String.class});
m.invoke(s, "zhangsan");
System.out.println(s.getNameString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class student
{
private String nameString;
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
}