一、依赖注入(DI)
依赖注入听起来很高深的样子,其实白话就是:给属性赋值。一共有两种方法,第一是以构造器参数的形式,另外一种就是以setting方法的形式。
1 构造器注入
1 使用构造器注入
使用xml的注入方式
A. 通过参数的顺序
B. 通过参数的类型
具体实例
假如现在要对一个Person类注入参数,Student是一个另外一个类。
public class Person {
private String pid;
private String name;
private Student student;
public Person(String pid, Student student){
this.pid= pid;
this.student = student;
}
public Person(String pid, String name){
this.pid = pid;
this.name = name;
}
}
配置applicationContext.xml,假如不进行参数配置,则报错,找不到相应的构造器。配置了相应的参数,则应在类中声明相应的构造函数。
编写测试类DIXMLConstructorTest ,进行断点调试,将会发现根据配置的参数,进入的构造函数是Person(String pid, Student student)
public class DIXMLConstructorTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
}
}
2 使用属性setter方法进行注入
使用xml的注入方式:
A. 简单Bean的注入
简单Bean包括两种类型:包装类型和String
B. 引用其他Bean
1.1 装配list集合
1.2 装配set集合
list1 list2
1.3 装配map
map中的以及
1.4 装配Properties
prop1 prop2
具体实例
1.创建两个对象Person和Student
package xgp.spring.demo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Person {
private String pid;
private String name;
private Student student;
private List lists;
private Set sets;
private Map map;
private Properties properties;
private Object[] objects;
public Person(){
System.out.println("new person");
}
//省略getter和setter方法
}
package xgp.spring.demo;
public class Student {
public Student(){
System.out.println("new student");
}
public void say(){
System.out.println("student");
}
}
配置applicationContext.xml文件
set1 set2 prop1 prop2
aa bb
编写测试类DIXMLSetterTest
package xgp.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xgp.spring.demo.Person;
public class DIXMLSetterTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPid());
System.out.println(person.getName());
System.out.println(person.getLists());
System.out.println(person.getSets());
System.out.println(person.getMap());
System.out.println(person.getObjects().length);
}
}
spring容器的执行顺序
1.都是默认设置
2.设置student(lazy-init=true)
3.设置person(lazy-init=true)
总结
可以采用两种方法注入参数,构造器要写对应的构造函数,setter要生成相应的setter方法,并编写默认的构造器。
2.5 IOC与DI的意义
学了这些,发现有什么意义?下面写个文档管理系统例子来说明,需求见下图
1.编写document 接口
public interface document {
public void read();
public void write();
}
2、编写实现类Worddocument ,Exceldocument ,PDFdocument
public class Worddocument implements document{
public void read() {
System.out.println("word read");
}
public void write() {
System.out.println("word write");
}
}
3、编写文档管理 系统 documentManager
public class documentManager {
private document document;
public void setdocument(document document) {
this.document = document;
}
public documentManager(){
}
public documentManager(document document) {
super();
this.document = document;
}
public void read(){
this.document.read();
}
public void write(){
this.document.write();
}
}
4、编写测试类documentTest
public class documentTest {
@Test
public void testdocument_NOSPRING(){
document document = new Worddocument();
documentManager documentManager = new documentManager(document);
documentManager.read();
documentManager.write();
}
@Test
public void testdocument_Spring(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
documentManager documentManager =(documentManager)context.getBean("documentManager");
documentManager.read();
documentManager.write();
}
}
从上面可以看出不适用spring和适用spring的区别
使用spring只需要在applicationContext中配置相应的对象,而不需要关注具体的实现类,实现完全的面向接口编程,这也是为什么spring能够和这么多工具集成的原因。
2.6 mvc实例–模拟structs2
需求描述
建立工程目录
编码:
1、创建Dao层
建立PersonDao接口和实现类PersonDaoImpl
public interface PersonDao {
public void savePerson();
}
public class PersonDaoImpl implements PersonDao {
@Override
public void savePerson() {
System.out.println(" save person");
}
}
2、建立service层,PersonService接口与PersonServiceImpl实现类
public interface PersonService {
public void savePerson();
}
public class PersonServiceImpl implements PersonService{
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson() {
this.personDao.savePerson();
}
}
3、建立Action,PersonAction类
public class PersonAction {
private PersonService personService;
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public void savePerson(){
this.personService.savePerson();
}
}
4、配置applicationContext.xml
5、编写测试类testMVC
public class MVCTest {
@Test
public void testMVC(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonAction personAction = (PersonAction)context.getBean("personAction");
personAction.savePerson();//save person
}
}
上述实例很清楚的展现出了spring的面向接口编程,service层只需调用dao层的接口,而不需要关注于dao层的实现类,action也只需调用service的接口,而不需要关注service的实现类。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



