首先,感谢一下@狂神老师的Spring课程https://www.bilibili.com/video/BV1WE411d7Dv?spm_id_from=333.999.0.0
其次,感谢一下@黑心白莲同学的笔记https://blog.csdn.net/li643937579/article/details/109569887
前言:这一阶段的学习,主要注重思想上的学习,重点关注的是IOC和AOP,以及静态代理或者动态代理设计模式的学习
Springboot和Spring Cloud
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
个人认为控制反转就是:获得依赖对象的方式变了
采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。
对应程序是 Spring-02-hello
1. 新建一个Maven项目,编写实体类
package com.qin.dao;
public class Hello {
private String str;
public String getStr() {
return str;
}
// spring 本质上是通过set方法进行注入的
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
2. 编写XML配置文件(配置文件的编写,可以参考官方的文档)
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.qin.dao.Hello">
<property name="str" value="Spring!!!"/>
bean>
beans>
3. 测试
import com.qin.dao.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
// 第一步CPX 加载XML的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 第二步 getbean 获取bean的对象
Hello hello = (Hello) context.getBean("hello");
// 打印输出结果
System.out.println(hello.toString());
}
}
思考问题:
这个过程就叫做控制反转
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的。
反转:程序本身不创建对象,而变成被动的接收对象。
依赖注入:就是利用set方法来进行注入的。
OK,到了现在,我们彻底不用在程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IOC,一句话搞定:对象由Spring来创建,管理,装配!
总结:所谓的IOC是,对象由Spring来创建,管理和装配!
对应的程序Spring-02-IOC
1. User类
package com.qin.dao;
public class User {
private String name;
public User(){
System.out.println("User的无参构造函数");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
2、UserT类
package com.qin.dao;
public class UserT {
private String name;
public UserT(String name){
this.name = name;
System.out.println("UserT的有参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
3.XML配置文件
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.qin.dao.User">
<property name="name" value="qinqs"/>
bean>
<bean id="userT" class="com.qin.dao.UserT">
<constructor-arg name="name" value="qinqsTTT"/>
bean>
4. 测试类
import com.qin.dao.User;
import com.qin.dao.UserT;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user", User.class);
user.show();
// UserT userT = (UserT) context.getBean("userT");
// userT.show();
}
}
4.2 使用有参构造创建对象的时候
<alias name="userT" alias="userNew"/>
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
bean>
<import resource="bean.xml"/>
<import resource="bean2.xml"/>
<import resource="bean3.xml"/>
对应的练习程序为Spring-04-ID
<bean id="userT" class="com.qin.dao.UserT">
<constructor-arg name="name" value="qinqsTTT"/>
bean>
要求被注入的属性,必须有set方法
测试的pojo类(注意,要注入的熟悉中,都有需要的set方法)
Address.class
package com.qin.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
Student.class
package com.qin.pojo;
import java.util.*;
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;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
Mytest.class 测试类
import com.qin.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
xml 配置文件
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.qin.pojo.Address">
<property name="address" value="兰州"/>
bean>
<bean id="student" class="com.qin.pojo.Student">
<property name="name" value="秦"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>红楼梦value>
<value>西游记value>
<value>三国演义value>
<value>水浒传value>
array>
property>
<property name="hobbies">
<list>
<value>打篮球value>
<value>踢足球value>
<value>敲代码value>
list>
property>
<property name="card">
<map>
<entry key="身份证" value="1234">entry>
<entry key="银行卡" value="789456">entry>
map>
property>
<property name="games">
<set>
<value>LOLOvalue>
<value>CCOvalue>
set>
property>
<property name="wife">
<null>null>
property>
<property name="info">
<props>
<prop key="urel">123prop>
<prop key="pass">hhhhhprop>
props>
property>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User" p:name="qin" p:age="20"/>
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22"/>
beans>
singleton单例模式(Spring默认的机制)
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="singleton"/>
单例模型,每次产生的对象都是一样的
prototyper原型模型
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>
每次从容器中getbean的时候,都会产生一个新的对象
对应的练习程序为Spring-05-auto
在Spring中有三种装配的方式:
1. 在XML中显示的配置
1. 在java中显示的配置
1. 饮食的自动装配bean【重要】
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.qin.pojo.Cat">bean>
<bean id="dog" class="com.qin.pojo.Dog">bean>
<bean id="user" class="com.qin.pojo.User" >
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
<property name="name" value="qin"/>
bean>
beans>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.qin.pojo.Cat">bean>
<bean id="dog" class="com.qin.pojo.Dog">bean>
<bean id="user" class="com.qin.pojo.User" autowire="byName">
<property name="name" value="qin"/>
bean>
beans>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.qin.pojo.Cat">bean>
<bean id="dog" class="com.qin.pojo.Dog">bean>
<bean id="user" class="com.qin.pojo.User" autowire="byType">
<property name="name" value="qin"/>
bean>
beans>
小结:
jdk1.5支持的注解,Spring2.5就支持注解了!
使用注解开发的时候,需要导入约束的头文件,并开启注解的支持!需要context的支持
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
beans>
在大部分的情况下,我们都会选用注解开发,尤其是在后期的SpringBoot中
Autowired的注解
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.qin.pojo.Cat">bean>
<bean id="dog" class="com.qin.pojo.Dog">bean>
<bean id="user" class="com.qin.pojo.User">bean>
beans>
package com.qin.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class User {
// Autowired的注解,在属性上可以使用,在set方法上也可以直接使用
// 使用Autowired的注解,本质上利用的是反射的实现,连set方法也可以省略不要
// 使用Autowired的注解,XML配置可以变的很干净,只需要注册bean,不需要装配属性
@Autowired // Autowired的注解,在属性上
private Cat cat;
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "com.qin.pojo.User{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
Qualifier注解(不能单独使用,必须配合autowire的注解使用)
如果@Autowired自动装配的时候环境比较复杂,自动装配无法通过一个注解【@Autowired】来完成的时候【本质上是通过byType来完成的注解】,我们可以通过【@Qualifier(value=“xxx”)】来配合@Autowired的使用
本质:@Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配;@Qualifier不能单独使用。
xml中的内容
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.qin.pojo.Cat">bean>
<bean id="dog" class="com.qin.pojo.Dog">bean>
<bean id="dog1" class="com.qin.pojo.Dog">bean>
<bean id="dog2" class="com.qin.pojo.Dog">bean>
<bean id="user" class="com.qin.pojo.User">bean>
beans>
User.calss
package com.qin.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class User {
// Autowired的注解,在属性上可以使用,在set方法上也可以直接使用
// 使用Autowired的注解,本质上利用的是反射的实现,连set方法也可以省略不要
// 使用Autowired的注解,XML配置可以变的很干净,只需要注册bean,不需要装配属性
@Autowired // Autowired的注解,在属性上
private Cat cat;
@Autowired
@Qualifier(value = "dog1") // Qualifier注解配合Autowired注解的使用,来增强bean的自动装配
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "com.qin.pojo.User{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
Resource注解
Resource注解的使用,也是直接放在属性的字段上面
@Resource
private Cat cat;
@Resource
private Dog dog;
private String name;
Autowired和Resource的区别
对应的练习程序为:Spring-06-anno
使用注解开发,必须导入AOP包,同时使用注解的时候,要在XML中进行配置,支持注解的开发!
注解可以指定要扫描的包,使用注解的开发,必须要扫描指定的包
<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
https://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="com.qin.pojo"/>
<context:annotation-config/>
beans>
bean(@Component注解来注册bean)
package com.qin.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 这个注解就等价于
// @Component 代表的是组件
@Component // 一般情况下放在类上,说明这个类被Spring管理了
public class User {
@Value("哈哈") // 这个等价于属性的注入
public String name;
}
属性如何注入
// 这个注解就等价于
// @Component 代表的是组件
@Component
public class User {
@Value("哈哈") // 这个等价于属性的注入
public String name;
}
衍生的注解
@Component的有几个衍生的注解,我们在web的开发中,会按照MVC三成架构来进行分层
dao层【@Repository】
service层【@Service】
web层【@Controller】
这四个注解的功能都是一样的,都是将某个类注册到Spring中的容器中,进行装配
自动装配
作用域
XML和注解
对应的练习程序为:Spring-07-anno2
本质上,使用的还是注解
现在我们完全不使用XML进行配置了,全权交给Java来进行配置
本质就是:用一个配置类来代替原来的XML来实现bean的注册的装配
User.class
package com.qin.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 注解的意思是,说明这个类完全呗Spring接管了,注册到容器中了
@Component
public class User {
@Value("qin") // 给属性注入值
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
Myconfig.class
package com.qin.config;
import com.qin.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 现在我们完全不需要Spring的xml配置信息了,全权交给Java来做
// @Configuration 代表的这是一个配置类,和原来的beans.xml道理一样
@Configuration
public class Myconfig {
@Bean // 注册一个bean 方法的名字就相当于bean标签中的id属性
public User user(){
return new User();
// 返回值相当于bean标签中的class属性
}
// 总结:这种用java来接管的配置,在SpringBoot中随处可见
}
Mytest.class测试类
import com.qin.config.Myconfig;
import com.qin.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args) {
// 如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,
// 通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(Myconfig.class);
User user = (User) context.getBean("user");
System.out.println(user.getName());
}
}
总结:这种用java来接管的配置,在SpringBoot中随处可见!!!Spring完全可以摒弃XML的配置!!!
AOP的实现本质上利用的是代理模式(静态代理和动态代理)----代理的是接口
对应的程序为Spring-09-aop
再不改变原有的业务的情况下,添加新的功能(比如添加日志)!
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现 程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的 一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使 得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
通俗的来讲:AOP就是在不改变原有逻辑的情况下,添加新的功能,如:日志、安全、缓存、事务等等!!!
横切关注点:通俗的讲就是,在不改变原有逻辑的情况下,执行添加新功能的点!
切面:自定义的一个类,后面还需要关注点和通知
通知:切面必须要完成的工作,通俗的讲就是类中的一个方法
Spring支持五种通知:
1、前置通知 2、后置通知 3、环绕通知 4、异常抛出通知 5、引介入通知
总结:AOP就是在不改变原有代码的情况下,去增加新的功能
方式一:使用Spring的API接口实现
首先导入Maven的依赖
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
然后再servlet的包下,定义UserService业务接口和UserServiceImpl实现类
package com.qin.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
package com.qin.service;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户!");
}
@Override
public void delete() {
System.out.println("删除了一个用户!");
}
@Override
public void update() {
System.out.println("更新了一个用户!");
}
@Override
public void select() {
System.out.println("查询了一个用户!");
}
}
在log包下,定义我们的增强类,一个前置通知和一个后置通知
package com.qin.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
@Override
// method :表示要执行的目标对象的方法
// agrs :参数
// target :目标对象
public void before(Method method, Object[] agrs, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行");
}
}
package com.qin.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
配置benas.xml(重点)(注意需要导入相应的约束条件)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.qin.service.UserServiceImpl">bean>
<bean id="log" class="com.qin.log.Log"/>
<bean id="afterlog" class="com.qin.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.qin.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
aop:config>
beans>
测试
import com.qin.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 此处唯一一点的不同是:到那个太代理代理的是接口:这个是注意点
UserService userService = (UserService) context.getBean("userService");
userService.delete();
}
// AOP 就是在不影响原来业务的情况下,实现动态增强
}
方式二:自定义的类来实现AOP【主要是切面定义】
创建DIYPointCut.class
package com.qin.diy;
public class DiyPointCut {
public void before(){
System.out.println("=======方法执行前========");
}
public void after(){
System.out.println("=======方法执行后========");
}
}
注册bean.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.qin.service.UserServiceImpl">bean>
<bean id="diy" class="com.qin.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.qin.service.UserServiceImpl.*(..))"/>
<aop:after method="after" pointcut-ref="point"/>
<aop:before method="before" pointcut-ref="point"/>
aop:aspect>
aop:config>
beans>
测试
方式三:注解实现AOP【注解实现还是有一定的约束和限制】
package com.qin.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标注这是一个切面
public class AnnotationPointCut {
// 前置通知 注解的内容主要是写切入点
@Before("execution(* com.qin.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法执行前=======");
}
// 后置通知
@After("execution(* com.qin.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====方法执行后=======");
}
// 环绕通知
@Around("execution(* com.qin.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Object proceed = jp.proceed();
System.out.println("环绕后");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.qin.service.UserServiceImpl">bean>
<bean id="annotationPointCut" class="com.qin.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
AOP主要学习的是一种横向编程的思想,即在不改变原有代码的情况下,实现业务的增强(本质上是使用的动态代理模式)!!!
步骤:
导入相应的jar包
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Spring-studyartifactId>
<groupId>com.qingroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>Spring-10-MybatisartifactId>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.49version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.10version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.2version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
project>
Mybatis的步骤
配合联系的代码程序:Spring-10-Mybatis
整合方式一
首先需要导入需要的maven包 mybatis-spring
在Spring-dao.xml中:使用Spring的数据源替换Mybatis的数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://ip地址:3306/test_1?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="test_1"/>
<property name="password" value="LAbrXKKXMsH3bwPn"/>
bean>
配置SqlSessionFactory,关联Mybatis
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/qin/dao/*.xml"/>
bean>
注册sqlSessionTemplate,也就是我们需要的sqlsession
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
bean>
创建一个实现类(这就麻烦了!)(需要给接口加实现类,这是多了一步而已)
package com.qin.dao;
import com.qin.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
// Spring需要set方法的注入
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
// 需要getmapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
注册bean.xml(将自己写的实现类,注册到Spring中)
<bean id="userMapper" class="com.qin.dao.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
bean>
测试
import com.qin.dao.UserMapper;
import com.qin.pojo.User;
import com.qin.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
总结思路:(不需要mybatis中的工具类了!!!)
mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
configuration>
spring-dao.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:/ip地址:3306/test_1?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="test_1"/>
<property name="password" value="LAbrXKKXMsH3bwPn"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/qin/dao/*.xml"/>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
bean>
<bean id="userMapper" class="com.qin.dao.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
bean>
beans>
需要Spring的托管,所以需要实现类UserMapperImple.java
package com.qin.dao;
import com.qin.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
// Spring需要set方法的注入
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
// 需要getmapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
整合方式二
SqlSessionDaoSupport
dao继承Support类 , 直接利用 getSqlSession() 获得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不 需要管理SqlSessionTemplate , 而且对事务的支持更加友好 .
UserMapperImplTwo.java
package com.qin.dao;
import com.qin.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImplTwo extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
Spring-dao.xml的配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://ip地址:3306/test_1?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="test_1"/>
<property name="password" value="LAbrXKKXMsH3bwPn"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/qin/dao/*.xml"/>
bean>
<bean id="userMappertwo" class="com.qin.dao.UserMapperImplTwo">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
beans>
mybatis-config,xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
configuration>
测试类
import com.qin.dao.UserMapper;
import com.qin.pojo.User;
import com.qin.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMappertwo",UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
事务ACID原则:
引入程序Spring-11-transaction
Spring支持编程式事务和声明式事务管理
编程式事务
声明式事务
将事务管理的代码,从业务中分离出来,以声明的方式来管理事务**(其实是死的,谁需要配置事务,直接更改AOP的切入点即可)**
将事务管理作为横切关注点,通过aop方法模块化。Spring中通过Spring AOP框架支持声明式事务 管理
导入相应的约束tx
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://ip地址:3306/test_1?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="test_1"/>
<property name="password" value="LAbrXKKXMsH3bwPn"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/qin/dao/*.xml"/>
bean>
<bean id="userMapperImpl" class="com.qin.dao.UserMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.qin.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
aop:config>
beans>
为什么要配置事务:事务在项目的开发过程中非常的重要,尤其是涉及到数据的一致性的问题,不容马虎!!!