Spring5

**Spring5**

***

### 1.Spring框架概述

- Spring是轻量级的开源的JavaEE框架
- Spring可以解决企业应用开发的复杂性
- Spring有两个核心部分:IOC和AOP
- IOC:控制反转,把创建对象的过程交给Spring进行管理
- AOP:面向切面,不修改源代码的情况下进行功能增强
- Spring特点
- 方便解耦,简化开发
- AOP编程支持
- 方便程序测试
- 方便和其他框架进行整合
- 降低JavaEE API调用难度
- 方便事务的管理

---

### 2.IOC概念和原理

- 什么是IOC
- 控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理

### 2.IOC概念和原理

```
1.什么是IOC
(1)控制反转,把对象创建和对象之间的调用过程,交给spring进行管理
(2)使用IOC的目的,为了耦合度降低
2.IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
3.Spring提供IOC容器实现两种方式:(两个接口)
(1).BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用
(2).ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人员进行使用。有两个重要的实现类,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext
```

### 3.IOC操作Bean管理

```
1.什么是Bean管理
(1)Spring创建对象
(2)Spring注入属性
2.Bean管理操作有两种方式
(1)基于XML配置文件方式实现
(2)基于注解方式实现
```

### 4.IOC操作Bean管理(基于XML方式)

```
1.基于XML方式创建对象
<bean id="user" class="com.wyl.User"></bean>
id属性:唯一标识
class属性:类全路径(包类路径)

创建对象的时候,默认也是执行无参数构造方法完成对象创建
2.基于XML方式注入属性
(1)DI:依赖注入,就是注入属性
第一种注入方式:使用set方式进行注入
<bean id="book" class="com.wyl.Book">
<property name="bName" value="三国演义"></property>
<property name="bPrince" value="100.00"></property>
</bean>
第二种注入方式:使用有参构造进行注入
<bean id="order" class="com.wyl.Orders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="oaddress" value="China"></constructor-arg>
</bean>

<bean id="order" class="com.wyl.Orders">
<constructor-arg index="0" value="abc"></constructor-arg>
<constructor-arg index="1" value="China"></constructor-arg>
</bean>

3.p名称空间注入
(1)使用P名称空间注入,可以简化基于XML配置方式
第一步 添加p名称空间在配置文件中
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

第二步 进行属性注入,在bean标签里面进行操作
<bean id="book" class="com.wyl.Book" p:bName="九阳神功" p:bPrince="100.00"></bean>

4.c名称空间注入
<bean id="order" class="com.wyl.Orders" c:oname="abc" c:oaddress="China"></bean>

```

### 5.DI和IOC的区别

```
DI是IOC的一种具体实现
```

### 6.IOC操作Bean管理(xml注入其他类型的属性)

```
1.字面量
(1)null值
<bean id="book" class="com.wyl.Book">
<property name="bName" value="abc"></property>
<property name="bPrince" value="100.00"></property>
<property name="bauthor">
<null></null>
</property>
</bean>
(2)属性值包含特殊符号
<bean id="book" class="com.wyl.Book">
<property name="bName" value="abc"></property>
<property name="bPrince" value="100.00"></property>
<property name="bauthor">
<value>&lt;&lt;南京&gt;&gt;</value>
</property>
</bean>

<bean id="book" class="com.wyl.Book">
<property name="bName" value="abc"></property>
<property name="bPrince" value="100.00"></property>
<property name="bauthor">
<value><![CDATA[<<南京>>]]></value>
</property>
</bean>

2.注入属性-外部bean
<bean id="userDao" class="com.wyl.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.wyl.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
3.注入属性-内部bean和级联赋值
<bean id="emp" class="com.wyl.bean.Emp">
<property name="empName" value="abc"></property>
<property name="empSex" value="男"></property>
<property name="empDept">
<bean id="dept" class="com.wyl.bean.Dept">
<property name="deptNo" value="1"></property>
<property name="deptName" value="保安科"></property>
</bean>
</property>
</bean>
级联
<bean id="emp" class="com.wyl.bean.Emp">
<property name="empName" value="abc"></property>
<property name="empSex" value="男"></property>
<property name="empDept" ref="dept"></property>
</bean>

<bean id="dept" class="com.wyl.bean.Dept">
<property name="deptNo" value="1"></property>
<property name="deptName" value="保安科"></property>
</bean>

修改属性值
<bean id="emp" class="com.wyl.bean.Emp">
<property name="empName" value="abc"></property>
<property name="empSex" value="男"></property>
<property name="empDept" ref="dept"></property>
<property name="empDept.deptName" value="技术部"></property>
</bean>

<bean id="dept" class="com.wyl.bean.Dept">
<property name="deptNo" value="1"></property>
<property name="deptName" value="保安科"></property>
</bean>

```

### 7.IOC操作Bean管理(xml注入集合属性)

```
1.注入数组类型属性
2.注入List集合类型属性
3.注入Map集合类型属性
4.注入Set类型属性
<bean id="stu" class="com.wyl.collectiontype.Stu">
<!--数组类型属性注入-->
<property name="course">
<array>
<value>111</value>
<value>222</value>
</array>
</property>

<!--list类型属性注入-->
<property name="list">
<list>
<value>333</value>
<value>444</value>
</list>
</property>
<!--map类型属性注入-->
<property name="maps">
<map>
<entry key="55" value="555"></entry>
<entry key="66" value="666"></entry>
</map>
</property>
<!--set类型属性注入-->
<property name="sets">
<set>
<value>777</value>
<value>888</value>
</set>
</property>
</bean>

5.在集合里面设置对象类型值
<!--list类型属性注入,值是对象-->
<property name="listCourse">
<list>
<ref bean="course"></ref>
<ref bean="course1"></ref>
</list>
</property>

<bean id="course" class="com.wyl.collectiontype.Course">
<property name="courseName" value="Spring5"></property>
</bean>
<bean id="course1" class="com.wyl.collectiontype.Course">
<property name="courseName" value="Mybatis"></property>
</bean>

6.把集合注入部分提取出来
在spring配置文件中引入名称空间util
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!--提取list集合类型属性注入-->
<util:list id="bookListId">
<value>111</value>
<value>222</value>
<value>333</value>
</util:list>
<!--提取list类型属性注入使用-->
<bean id="book" class="com.wyl.collectiontype.Book">
<property name="bookList" ref="bookListId"></property>
</bean>

</beans>
```

### 8.IOC操作Bean管理(FactoryBean)

```
1.Spring有两种类型bean,一种普通bean,另一种工厂bean(FactoryBean)
2.普通bean:在配置文件中定义bean类型就是返回类型
3.工厂bean:在配置文件中定义bean类型可以和返回类型不一样
public class Mybean implements FactoryBean<Course> {
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCourseName("abc");
return course;
}

@Override
public Class<?> getObjectType() {
return null;
}

@Override
public boolean isSingleton() {
return false;
}
}

测试
@Test
public void test08(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Course mybean = context.getBean("mybean", Course.class);
System.out.println(mybean);
}
```

### 9.IOC操作Bean管理(bean作用域)

```
1.在spring里面,设置创建bean实例是单实例还是多实例
2.在spring里面,默认情况下,bean是单实例对象
scope="singleton"
scope="prototype"
设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象
设置scope值是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象


```

### 10.IOC操作Bean管理(bean生命周期)

```
1.生命周期
(1)从对象创建到对象销毁的过程

2.bean生命周期
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
把bean实例传递bean后置处理器的方法 postProcessBeforeInitialization
(3)调用bean的初始化的方法(需要进行配置)
把bean实例传递bean后置处理器的方法 postProcessAfterInitialization
(4)bean可以使用了(对象获取到了)
(5)当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)

3.演示bean生命周期
public class Orders {
private String oname;

public Orders() {
System.out.println("第一步 执行无参构造创建bean实例");
}

public void setOname(String oname) {
this.oname = oname;
System.out.println("第二步 调用set方法设置属性的值");
}
public void initMethod(){
System.out.println("第三步 执行初始化的方法");
}
public void destroyMethod(){
System.out.println("第五步 执行销毁方法");
}
}

测试
@Test
public void test09(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 获取到创建bean实例对象");
System.out.println(orders);
((ClassPathXmlApplicationContext)context).close();
}
执行结果:
第一步 执行无参构造创建bean实例
第二步 调用set方法设置属性的值
在初始化之前执行的方法
第三步 执行初始化的方法
在初始化之后执行的方法
第四步 获取到创建bean实例对象
com.wyl.bean.Orders@1990a65e
第五步 执行销毁方法
4.演示添加后置处理器效果
(1)创建类,实现BeanPostProcessor,创建后置处理器
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}

配置文件:
<?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="orders" class="com.wyl.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="hahah"></property>
</bean>

<!--配置后置处理器-->
<bean id="myBeanPost" class="com.wyl.bean.MyBeanPost"></bean>
</beans>
```

### 11.IOC操作bean管理(xml自动装配)

```
1.什么是自动装配
(1)根据指定装配规则(属性名称或者属性类型),spring自动将匹配的属性值进行注入
(2)演示自动装配过程
(1)根据属性名称自动注入
<bean id="emp" class="com.wyl.autowired.Emp" autowire="byName"></bean>
<bean id="dept" class="com.wyl.autowired.Dept"></bean>

```

### 12.IOC操作bean管理(外部属性文件)

```
1.直接配置数据库信息
(1)配置德鲁伊连接池
<!-- 直接配置连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://192.168.83.2:3306/sunds"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
(2)引入德鲁伊连接池依赖jar包

2.引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,写数据库信息
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://192.168.83.2:3306/sunds
prop.userName=root
prop.password=123456
(2)把外部properties属性文件引入到spring配置文件中
引入context名称空间
在spring配置文件中使用标签引入外部属性文件
<!-- 引入外部属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property- placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>

```

### 13.IOC操作bean管理(基于注解方式)

```
1.什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...)
(2)使用注解,注解可以作用在类上面,方法上面,属性上面
(3)使用注解的目的:简化XML配置

2.spring针对bean管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository
上面四个注解功能是一样的,都可以用来创建bean实例

3.基于注解方式实现对象创建
第一步:引入依赖 spring-aop-5.2.6.jar
第二步:开启组件扫描
<!--开启组件扫描-->
<!-- 1.如果扫描多个包,多个包使用逗号隔开
2.扫描上层目录
-->
<context:component-scan base-package="com.wyl"></context:component-scan>
第三步:创建类,在类上面添加创建对象注解
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
@Component(value = "userService")//<bean id="userService" class="..."/>
public class UserService {
public void add(){
System.out.println("service add....");
}
}


4.开启组件扫描细节配置
<!--实例1
use-default-filters="false" 表示现在不使用默认filter,自己配置filter
context:include-filter,设置扫描哪些内容
-->
<context:component-scan base-package="com.wyl" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--实例2
context:exclude-filter:设置哪些内容不进行扫描
-->
<context:component-scan base-package="com.wyl">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

5.基于注解方式实现属性注入
(1)@AutoWired:根据属性类型进行自动装配
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void teststr(){
userDao.update();
}
}
----------------------------
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("update......");
}
}

(2)@Qualifier:根据属性名称进行自动注入(这个注解的使用和@AutoWired一起使用)
@Service
public class UserService {
@Autowired
@Qualifier(value = "haha")
private UserDao userDao;
public void teststr(){
userDao.update();
}
}
----------------------------
@Repository(value = "haha")
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("update......");
}
}
(3)@Resource:可以根据类型注入,可以根据属性注入
@Service
public class UserService {
// @Resource//根据类型注入
@Resource(name = "haha")
private UserDao userDao;
public void teststr(){
userDao.update();
}
}
--------------------------
@Repository(value = "haha")
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("update......");
}
}
(4)@Value:注入普通类型属性
@Value(value = "abc")
private String name;


6.完全注解开发
(1)创建配置类,替代XML配置文件
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.wyl"})
public class SpringConfig {
}
(2)测试
@Test
public void test02(){
//加载配置类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.teststr();
}
}
```

### 14.AOP(概念)

```
1.什么是AOP
(1)面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能

AOP(底层原理)
1.AOP底层使用动态代理
(1)有两种情况动态代理
第一种:有接口情况,使用JDK动态代理

第二种:没有接口情况,使用CGLB动态代理
```

### 15.AOP(JDK动态代理)

```
1.使用JDK动态代理,使用Proxy类里面的方法创建代理对象
2.编写JDK动态代理代码
(1)创建接口,定义方法
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}
(2)创建接口实现类,实现方法
public class UserDaoImpl implements UserDao {
@Override
public int add(int a, int b) {
return a+b;
}

@Override
public String update(String id) {
return id;
}
}
(3)使用Proxy类创建接口代理对象
public class JDKProxy1 {
public static void main(String[] args) {
Class[] interfaces = {UserDao.class};
UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy1.class.getClassLoader(), interfaces, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//
System.out.println("方法执行之前"+method.getName()+"传递的参数 "+ Arrays.toString(args));
Object res = method.invoke(new UserDaoImpl(), args);

System.out.println("方法执行之后"+res);
return res;
}
});
int add = dao.add(1, 2);
System.out.println("result:"+add);
}
}

```

### 16.AOP(术语)

```
1.连接点
类里面哪些方法可以被增强,这些方法称为连接点
2.切入点
实际被真正增强的方法,称为切入点
3.通知(增强)(加的日志功能)
(1)实际增强的逻辑部分称为通知(增强)
(2)通知有多种类型
前置通知
后置通知
环绕通知
异常通知
最终通知
4.切面
是动作,把通知应用到切入点过程

```

### 17.AOP操作(准备)

```
1.Spring框架一般都是基于AspectJ实现AOP操作
(1)什么是AspectJ
AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作
2.基于AspectJ实现AOP操作
(1)基于XML配置文件

(2)基于注解方式实现
3.在项目工程里面引入AOP相关依赖
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.jar
spring-aop-5.3.9.jar
spring-aspects-5.3.9.jar
spring-beans-5.3.9.jar
spring-context-5.3.9.jar
spring-core-5.3.9.jar
spring-expression-5.3.9.jar
4.切入点表达式
(1)切入点表达式作用:知道对哪个类里面的那个方法进行增强
(2)语法结构:
execution([权限修饰符][返回类型][类的全路径][方法名称]([参数名称]))

举例1:对com.wyl.dao.BookDao类里面的add进行增强
execution(* com.wyl.dao.BookDao.add(..) )
举例2:对com.wyl.dao.BookDao类里面的所有方法进行增强
execution(* com.wyl.dao.BookDao.*(..) )
举例3:对com.wyl.dao包里面的所有类的所有方法进行增强
execution(* com.wyl.dao.*.*(..) )
```

### 18.AOP操作(AspectJ注解)

```
1.创建类,在类里面定义方法
public class User {
public void add(){
System.out.println("add......");
}
}
2.创建增强类(编写增强逻辑)
(1)在增强类里面,创建方法,让不同方法代表不同通知类型
3.进行通知的配置
(1)在spring配置文件中,开启注解扫描
(2)使用注解创建User和UserProxy对象
@Component
public class User {
public void add(){
System.out.println("add......");
}
}
------------------------------------------
@Component
public class UserProxy {
public void before(){
System.out.println("before......");
}
}
(3)在增强类上面添加注解@Aspect
@Component
@Aspect //生成代理对象
public class UserProxy {
public void before(){
System.out.println("before......");
}
}
(4)在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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.wyl"></context:component-scan>
<!--开启Aspect生成代理对象 就是在类上找@Aspect注解,然后把这个类变成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

4.配置不同类型的通知
(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
@Component
@Aspect //生成代理对象
public class UserProxy {
@Before(value = "execution(* com.wyl.aopanno.User.add(..))")
public void before(){
System.out.println("before......");
}
}

5.相同的切入点抽取
//相同切入点抽取
@Pointcut("execution(* com.wyl.proxytest.User.add(..))")
public void pointdemo(){

}
@Before("pointdemo()")
public void before(){
System.out.println("执行增强方法前执行前置方法");
}

6.有多个增强类对同一个方法进行增强,设置增前类的优先级
(1)在增前类的上面添加注解 @Order(数字类型值),数字类型的值越小,优先级越高
@Component
@Aspect
@Order(1)
public class PersonProxy {
@Before("execution(* com.wyl.proxytest.User.add(..))")
public void personBefore(){
System.out.println("person 前置方法");
}
}
```

### 19.AOP操作(AspectJ xml配置文件)

```java
1.配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--创建对象-->
<bean id="book" class="com.wyl.proxyXml.Book"></bean>
<bean id="bookProxy" class="com.wyl.proxyXml.BookProxy"></bean>
<!--配置aop增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* com.wyl.proxyXml.Book.buy(..))"/>
<!--配置切面 -->
<aop:aspect ref="bookProxy">
<!--增强用在具体的方法上-->
<aop:before method="before" pointcut-ref="p"></aop:before>
<aop:after method="after" pointcut-ref="p"></aop:after>
<aop:after-returning method="afterReturning" pointcut-ref="p"></aop:after-returning>
<aop:after-throwing method="afterThrowing" pointcut-ref="p"></aop:after-throwing>
<aop:around method="around" pointcut-ref="p"></aop:around>
</aop:aspect>
</aop:config>
</beans>

2.代理类
public class BookProxy {
public void before() {
System.out.println("前置通知...");
}

public void after() {
System.out.println("后置通知...");
}

public void afterReturning() {
System.out.println("后置返回...");
}

public void afterThrowing() {
System.out.println("后置异常...");
}

public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前......");
proceedingJoinPoint.proceed();
System.out.println("环绕后.....");
}
}
```

### 20.AOP操作(AspectJ 配置类开发)

```java
1.配置类
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.wyl.config"}) //相当于<context:component-scan base-package="com.wyl"></context:component-scan>
@EnableAspectJAutoProxy(proxyTargetClass = true) //相当于<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
public class ConfigAop {

}

2.代理类
@Component
@Aspect
public class UserProxy {
@Before("execution(* com.wyl.config.User.add(..))")
public void before(){
System.out.println("前置方法");
}
@After("execution(* com.wyl.config.User.add(..))")
public void after(){
System.out.println("后置方法");
}
}

3.被代理类
@Component
public class User {
public void add(){
System.out.println("add.....");
}
}

4.测试类
public class AopTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigAop.class);
User user = context.getBean( "user", User.class);
user.add();
}
}
```

---

### 21.JdbcTemplate(概念和准备)

---

- 什么是JdbcTemplate
- Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
- 准备工作
- 引入相关jar把

 

posted @ 2021-12-20 07:39  wyl677  阅读(55)  评论(0编辑  收藏  举报