Spring框架
#本文为上课时实例,篇幅较长,请耐心查阅
*什么是spring?
Spring是个开源的轻量级的Java开发框架。 是一种简化应用程序的开发。
在spring出来之前,service层调用dao层都是用new的方式, 在spring出来之后,service层和到dao层都会放在spring容器 去管理,这是spring的第一种特性, 我们称之为IoC,控制反转。
spring还有一种特性, 我们称之为AOP,大白话,所谓“面向切面”,说白了就是专门的人干专门的事。
在项目很多公有的或是要被重复被调用的模块可以被抽取出来,利用的就AOP的特性,例如日志模块。
1.Spring定义
轻量级框架, Java EE的春天,当前主流框架
2.Spring特性
1.IOC (控制反转,DI:依赖注入)
2.AOP实现 (面向切面编程)
3.Spring组成部分
4.Spring理念
面向Bean的编程
*Spring中的核心思想是什么?
在Spring中万物都是Bean组件 (面向Bean编程)
5.Spring优势
低侵入式设计
独立于各种应用服务器
依赖注入特性将组件关系透明化,降低了耦合度
面向切面编程特性允许将通用任务进行集中式处理
与第三方框架的良好整合
6.IOC特性
A.非IOC思想
(1)创建maven工程,java,resources,test,并配置路径参数
(2)UserService
> public interface UserService {
public void print();
}
(3)UserServiceImpl
public class UserServiceImpl implements UserService{
@Override
public void print() {
System.out.println("输出****HelloWorld*********");
}
}
(4)Test001
@Test
public void testPrint(){
UserServiceImpl userService = new UserServiceImpl();
userService.print();
}
B.IOC思想
(1)pom
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
(2)spring配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--申明bean对象-->
<!--id:对象的自定义名称,唯一值,spring通过这个名称找到对象-->
<!--class:类的全路径名称-->
<bean id="userService" class="cn.kgc.service.UserServiceImpl"></bean>
</beans>
(3)Test001
@Test
public void testSpringPrint(){
String conf = "beans.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
UserService userService = (UserService) ac.getBean("userService");
//测试
userService.print();
}
7.DI注入(IOC的实现)
7.1.set注入
7.1.1-属性赋值
A.实体类User
package cn.kgc.entity;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setId(Integer id) {
System.out.println("调用方法setId......");
this.id = id;
}
public void setName(String name) {
System.out.println("调用方法setName......");
this.name = name;
}
}
B.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"
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>
C.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
//测试
System.out.println("编码:"+user.getId()+"名称:"+user.getName());
}
7.1.2引用类型手工注入(ref方式)
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.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">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User">
<property name="id" value="11"/>
<property name="name" value="zs"/>
<property name="role" ref="myRole"/>
</bean>
<bean id="myRole" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
D.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("user名称:"+user.getRole().getrName());
}
7.1.3引用类型自动注入
(1)byName
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
/**
* byName(按名称注入):
* java类中引用类型的属性名和spring容器中(配置文件)<bean>的id名称一样,且
* 数据类型是一致的,这样的容器中的bean,spring能够赋值给引用类型
*/
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.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">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User" autowire="byName">
<property name="id" value="11"/>
<property name="name" value="zs"/>
<!--<property name="role" ref="myRole"/>-->
</bean>
<bean id="role" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
D.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("Role名称:"+user.getRole().getrName());
}
(2)byType
*第一种同源(一致性)
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
/**
*byType(按类型注入):java类中引用类型的数据类型和spring容器中(配置文件)<bean>的class属性
* 是同源关系的,这样的bean能够赋值给引用类型
*
* 同源就是一类型的意思:
* 例如:第一种同源.java引用类型的数据类型和bean的class的值是一样的
* 第二种同源.java引用类型的数据类型和bean的class的值是父子类间的关系
* 第三种同源.java引用类型的数据类型和bean的class的值是接口和实现类的关系
*/
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.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">
<!--声明对象-->
<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>
D.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("Role名称:"+user.getRole().getrName());
}
*第二种同源(父子类)
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
/**
*byType(按类型注入):java类中引用类型的数据类型和spring容器中(配置文件)<bean>的class属性
* 是同源关系的,这样的bean能够赋值给引用类型
*
* 同源就是一类型的意思:
* 例如:1.java引用类型的数据类型和bean的class的值是一样的
* 2.java引用类型的数据类型和bean的class的值是父子类间的关系
* 3.java引用类型的数据类型和bean的class的值是接口和实现类的关系
*/
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.SonRole
package cn.kgc.entity;
public class SonRole extends Role{
}
D.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">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User" autowire="byType">
<property name="id" value="33333"/>
<property name="name" value="555555"/>
</bean>
<bean id="sonRole" class="cn.kgc.entity.SonRole">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
E.Test001
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("Role名称:"+user.getRole().getrName());
}
*第三种同源(接口和实现类)
1.mapper/UserMapper.java
public interface UserMapper {
public void add();
}
2.mapper/UserMapperImpl.java
public class UserMapperImpl implements UserMapper{
@Override
public void add() {
System.out.println("add UserMapper....");
}
}
3.service/UserService.java
public interface UserService {
public void add();
}
** 4.service/UserServiceImpl**
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
@Override
public void add() {
userMapper.add();
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
5.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">
<bean id="userService" class="cn.kgc.service.UserServiceImpl" autowire="byType">
</bean>
<bean id="userMapper" class="cn.kgc.mapper.UserMapperImpl">
</bean>
</beans>
6.TestUserServiceImpl
@Test
public void testprint3(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
*拓展(第三种同源换成byName方式实现)
1.mapper/UserMapper.java
package cn.kgc.mapper;
public interface UserMapper {
public void add();
}
2.mapper/UserMapperImpl.java
package cn.kgc.mapper;
public class UserMapperImpl implements UserMapper{
@Override
public void add() {
System.out.println("add UserMapper....");
}
}
3.service/UserService
public interface UserService {
public void add();
}
4.service/UserServiceImpl
package cn.kgc.service;
import cn.kgc.mapper.UserMapper;
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
@Override
public void add() {
userMapper.add();
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
5.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">
<bean id="userService" class="cn.kgc.service.UserServiceImpl" autowire="byName">
<property name="userMapper" ref="userMapper"/>
</bean>
<bean id="userMapper" class="cn.kgc.mapper.UserMapperImpl">
</bean>
</beans>
6.TestUserServiceImpl
public class TestUserServiceImpl {
@Test
public void testprint3(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
}