Spring核心面向接口编程小程序

来源:互联网 发布:淘宝隐藏导航代码 编辑:程序博客网 时间:2024/05/22 01:33

16.1新建一个IDemo1的接口类;

package com.eduask.mypack1;

public interface IDemo1 {

public void testDemo1();

}

16.2新建一个Demo1的类,实现IDemo1接口;

package com.eduask.mypack1;

//Demo1实现IDemo1接口;

public class Demo1 implements IDemo1 {

private int id;

private String name;

private String pwd;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

@Override

public void testDemo1() {

// TODO Auto-generated method stub

System.out.println("id="+id+"  name="+name+"   pwd="+pwd);

}

}

16.3新建一个TestDemo1的测试类;

package com.eduask.mypack1;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo1 {

public static void main(String[] args) {

ClassPathXmlApplicationContext cx=new ClassPathXmlApplicationContext("entityXmlmypack1/demo1.xml");

Demo1 demo1=(Demo1) cx.getBean("demo1");

demo1.setId(1);

demo1.setName("tom");

demo1.setPwd("123456");

demo1.testDemo1();

}

}

16.3 新建一个demo1.xml的配置文件;

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

>

<!-- 模板bean -->

<bean id="base" abstract="true"/>

<bean id="demo1" class="com.eduask.mypack1.Demo1" parent="base" scope="prototype"/>

</beans>

16.5 程序运行如下:

id=1 name=tom pwd=123456

0 0