使用注解实现SpringIOC和SpringAOP
使用注解实现ioc
@Component:实现Bean组件的定义
@Repository:标注dao类
@Service:标注业务类
@Controller:标注控制类
Bean的自动装配:
@Autowired 默认按类型匹配
@Qualifier 指定Bean的名称
使用注解实现AOP
AspectJ :面向切面的框架,扩展了java语言,定义了AOP语法,能够在编译器提供代码的织入
@AspectJ
AspectJ 5新增的功能,使用jdk5.0注解技术和正规的AspectJ切点表达式语言描述切面
Spring通过集成AspectJ实现了以注解的方式定义增强类,大大减少了配置文件中的工作量
利用轻量级的字节码处理框架asm处理@AspectJ中所描述的方法参数名
****使用@AspectJ,首先保证所用的jdk版本5.0或以上版本
下面用一个案例为大家解释
1 package cn.kgc.springtest2.demo1.dao;
2
3 /**
4 * @author
5 * @create 2019-07-30 15:35
6 * dao层设计模拟数据访问层
7 **/
8 public interface UserDao {
9
10 void save(String date);
11 }
1 package cn.kgc.springtest2.demo1.dao.impl;
2
3
4 import cn.kgc.springtest2.demo1.dao.UserDao;
5 import org.springframework.stereotype.Repository;
6
7 /**
8 * @author
9 * @create 2019-07-30 15:35
10 **/
11 @Repository
12 public class UserDaoImpl implements UserDao {
13 public void save(String date) {
14 System.out.println("数据存入中.....");
15 System.out.println("数据存储完成....内容为"+date);
16 }
17 }
1 package cn.kgc.springtest2.demo1.service;
2
3 public interface UserService {
4 /**
5 *
6 * @param date
7 */
8 void saveUser(String date);
9 }
1 package cn.kgc.springtest2.demo1.service.impl;
2
3
4 import cn.kgc.springtest2.demo1.dao.UserDao;
5 import cn.kgc.springtest2.demo1.service.UserService;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8
9 import javax.annotation.Resource;
10 import javax.annotation.Resources;
11
12 /**
13 * @author
14 * @create 2019-07-30 15:38
15 **/
16
17 @Service("userService")
18 public class UserServiceImpl implements UserService {
19
20 @Resource
21 private UserDao userDao;
22
23 /**
24 * spring 注入方式中的设值注入
25 * @param userDao
26 */
27 /*public void setUserDao(UserDao userDao){
28 this.userDao = userDao;
29 }*/
30
31 /* public UserServiceImpl(UserDao userDao){
32 this.userDao = userDao;
33 }*/
34
35 /**
36 * @param date
37 */
38 public void saveUser(String date) {
39
40 //模拟业务逻辑层做一些判断
41
42 date = date.hashCode()+date;
43 System.out.println("业务逻辑层执行。。。。。。。。。。。。。");
44 userDao.save(date);
45 //模拟
46 throw new RuntimeException("你在这里少打了一个分号");
47 }
48 }
1 package cn.kgc.springtest2.demo1.logger;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.*;
6 import org.springframework.stereotype.Component;
7
8 /**
9 * @author
10 * @create 2019-07-31 17:31
11 **/
12 @Slf4j
13 @Aspect
14 @Component
15 public class ServiceLogger {
16
17 @Pointcut("execution(* cn.kgc.springtest2.demo1.service..*(..))")
18 public void pointcut(){
19
20 }
21 @AfterThrowing(value = "pointcut()",throwing = "c")
22 public void afterThrowing(RuntimeException c){
23 log.error("我们检测到报错情况,信息如下"+c.getMessage());
24 }
25
26 @After("pointcut()")
27 public void after(){
28 log.info("无论上面有没有报错我都执行");
29 }
30
31 /**
32 * 环绕增强
33 * @param point
34 */
35 @Around("pointcut()")
36 public void around(ProceedingJoinPoint point){
37 try {
38 log.info(point.getSignature().getName()+"目标方法执行前 我是前置增强");
39 Object result = point.proceed();
40 log.info("我是后置增强方法执行完成,返回值是"+result);
41 } catch (Throwable throwable) {
42
43 log.error("傻逼报错了 我是异常增强。。。。。信息是"+throwable.getMessage());
44 throwable.printStackTrace();
45 }finally {
46 log.info("傻逼要看见我了,我是最终增强");
47 }
48 }
49
50
51 }
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:context="http://www.springframework.org/schema/context"
5 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6
7 <!--<!–开启注解扫描–>-->
8 <context:component-scan base-package="cn.kgc"/>
9 <!--开启aop注解扫描-->
10 <aop:aspectj-autoproxy/>
11
12 </beans>
@Test
public void saveUser() {
String resource = "springioc.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(resource);
UserService userService = context.getBean("userService", UserService.class);
userService.saveUser("testData");
}
打完上面的示例就可以看懂了
使用注解实现SpringIOC和SpringAOP的更多相关文章
- 【SpringAop】【统一日志处理】注解方式理解以及使用
[注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...
- SpringIOC框架简单实现(注解实现)
SpringIOC框架简单实现(注解实现) 前情回顾 SpringIOE简单介绍 运用注解的方式来实现IOC 首先,让我们来创建一个Dog类 @Component("dog")// ...
- Spring:Spring注解大全
@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象. @Controller public class TestController ...
- Java注解的基本概念和原理及其简单实用
一.注解的基本概念和原理及其简单实用 注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析 ...
- Spring-【高阶】注解-转载
从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以取代 ...
- Spring注解实现原理
[Spring如何使用注解机制完成自动装配] Java实例构造时会调用默认父类无参构造方法,Spring正是利用了这一点,让"操作元素的代码"得以执行. [两种处理策略] ( ...
- Spring - SpringIOC容器详解
一.什么是Spring IOC: Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是 ...
- Spring对注解(Annotation)处理【转】
1.从Spring2.0以后的版本中,spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...
- spring中注解的实现原理
@Autowired和@Resource的区别: 在Java中使用@Autowired和@Resource注解进行装配,这两个注解分别是:1.@Autowired按照默认类型(类名称)装配依赖对象,默 ...
随机推荐
- 【Python自动化Excel】pandas处理Excel数据的基本流程
这里所说的pandas并不是大熊猫,而是Python的第三方库.这个库能干嘛呢?它在Python数据分析领域可是无人不知.无人不晓的.可以说是Python世界中的Excel. pandas库处理数据相 ...
- Texture+PBR两种工作流程
一.导入Texture 1.Inpspector TextureSize 2的n次幂,底层图形学需要,计算更快:不使用2的倍数,系统也会添加像素补全2n: 有最大尺寸限制8k,cubemap最高4k: ...
- 使用Redis分布式锁控制请求串行处理
1.需求背景 在一些写接口的场景下,由于一些网络因素导致用户的表单重复提交,就会在相邻很短的时间内,发出多个数据一样的请求.后台接口的幂等性保证一般都是先检查数据的状态,然后决定是否进行执行写入操作, ...
- vuecli学习01 - 环境搭建
到这个链接下载nvm的安装包:https://github.com/coreybutler/nvm-windows/releases. 然后点击一顿下一步,安装即可! 安装完成后,还需要配置环境变量. ...
- gin中在中间件或handler中使用goroutine
package main import ( "fmt" "github.com/gin-gonic/gin" "log" "tim ...
- TCP和UDP的区别以及应用
TCP定义 传输控制协议 (Transmission Control Protocol).TCP协议是面向连接的通信协议,即传输数据之前,在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算 ...
- <input type="file"> 标签详解
详见:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input/file#attr-multiple 使用 type=" ...
- wget: unable to resolve host address ‘dl.grafana.com’的解决方法
[root@Server-qnrsyp system]# wget --no-check-certificate https://dl.grafana.com/oss/release/grafana_ ...
- C# 获取网页验证码
转载请注明来源:https://www.cnblogs.com/hookjc/ 以下方法必需在WebBrowser控件加载网页完成后才可以调用,否则会提示无法将对象引用到实例,切记!!! public ...
- C字符串倒置-中部对称
问题如图 Code #include<stdio.h> #include<string.h> #define MAX_LENGTH 10//最大字符串长度 void inver ...