Spring框架学习笔记02

xiaoxiao2021-02-28  93

1.使用注解配置spring

(1)步骤

1.导包(4+2+spring-aop)

2.为主配置文件引入新的命名空间(约束) 3.开启使用注解代理配置文件

4.在类中使用注解完成配置

(2)将对象注册到容器

@Component("user") //@Service("user")//service层 //@Controller("user")//web层 //@Repository("user")//dao层 //相当于<bean name="user" class="cn.itheima.bean.User"></bean> public class User { }

(3)修改对象的作用范围

(4)值类型注入

1.通过反射的Field赋值,破坏了封装性

2.通过set方法赋值,推荐使用. @Value("lijisheng") public void setName(String name) { this.name = name; }

(5)引用类型注入

(6)初始化|销毁方法

2.STS插件

Eclipse用户可以安装STS插件(Spring Tool Suite)可以方便spring配置文件的编辑,MyEclipse自带

3.spring余junit整合测试

(1)导包4+2+aop+test

(2)配置注解

package cn.itheima.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itheima.bean.User; //帮我们创建容器 @RunWith(SpringJUnit4ClassRunner.class) //指定创建容器时使用哪个配置文件 @ContextConfiguration("classpath:applicationContext.xml") public class Demo { @Resource(name="user") private User u; //将名为user的对象注入到u变量中 }

(3)测试

@Test public void fun1(){ System.out.println(u); }

4.spring中的aop

(1)aop思想介绍

(2)spring中的aop概念

(3)spring实现aop的原理

1.动态代理(优先) 被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术 2.cglib代理(没有接口) 第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.

(4)aop名词学习

5.spring中的aop演示

(1)步骤(xml配置)

1.导包4+2

2.准备目标对象

package cn.itheima.service.impl; import cn.itheima.service.UserService; public class UserServiceImpl implements UserService{ public void save() { System.out.println("新增用户"); } public void delete() { System.out.println("删除用户"); } public void update() { System.out.println("修改用户"); } public void select() { System.out.println("查询用户"); } } 3.准备通知 package cn.itheima.spring_aop; import org.aspectj.lang.ProceedingJoinPoint; //通知类 public class MyAdvice { //前置通知(目标方法运行之前调用) //后置通知(如果出现异常不会调用,目标方法运行之后调用) //环绕通知(目标方法之前和之后都会调用) //异常拦截通知(如果出现异常就会出现) //后置通知(无论是否出现异常都会调用) //前置通知 public void before(){ System.out.println("这是一个前置通知!"); } //后置通知 public void afterReturning(){ System.out.println("这是一个后置通知,出现异常不会调用"); } //环绕通知 public Object around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("这是环绕通知之前的部分"); Object obj = pjp.proceed();//调用目标方法 System.out.println("这是环绕通知之后的部分"); return obj; } //异常通知 public void afterExecption(){ System.out.println("出事了,出现异常了"); } //后置通知 public void after(){ System.out.println("这是后置通知,出现异常也会调用"); } } 4.配置进行织入,将通知织入目标对象中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 准备工作:准备导入aop约束 --> <!-- 1.配置目标对象 --> <bean name="userService" class="cn.itheima.service.impl.UserServiceImpl"></bean> <!-- 2.配置通知对象 --> <bean name="myAdvice" class="cn.itheima.spring_aop.MyAdvice"></bean> <!-- 3.将通知织入目标对象 --> <aop:config> <!-- 配置切入点 public void cn.itheima.service.impl.UserServiceImpl.save() void cn.itheima.service.impl.UserServiceImpl.save() * cn.itheima.service.impl.UserServiceImpl.*(..) * cn.itheima.service.impl.*ServiceImpl.*(..)/* cn.itheima.service.impl..*ServiceImpl.*(..) --> <aop:pointcut expression="execution( * cn.itheima.service.impl.*ServiceImpl.*(..))" id="pc"/> <aop:aspect ref="myAdvice"> <!-- 指定名为before的方法为前置方法 ,切入到pc中--> <aop:before method="before" pointcut-ref="pc"/> <!-- 后置 --> <aop:after-returning method="afterReturning" pointcut-ref="pc"/> <!-- 环绕 --> <aop:around method="around" pointcut-ref="pc"/> <!-- 异常拦截 --> <aop:after-throwing method="afterExecption" pointcut-ref="pc"/> <!-- 后置 --> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans>

(2)步骤(注解配置)

1.导包4+2 2.准备目标对象 3.准备通知 4.配置进行织入,将通知织入目标对象中 <!-- 准备工作:准备导入aop约束 --> <!-- 1.配置目标对象 --> <bean name="userService" class="cn.itheima.service.impl.UserServiceImpl"></bean> <!-- 2.配置通知对象 --> <bean name="myAdvice" class="cn.itheima.spring_aop.MyAdvice"></bean> package cn.itheima.e_annotation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; //通知类 @Aspect //表示该类是一个通知类 public class MyAdvice { //前置通知(目标方法运行之前调用) //后置通知(如果出现异常不会调用,目标方法运行之后调用) //环绕通知(目标方法之前和之后都会调用) //异常拦截通知(如果出现异常就会出现) //后置通知(无论是否出现异常都会调用) @Pointcut(value = "execution( * cn.itheima.service.impl.*ServiceImpl.*(..))") public void pc(){} @Before("MyAdvice.pc()") //指定该方法是前置通知,并指定切入点 public void before(){ System.out.println("这是一个前置通知!"); } //后置通知 @AfterReturning("MyAdvice.pc()") public void afterReturning(){ System.out.println("这是一个后置通知,出现异常不会调用"); } //环绕通知 @Around("MyAdvice.pc()") public Object around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("这是环绕通知之前的部分"); Object obj = pjp.proceed();//调用目标方法 System.out.println("这是环绕通知之后的部分"); return obj; } //异常通知 @AfterThrowing("MyAdvice.pc()") public void afterExecption(){ System.out.println("出事了,出现异常了"); } //后置通知 @After("MyAdvice.pc()") public void after(){ System.out.println("这是后置通知,出现异常也会调用"); } }
转载请注明原文地址: https://www.6miu.com/read-71125.html

最新回复(0)