[Spring 01] IOC
Spring_01 IOC
思路: 不用new对象-> IOC思想,Spring托管一切 -> 用xml配置 -> DI 给xml配置下给对象赋值 -> 复杂类型自动注入 -> 干掉xml文件,用注解开发
interface21官网
spring官网:https://spring.io/
下载:
-
projects--overview--spring framework--learn,reference doc, 下载网址: 进Html, Distribution Zip files, 进网站选择版本
-
spring.io,project, 点击 github图标,去github下载
-
web支持,搜 spring maven,找到依赖包 dependence: spring-webmvc,(同时导入spring-jdbc依赖包)
优点:1开源免费,2轻量级非入侵,3控制反转(IOC,面向切面编程(AOP, 4支持事务处理,框架整合的支持
Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架(容器)
spring boot快速构建单个微服务(需要基础:spring, springMVC),spring cloud协调一切
1. IOC
1. 组成
2. IOC理论推导
MVC理论:
Userdao接口
Userdao类1 interface 接口
UserService接口
UserService 类
private Userdao userdao = new Userdao类1;
public void method(){调用userdao的方法}
main
UserService userService;
userService.method();
//当添加一个需求userdao类2时,需要改变UserService类中的代码
//改变:设计模式的改变, IOC的原型
UserService 类
private Userdao userdao;
public setUserdao(Userdao userdao){this.userdao = userdao;}//修改
public void method(){调用userdao的方法;}
main变为:
UserService userService;
userService.setUserdao(new Userdao类());//主动权的改变
userService.method();
降低耦合,专注业务(不用为了业务的转换去做适配了)IOC原型
+重点:主动权的改变:主动权从业务层(UserService)改变为用户(main)
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。Spring中用IOC容器是实现,方法是依赖注入(DI)。
3. helloSpring
pojo类:
public class Hello{
private String name;
get/set/
}
resource中配置xml元数据配置bean,id:实例化的类名,class对应类的位置,property是属性赋值
name是属性名,value为八大数据类型的值,ref为引用类的类型名
<?xml version="1.0" encoding="UTF-8"?>
测试类
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello first = (Hello) context.getBean("first");
System.out.println(first.toString());
重要理解:控制反转, 依赖注入
控制: 谁来控制对象的创建,以前是程序本身控制,现在是Spring控制
反转: 程序本身不创建对象,而变成被动的接受
依赖注入: 利用set方法进行注入
注入方法有: set方法 和 构造参数
4. IOC创建对象的方式
默认在加载配置文件的时候调用无参构造获取对象
有参构造获取对象的三种方法:
-
index :(不推荐)
-
type:(不推荐)
-
name 推荐:当属性是对象时,用ref,指向其他的bean
5. Spring的配置
1. alias
2. bean
id: 对象名
class:类型(包名+类名)
name: 也可以同时取多个别名: name="U1,U2"用逗号、分号或者空格分隔
3. import
6. 依赖注入
1. 构造器注入(必须有有参)
2. Setter注入(必须有无参构造器)
依赖:bean的创建依赖于容器
注入:bean的所有属性,由容器来注入
环境搭建:
pojo类Address:
public class Address {
private String address;
getter/setter;
toString;
}
pojo类Student:
public class Student {
private String name;
private Address address;
private String[] books;
private List hobbies;
private Map card;
private Set games;
private String wife;
private Properties info;
get/set;
toString;
}
beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
...
测试类:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
对应复杂类型注入方法:
Array:
private String[] books;
book1
book2
book3
List:
private List hobbies;
hobby1
hobby2
hobby3
Map:
private Map card;
Set:
private Set games;
gta5
gta6
Null:
private String wife;
Properties:
private Properties info;
123123
boy
3. P-namespace
对应setter注入,简化setter注入
xmlns:p="http://www.springframework.org/schema/p"
4. C-namespace
对应构造器注入,简化constructor-args方式
x 1
xmlns:c="http://www.springframework.org/schema/c"
5. Bean的作用域
在bean标签中添加scope属性, 值为singleton或prototype
作用域还有: request, session, application, 在web开发中使用