第1章 Spring简介
1、Spring定义
Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
2、Spring特性
IOC(控制反转,DI:依赖注入)
AOP(面向切面编程)
3、Spring优点
-
Spring是一个开源的免费的框架(容器)
-
Spring是一个轻量级、非入侵式的框架
-
控制反转(IOC),面向切面编程(AOP)
-
支持事务的处理,对框架整合的支持
总结:Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架。
4、Spring理念
面向Bean编程
5、Spring组成部分
第2章 Spring IOC(控制反转)
2.1、IOC是什么
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方式。
所谓控制反转就是:获得依赖对象的方式反转了。
所谓IoC一句话搞定:对象由Spring来创建,管理,装配。
2.2、原本的方法
-
UserDao接口
-
UserDaoImpl实现类
-
UserService业务接口
-
UserServiceImpl业务实现类
在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原代码!如果程序代码量十分大,修改一次的成本代价十分昂贵!
我们使用一个Set接口实现。已经发生了革命性的变化!
private UserMapper userMapper;
//利用set进行动态实现值的注入!
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
-
之前程序是主动创建对象,控制权在程序员手上!
-
使用了set注入后,程序不在具有主动性,而是变成了被动的接收对象!
这种思想,从本质上解决了问题,我们程序员不用再去管理对象的创建了。系统的耦合性大大降低,可以更加专注在业务的实现上。这是IOC的原型!
2.3、IOC的本质
IOC是Spring框架的核心内容,使用多种方式完美的实现了IOC
-
可以使用xml配置,
-
也可以使用注解。
Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从IOC容器中取出需要的对象。
控制反转是一种描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IOC容器,其实现方法是依赖注入DI。
2.4、创建IOC
<?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">
<!--使用Spring来创建对象,在Spring这些都称为Bean-->
<bean id="person" class="cn.test.entity.Person">
<property name="personName" value="zs"/>
</bean>
</beans>
public class TestSpring {
@Test
public void test(){
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以了
Person person = context.getBean("person", Person.class);
System.out.println(person.getPersonName());
}
}
2.5、思考问题?
-
Person对象是谁创建的?
Person对象是由Spring创建的
-
Person对象的属性是怎么设置的?
属性是由Spring容器设置的
这个过程就叫控制反转:
-
控制:谁来控制对象的创建。传统应用的对象是由程序本身控制创建的,而使用Spring后,对象是由Spring创建的。
-
反转:程序本身不创建对象,而是变成被动接收对象。
-
依赖注入:就是利用set方法来进行注入
IOC是一种编程思想,由主动的编程变成被动的接收。
第3章 Spring DI(依赖注入)
1、Set注入(属性注入)
1.1、手动注入(ref引入)
<bean id="person" class="cn.test.entity.Person">
<property name="personName" value="用户名称"></property>
<property name="role" ref="role"></property>
</bean>
<bean id="role" class="cn.test.entity.Role">
<property name="roleName" value="角色名"></property>
</bean>
1.2、自动注入(autowire)
1.21、ByName自动装配
<!--
byName:会自动在容器上下文中,查找和自己对象set方法对应的bean id
-->
<bean id="person" class="cn.test.entity.Person" autowire="byName">
<property name="personName" value="用户名称"></property>
</bean>
<bean id="role" class="cn.test.entity.Role">
<property name="roleName" value="角色名"></property>
</bean>
public class Person {
private Role role;
//即setRole中的Role和bean id="role"的role要对应
public void setRole(Role role) {
this.role = role;
}
}
1.22、ByType自动装配
<!--
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
只要是同源关系,这样的bean都能赋值给引用类型,同源即同一个个类型
1.java数据类型和bean的class值是一样的
2.java数据类型和bean的class值是父子类
3.java数据类型和bean的class值是接口和实现类
-->
<bean id="person" class="cn.test.entity.Person" autowire="byType">
<property name="personName" value="用户名称"></property>
</bean>
<bean id="role" class="cn.test.entity.Role">
<property name="roleName" value="角色名"></property>
</bean>
public class Person {
//即private Role role的Role要和class="cn.test.entity.Role"的Role对应
private Role role;
public void setRole(Role role) {
this.role = role;
}
}
1.23、总结
-
使用byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法一致。
-
使用byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
2、注解注入@(创建对象)
-
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。
-
@Component是所有受Spring管理组件的通用形式;
-
而@Repository、@Service和 @Controller则是@Component的细化,用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。
-
也就是说,你能用@Component来注解你的组件类,但如果用@Repository、@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。
-
@Component和@Repository,@Service,@Controller的异同 同:都可以创建对象 异:@Repository,@Service,@Controller都有自己的分层角色.
2.1、加入组件扫描器
在使用注解@时,需要在applicationContext.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"
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">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.test.entity"/>
<context:component-scan base-package="cn.test.dao"/>
<context:component-scan base-package="cn.test.service "/>
<context:component-scan base-package="cn.test.controller"/>
</beans>
2.2、@Component
(1)定义
创建对象,等同于<bean></bean>功能
<bean id="user" class="cn.kgc.entity.User"></bean>
@Component("user")
(2)加入注解
import org.springframework.stereotype.Component;
@Component("user")
public class User {
private Integer id;
private String name;
}
2.3、@Repository
(1)定义
用在持久层,dao的实现类上面,表示创建dao对象
@Repository("userMapper")
(2)UserMapper接口
public interface UserMapper {
public Integer addUser();
}
(3)UserMapperImpl实现类
import org.springframework.stereotype.Repository;
@Repository("userMapper")
public class UserMapperImpl implements UserMapper {
@Override
public Integer addUser() {
System.out.println("调用UserMapper addUser方法...");
return null;
}
}
2.4、@Service
(1)定义
用在业务层,service的实现类上面,表示创建service对象,可以有一些事务功能。
@Service("userService")
2.5、@Controller
(1)定义
用在控制器Controller类上,创建控制器对象,能够接收用户提交的参数,显示请求的处理结果。
@Controller("userController")
3、注解注入@
3.1、@Value
(1)定义
给简单类型属性对象赋值
@Component("user")
public class User {
@Value("1")
private Integer id;
@Value("zs")
private String name;
}
3.2、@Autowired
(1)自动装配ByType
给引用类型属性赋值(自动装配Bean),默认使用byType
@Component
public class User {
@Value("1")
private Integer id;
@Value("zs")
private String name;
@Autowired
private Role role;
}
@Component
public class Role {
@Value("1")
private Integer roleId;
@Value("经理")
private String roleName;
}
3.3、@Resource
(1)自动装配byName
给引用类型属性赋值(自动装配Bean),默认使用byName
@Component
public class User {
@Value("1")
private Integer id;
@Value("zs")
private String name;
@Resource(name = "role")
private Role role;
}
@Component
public class Role {
@Value("1")
private Integer roleId;
@Value("经理")
private String roleName;
}
@Resource是JDK的注解,Spring提供了对JDK注解@Resource的支持
默认按名称注入,如果按名称注入失败,自动按类型注入