Spring框架依赖注入的一个简单应用

本程序是我原创代码,不要吐槽。

步骤一:

创建一个pen接口和一个实现pen接口的pencil类。

public interface pen {
public void write();
}


public class pencil implements pen{
public void write() {
System.out.println("人调用笔的方法写字");
}
}

步骤2:

定义一个person接口,Me类为实现该接口的类。

public interface Person {
public void write() ;
}

public class Me implements Person{
public void setP(pen p) {
this.p = p;
}
pen p;

public void write() {
p.write();
}

}

步骤3:

创建一个测试类。

public class Test {
public static void main(String args[])throws Exception{
ApplicationContext context=new ClassPathXmlApplicationContext
("applicationContext.xml");
Me m=(Me)context.getBean("person");
m.write();
}
}

步骤4:

配置spring框架

Spring框架依赖注入的一个简单应用_第1张图片

代码完成:

运行。




你可能感兴趣的:(spring,框架,依赖注入的一个简单例子)