search
HomeJavajavaTutorialIntroduction to the annotation method of AOP in spring

 AOP (Aspect Oriented Programming): The professional term for AOP is "Aspect Oriented Programming". What is aspect oriented programming? My understanding is: enhancing functions without modifying the source code. Okay, let's talk about the aop annotation method. This will be mentioned by the way.

1. Build an environment for the aop annotation method (import the following packages)

2. Implement

After the environment is set up, create the project.

 1. Create an interface class (CustomerDao) and add two methods

 2. After the interface class is created, it is natural to create a new implementation class (CustomerDaoImpl) and implement the methods in the interface

 

3. After completing the above basic work, we need to use the idea of ​​springIOC and hand over the customerDaoImpl class to spring management

   Create an xml file named applicationContext.xml in the src directory

   Bean is a tag pair in spring , you can understand it as a small item, the id is the name you choose, but it is usually the class name and the first letter is lowercase. What is added to class is the full path of the class

  Turn on automatic annotation/agent: only if automatic is turned on Agent, the annotations we write next will have real applications

   

   4. The environment and basic classes have been written, and then a Demo class can be created for implementation

  

First we need to take out the CustomerDaoImpl class that exists in the spring container

  Codes '1' and '2' do this.

  As the name suggests, you have to tell it where to get it from, so you have to give it a path

  "2" is the way of annotation, called dependency injection: inject the class written long ago in the xml file into the field customerDao , the name in @Resource refers to the id value in the previous bean

The next step is to test (need to add code '3''4'), write a run1() and add @Test to the method, double-click the method when testing After selecting the name, right-click and select Run on junit. This is the unit test, which is very convenient.

With the previous preparation, after executing the run1 method, you can display the two lines of output in the implementation class

3. aop aspect class

1. Create the aspect class MyAspectAnno.java (in the configuration file Add a bean and continue to hand it over to spring management)

 2. Add functional methods that need to be enhanced

 The code in value is an expression, and the * after public refers to any type of return value. , save() refers to where to enhance or add functions. After these codes are written, it means that the method under the annotation @After will be executed after save() is executed, that is, after(). This is enhancement

But when we write more notifications (surround notifications, pre-notifications, etc.), we have to add expression code every time, and copying and pasting is also troublesome. Here we can add an entry point and implement it as follows:

We only need to do this in the future, and a surrounding notification is added below:

 

  

 The value is no longer a long expression, which is also convenient for future maintenance

The above picture is very clear It shows that we have successfully implemented functional enhancements, using aop ideas and using annotations (the notifications we added are executed after and around the entry point (save()))

The above is the detailed content of Introduction to the annotation method of AOP in spring. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.