Spring框架
1.Spring定义
轻量级框架, Java EE的春天,当前主流框架
Spring是一个开源的轻量级Java开发框架,是一种简化应用程序的开发
在spring出来之前,service层调用dao层都是用new的方式。
在spring出来之后,service层和dao层都会放在spring容器去管理,这是spring的第一种特性,我们称之为IOC,控制反转。
spring还有一种特性,我们称之为AOP,大白话,“面向切面”,说白了就是专门的人干专门的事。在项目很多公有的或是
要被重复被调用的模块可以被抽取出来,利用的就是AOP的特性,例如日志模块。
2.Spring特性
1.IOC(控制反转,DI:依赖注入)
2.AOP实现 (面向切面编程)
3.Spring的核心思想
在Spring中万物都是Bean组件(不用new对象)
4.Spring理念
面向Bean的编程
5.Spring优势
低侵入式设计
独立于各种应用服务器
依赖注入特性将组件关系透明化,降低了耦合度
面向切面编程特性允许将通用任务进行集中式处理
与第三方框架的良好整合
6. DI注入分为:
1.set注入(属性注入)
2.注解注入@
3.构造器注入
4.静态工厂注入,动态工厂注入,方法参数注入等(不常用)
6.1 Set注入(对象、数值、引用类型,属性值等)
<?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="user" class="cn.kgc.entity.User">
<property name="id" value="11"/> ---->相当于启用命令调用setId()方法
<property name="name" value="zs"/> ---->相当于启用命令调用setName()方法
</bean>
</beans>
6.2引用类型手工注入(ref方式)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User">
<property name="id" value="11"/>
<property name="name" value="zs"/>
<property name="role" ref="myRole"/>//手动注入则需要使用ref指向下面的myRole
</bean>
<bean id="myRole" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
6.3引用类型自动注入
如果是属性注入,需要为每一个依赖类创建相应的getter和setter方法。
如果是构造方法注入,需要为依赖类创建相应的构造方法。
6.3.1 byName
byName(按名称注入):
java类中引用类型的属性名和spring容器中(配置文件)<bean>的id名称一样,且
数据类型是一致的,这样的容器中的bean,spring能够赋值给引用类型
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User" autowire="byName">
<property name="id" value="11"/>
<property name="name" value="zs"/>
</bean>
<bean id="role" class="cn.kgc.entity.Role">//id与实体类中的role属性相同
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
6.3.2 byType
byType(按类型注入):java类中引用类型的数据类型和spring容器中(配置文件)<bean>的class属性
是同源关系的,这样的bean能够赋值给引用类型
同源就是一类型的意思:
例如:第一种同源.java引用类型的数据类型和bean的class的值是一样的
第二种同源.java引用类型的数据类型和bean的class的值是父子类间的关系
第三种同源.java引用类型的数据类型和bean的class的值是接口和实现类的关系
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User" autowire="byType">
<property name="id" value="33333"/>
<property name="name" value="555555"/>
</bean>
<bean id="role3" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
6.3.2 byName 和 byType的区别
byName:根据属性名自动装配。将检查容器并根据名字查找与属性完全一致的bean
byType:根据属性类型自动装配。将检查容器并根据类型查找与属性完全一致的bean