IOC思想介绍
xml方式配置bean
注解方式配置bean
refresh方法解析
bean实例化解析
一、IOC思想
举例说明
假设有一个animal类有两个实现类Dog和Cat
Animal animal = new Dog();
Animal animal = new Cat();
使用IOC。 不需要自己实例对象
@Autowired
Animal animal、
IOC优点
1、松耦合
2、灵活性提高
3、可维护性提高
二、bean的配置方式
1、xml
2、注解
1、xml方式配置方式
优点: 低耦合,对象关系清晰,集中管理
缺点: 配置繁琐,开发效率稍低,文件解析耗时
1) 无参构造
2)有参构造
3) 静态工厂方法
4) 实例工厂方法
1无参构造
1.1)创建Student类
public class Student { private String name; private Integer age; private List<String> classList; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<String> getClassList() { return classList; } public void setClassList(List<String> classList) { this.classList = classList; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", classList=" + String.join(",", classList) + '}'; } }
1.2) 创建HelloService类
public class HelloService { private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String hello(){ return student.toString(); } }
1.3) resources/ioc文件夹下配置demo.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.xsd"> <bean id="student" class="com.example.demo.ioc.xml.Student"> <property name="name" value="zhangsan" /> <property name="age" value="13" /> <property name="classList"> <list> <value>math</value> <value>english</value> </list> </property> </bean> <bean id="helloServie" class="com.example.demo.ioc.xml.HelloService"> <property name="student" ref="student" /> </bean> </beans>
4) 测试
@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(locations = "classpath:ioc/demo.xml") public class BeansTest { @Autowired private HelloService helloService; @Test public void contextLoads() { } @Test public void testHello() { System.out.println(helloService.hello()); } }
5) 输出结果
Student{name='zhangsan', age=13, classList=math,english}
2有参构造
1)Student类增加构造函数
public class Student { private String name; private Integer age; private List<String> classList; public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<String> getClassList() { return classList; } public void setClassList(List<String> classList) { this.classList = classList; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", classList=" + String.join(",", classList) + '}'; } }
2)demo.xml修改如下
3) 输出结果
Student{name='zhangsan', age=13, classList=math,english}
3 静态工厂方法
1)、创建抽象类Animal
public abstract class Animal { abstract String getName(); }
2)、创建两个实现类Cat和Dog
public class Cat extends Animal { @Override String getName() { return "Cat"; } }
public class Dog extends Animal { @Override String getName() { return "Dog"; } }
3)创建工厂类
public class AnimalFactory { public static Animal getAnimal(String type){ if("dog".equals(type)){ return new Dog(); }else { return new Cat(); } } }
4)在HelloService类中增加引用
5) 配置demo.xml
6)测试
输出结果:
Dog
4、 实例工厂方法
1)AnimalFactory改造如下
2)xml配置改造如下
3、测试
Cat
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!