Spring(狂神说笔记)
初始spring
1.简介
- Spring是一个开源的免费的框架(容器)
- Spring是一个轻量级的,非入侵的框架
- 控制反转(IOC),面向切面编程(AOP)
- 支持事务处理,对框架整合的支持
Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
2.组成
3.扩展
- SpringBoot
- 一个快速开发的脚手架
- 基于Springboot可以快速开发单个微服务
- 约定大于配置
- SpringCloud
- SpringCloud是基于SpringBoot实现的
IOC的理解
可以参考前面的springboot第七节课笔记对IOC的理解
下面简单看一下spring中对IOC代码实现
1.简单例子
pojo类的编写
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hello {
private String str;
}
bean的注册
id相当于变量名,class是new的对象,property代表属性,name是具体的属性,value是属性的值,ref是已经创建好的bean的id
<?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="hello" class="com.hznu.ch.pojo.Hello">
<property name="str" value="hello world"/>
</bean>
</beans>
测试
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Hello bean = (Hello) ctx.getBean("hello");
System.out.println(bean);
}
}
以上这个过程就叫控制反转
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建,使用spring后,对象是由spring来创建的
反转:程序本身不创建对象,而变成被动的接收对象
IOC更是一种编程思想,由主动的编程变成被动的接收
为了更加深刻的了解IOC,我们接下来来一个更加复杂的例子
2.复杂例子
dao层
public interface UserDao {
void test();
}
public class UserDaoMysqlImpl implements UserDao{
public void test() {
System.out.println("mysql");
}
}
public class UserDaoOracleImpl implements UserDao {
public void test() {
System.out.println("oracle");
}
}
service层
public interface UserService {
void testService();
}
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void testService() {
userDao.test();
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
配置文件
<?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="userDaoMysql" class="com.hznu.ch.dao.UserDaoMysqlImpl"/>
<bean id="userDaoOracle" class="com.hznu.ch.dao.UserDaoOracleImpl"/>
<bean id="userServiceImpl" class="com.hznu.ch.service.UserServiceImpl">
<property name="userDao" ref="userDaoOracle"/>
</bean>
</beans>
测试
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService userServiceImpl = (UserService) ctx.getBean("userServiceImpl");
userServiceImpl.testService();
}
}
我们主要看一下配置文件,我们发现我们如果想把数据库改成mysql只需要把ref的值改成userDaoMysql
即可,不在像以前一样去修改代码里的东西了
到了现在,我们彻底不用在程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IOC,一句话搞定:对象由spring创建,管理,装配!
3.IOC创建对象的方式
1.构造器注入
- 使用无参构造创建
- 通过调用有参构造函数
<bean id="hello" class="com.hznu.ch.pojo.Hello">
<constructor-arg index="0" value="chenheng"/>
<constructor-arg index="1" value="123"/>
</bean>
- 通过调用有参构造函数(另一种方法)
<bean id="hello" class="com.hznu.ch.pojo.Hello">
<constructor-arg type="java.lang.String" value="chendan"/>
<constructor-arg type="int" value="321"/>
</bean>
2.set注入
- 这种方法是通过无参构造初始化的,再通过set来完成参数的注入的
<bean id="hello" class="com.hznu.ch.pojo.Hello">
<property name="str" value="ch"/>
<property name="age" value="116"/>
</bean>
小结:在配置文件加载的时候,容器中管理的对象就已经被初始化了
- 扩展IOC的使用
public class Address {
}
@Data
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> card;
private Set<String> games;
private String wife;
private Properties info;
}
<bean id="student" class="com.hznu.ch.pojo.Student">
<!-- 字符串类型-->
<property name="name" value="ch"/>
<!-- 引用类型-->
<property name="address" ref="address"/>
<!-- 数组类型-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<!-- list类型-->
<property name="hobbies">
<list>
<value>打代码</value>
<value>吃零食</value>
<value>玩三国杀</value>
</list>
</property>
<!-- map类型-->
<property name="card">
<map>
<entry key="46" value="dd"/>
<entry key="30" value="cd"/>
</map>
</property>
<!-- set类型-->
<property name="games">
<set>
<value>三国杀</value>
</set>
</property>
<!-- null类型-->
<property name="wife">
<null/>
</property>
<!-- properties-->
<property name="info">
<props>
<prop key="性格">很傻很天真</prop>
</props>
</property>
</bean>
3.其他注入
命名空间的注入,需添加对应的约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
- p命名空间注入(通过set注入)
<!--两则等价-->
<bean id="teacher" class="com.hznu.ch.pojo.Teacher">
<property name="name" value="ch"/>
<property name="age" value="21"/>
</bean>
<bean id="teacher" class="com.hznu.ch.pojo.Teacher" p:age=