130.【Spring注解_AOP】(四)

简介: 130.【Spring注解_AOP】
+关注继续查看

4.@EventListener 监听注解

(1).@EventListener 注解监听方法事件

@EventListener: 这个注解可以标注在方法上,然后包扫描到我们这个包就可以实现我们的注解监听。

1.UserService.java

package com.jsxs.service;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
/**
 * @Author Jsxs
 * @Date 2023/8/27 9:02
 * @PackageName:com.jsxs.service
 * @ClassName: UserService
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class UserService {
    // 设置监听的注解,里面的类是被监听的类。这里我们监听 ApplicationEvent 这个类
    @EventListener(classes = {ApplicationEvent.class})
    public void Listener(ApplicationEvent applicationEvent){ //这里是拿到我们监听的事件,这个是固定的,
        System.out.println("UserService 监听事件为...."+applicationEvent);
    }
}

2.配置类什么的都和上面的一样

package com.jsxs.Test;
import com.jsxs.etx.EtxConfig;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * @Author Jsxs
 * @Date 2023/8/24 10:48
 * @PackageName:com.jsxs.Test
 * @ClassName: IOC_ext
 * @Description: TODO
 * @Version 1.0
 */
public class IOC_ext {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(EtxConfig.class);
        // 1.自定义发布一个事件
        applicationContext.publishEvent(new ApplicationEvent(new String("我发布了一个事件")) {
        });
        applicationContext.close();
    }
}

image

(五)、Spring源码总结

1.Spring源码总结

1. Spring容器在启动的时候,先保存所有注册进来的Bean定义信息
   (1).xml注册bean <bean>
   (2).注解: @Service ....
2.Spring容器保存定义信息之后,将会根据保存的信息在合适的时机创建Bean的实列。
    (1).用到Bean的时候创建组件的实列,利用getBean的方法创建Bean。创建好的Bean实列将会保存在Spring容器中。
    (2).统一创建剩下所有Bean的时候。
3.后置处理器
    (1).每一个Bean创建完成,都会使用各种后置处理器进行处理,来增强我们的Bean功能,后置处理器在bean创建的各个环节中。比如 自动注入功能,Aop代理功能    
4.事件驱动模型
    (1).ApplicationListener 事件监听
    (2).ApplicationEventMulticaster 事件派发      

(六)、WEB 注解

注意事项: servlet3.0的版本要求要使用tomcat7.0+

1.简介与测试

(1).创建一个Maven的web项目

image

(2).创建页面支持

index.jsp

<html>
<body>
<a href="Hello">点击我跳转</a>
</body>
</html>
(3).编写后端
  1. 注解实现映射关系

使用@WebServlet注解之后,我们可以省略我们的web.xml的映射关系

HelloServlet .java

package com.jsxs.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @Author Jsxs
 * @Date 2023/8/27 10:01
 * @PackageName:com.jsxs.servlet
 * @ClassName: HelloServlet
 * @Description: TODO
 * @Version 1.0
 */
@WebServlet("/Hello")  // 1.设置拦截的URL,请求过来就会调用这个类的方法 ⭐
public class HelloServlet extends HttpServlet {  // 2.继承Servlet 并重写两个方法 ⭐⭐
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello..."); ⭐⭐⭐
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  1. 使用web.xml 映射关系
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
<!--  具体处理器: 具体的业务逻辑类-->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.jsxs.servlet.HelloServlet</servlet-class>
  </servlet>
<!--  处理器映射器: 主要根据处理器名字和路径找具体的处理器-->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

image


相关文章
|
1天前
|
数据采集 NoSQL Java
spring的aop
spring的aop
13 0
|
1天前
|
Java 数据库连接 Spring
mybatis与spring集成/spring aop集成pagehelper插件
mybatis与spring集成/spring aop集成pagehelper插件
18 0
|
1天前
|
XML Java 编译器
Spring的AOP和事务
Spring的AOP和事务
16 0
|
1天前
|
缓存 Java Spring
Spring AOP中CGLIB代理对象增强通知执行原理
Spring AOP中CGLIB代理对象增强通知执行原理
18 0
|
1天前
|
缓存 Java Spring
Spring AOP如何为目标方法创建拦截器链?
Spring AOP如何为目标方法创建拦截器链?
50 0
|
1天前
|
Java Spring
Spring AOP拦截器调用的实现
Spring AOP拦截器调用的实现
19 0
|
12天前
|
缓存 监控 安全
深入理解Spring Boot AOP:切面编程的优势与应用
深入理解Spring Boot AOP:切面编程的优势与应用
|
18天前
|
Java Spring
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
25 0
|
18天前
|
监控 安全 Java
作为spring框架的另外的重点AOP的介绍(详细篇)
作为spring框架的另外的重点AOP的介绍(详细篇)
|
19天前
|
前端开发 Java Spring
spring实现简单AOP
spring实现简单AOP
15 0
相关产品
云迁移中心
推荐文章
更多