Java-Spring例题

Java-Spring例题 Java-Spring例题
文章图片

Java-Spring例题
文章图片

dao包下的TestDIDao.java

package dao; public interface TestDIDao { public void sayHello(); }

dao包下的TestDIDaoImpl.java
package dao; public class TestDIDaoImpl implements TestDIDao{@Override public void sayHello() { // TODO Auto-generated method stub System.out.println("TestDIDao say:Hello,Study Hard!"); }}

service包下的TestDIService.java
package service; public interface TestDIService { public void sayHello(); }

service包下的TestDIServiceImpl.java
package service; import dao.TestDIDao; import dao.TestDIDaoImpl; public class TestDIServiceImpl implements TestDIService { private TestDIDao t; public TestDIServiceImpl(TestDIDao t) { this.t=t; }@Override public void sayHello() { // TODO Auto-generated method stub t.sayHello(); System.out.println("TestDIService 构造方法 注入 say:Hello,Study hard!"); }}

service包下的TestDIServiceImpl1.java
package service; import dao.TestDIDao; public class TestDIServiceImpl1 implements TestDIService{ private TestDIDao t1; public void setT1(TestDIDao t1) { this.t1 = t1; }@Override public void sayHello() { t1.sayHello(); System.out.println("TestDIService setter方法注入 say: Hello,study hard!"); } }

test包下的TestDI.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.TestDIService; public class TestDI { public static void main(String[] args) { //初始化Spring容器ApplicationContext,加载配置文件 ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml"); //通过容器获取testDIService实例,测试构造方法注入 TestDIService ts = (TestDIService)appCon.getBean("testDIService"); ts.sayHello(); TestDIService ts1 = (TestDIService)appCon.getBean("testDIService1"); ts1.sayHello(); } }

dao包下的Leg.java
package dao; public interface Leg { public void run(); }

dao包下的LegImpl.java
package dao; public class LegImpl implements Leg{@Override public void run() { // TODO Auto-generated method stub System.out.println("健步如飞"); } }

service包下的People.java
package service; public interface People { public void run(); }

service包下的PeopleImpl.java
package service; import dao.Leg; public class PeopleImpl implements People{ private Leg l; public PeopleImpl(Leg l) { this.l=l; }@Override public void run() { // TODO Auto-generated method stub l.run(); System.out.println("People 构造方法 注入 leg"); } }

service包下的PeopleImpl1.java
package service; import dao.Leg; public class PeopleImpl1 implements People{private Leg l1; public void setL1(Leg l1) { this.l1 = l1; }@Override public void run() { // TODO Auto-generated method stub l1.run(); System.out.println("People setter方法 注入 leg"); } }

test包下的TestPeople.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.People; public class TestPeople { public static void main(String[] args) { ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml"); People p=(People)appCon.getBean("people"); p.run(); People p1=(People)appCon.getBean("people1"); p1.run(); } }

applicationContext.xml
【Java-Spring例题】

    推荐阅读