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例题】
推荐阅读
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)
- Java常见集合例题(1、集合的嵌套遍历。2、获取随机数。3三种方式实现对集合中存储对象的遍历)
- 笔记|扩展欧几里得算法+板子+例题
- 2.3例题--校门外的树--2808
- 图论|【启发式合并】例题 CodeForces-600E(子树颜色 树上众数)+ CodeForces - 1009F(每层节点数的众数)+ CSU - 1811(去边后,求颜色并集大小)
- 扩展欧几里得算法——例题3( 最大公约数问题1)
- 扩展欧几里得算法——例题4( 最大公约数问题2)
- 0830-扩展欧几里得算法+例题
- 扩展欧几里得例题(luogu_1082)
- 拓展欧几里得+例题~