di(依赖注入)和Ioc(控制反转)
情景1:
新建一个项目并且导入spring的jar包,需要导入的jar包有
定义一个IHelloDao的接口,并且在接口里面写一个方法
package dao;
public interface IHelloDao {
public String sayHi(String name);
}
新建XML文件,在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-4.3.xsd
">
<bean id="helloDao" class="impl.HelloDaoImpl"></bean>
</beans>
在测试类中测试
package test;
import dao.IHelloDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
test1();
}
private static void test1() {
//加载spring配置文件
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
//IHelloDao hd = (IHelloDao) ac.getBean("helloDao");
IHelloDao hd = ac.getBean("helloDao",IHelloDao.class);
System.out.println(hd.sayHi("阿伟"));
}
}
情景2:
新建地址和学生的实体类
package entity;
public class Address {
private String homeaddress;
private String schooladdress;
public String getHomeaddress() {
return homeaddress;
}
public void setHomeaddress(String homeaddress) {
this.homeaddress = homeaddress;
}
public String getSchooladdress() {
return schooladdress;
}
public void setSchooladdress(String schooladdress) {
this.schooladdress = schooladdress;
}
public Address(String homeaddress, String schooladdress) {
this.homeaddress = homeaddress;
this.schooladdress = schooladdress;
}
public Address() {
}
}
package entity;
public class StudentInfo {
private String sid;
private String name;
private int age;
private Address address;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public StudentInfo(String sid, String name, int age, Address address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public StudentInfo() {
}
public StudentInfo(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
}
新建IStudentInfoDao接口
package dao;
import entity.StudentInfo;
public interface IStudentInfoDao {
public void addStudent(StudentInfo s);
public void findStudent();
}
新建StudentInfoDaoImpl类实现IStudentInfoDao接口
package impl;
import dao.IStudentInfoDao;
import entity.StudentInfo;
public class StudentInfoDaoImpl implements IStudentInfoDao {
@Override
public void addStudent(StudentInfo s) {
System.out.println("执行添加学生的业务逻辑");
}
@Override
public void findStudent() {
System.out.println("执行查询学生的业务逻辑");
}
}
在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-4.3.xsd
">
<bean id="address" class="entity.Address">
<property name="homeaddress" value="翻斗花园"></property>
<property name="schooladdress" value="翻斗幼儿园"></property>
</bean>
<bean id="studentInfo" class="entity.StudentInfo">
<property name="sid" value="1"></property>
<property name="name" value="阿伟"></property>
<property name="age" value="20"></property>
<property name="address" ref="address"></property>
</bean>
<bean id="studentInfoDao" class="impl.StudentInfoDaoImpl"></bean>
</beans>
进行测试
package test;
import conroller.StudentController;
import dao.IHelloDao;
import entity.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;
public class Test {
public static void main(String[] args) {
test2();
}
private static void test2() {
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
StudentInfo s = ac.getBean("studentInfo", StudentInfo.class);
System.out.println("您的地址为:"+s.getAddress().getSchooladdress());
}
}
结果为
控制
新建service类
package service;
import dao.IStudentInfoDao;
import entity.StudentInfo;
public class StudentService {
private IStudentInfoDao iStudentInfoDao;
public IStudentInfoDao getiStudentInfoDao() {
return iStudentInfoDao;
}
public void setiStudentInfoDao(IStudentInfoDao iStudentInfoDao) {
this.iStudentInfoDao = iStudentInfoDao;
}
public void addStudent(StudentInfo s) {
iStudentInfoDao.addStudent(s);
}
public void findStudent() {
iStudentInfoDao.findStudent();
}
}
在XML文件中配置
<bean id="studentInfoDao" class="impl.StudentInfoDaoImpl"></bean>
<bean id="studentService" class="service.StudentService" autowire="byType">
<property name="iStudentInfoDao" ref="studentInfoDao"></property>
</bean>
测试
package test;
import conroller.StudentController;
import dao.IHelloDao;
import entity.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;
public class Test {
public static void main(String[] args) {
//test1();
//test2();
test3();
}
private static void test3() {
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
StudentService ss = ac.getBean("studentService",StudentService.class);
ss.findStudent();
}
private static void test2() {
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
StudentInfo s = ac.getBean("studentInfo", StudentInfo.class);
System.out.println("您的地址为:"+s.getAddress().getSchooladdress());
}
private static void test1() {
//加载spring配置文件
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
//IHelloDao hd = (IHelloDao) ac.getBean("helloDao");
IHelloDao hd = ac.getBean("helloDao",IHelloDao.class);
System.out.println(hd.sayHi("阿伟"));
}
}
新建一个StudentController类
package conroller;
import entity.StudentInfo;
import service.StudentService;
public class StudentController {
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public String addStudent(){
studentService.addStudent(new StudentInfo());
return null;
}
public String findStudent(){
studentService.findStudent();
return null;
}
}
测试
package test;
import conroller.StudentController;
import dao.IHelloDao;
import entity.StudentInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;
public class Test {
public static void main(String[] args) {
//test1();
//test2();
//test3();
test4();
}
private static void test4() {
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
StudentController sc = ac.getBean("studentController",StudentController.class);
sc.findStudent();
}
private static void test3() {
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
StudentService ss = ac.getBean("studentService",StudentService.class);
ss.findStudent();
}
private static void test2() {
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
StudentInfo s = ac.getBean("studentInfo", StudentInfo.class);
System.out.println("您的地址为:"+s.getAddress().getSchooladdress());
}
private static void test1() {
//加载spring配置文件
ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
//IHelloDao hd = (IHelloDao) ac.getBean("helloDao");
IHelloDao hd = ac.getBean("helloDao",IHelloDao.class);
System.out.println(hd.sayHi("阿伟"));
}
}