Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the two configuration methods of spring aop in java framework?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what are the two configuration methods of spring aop in the java framework, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Annotated configuration AOP

Annotations configure AOP (implemented using the AspectJ class library), which is roughly divided into three steps:

1. Use the annotation @ Aspect to define a facet, pointcut (@ Pointcut) and notification type (@ Before, @ AfterReturning,@After,@AfterThrowing,@Around) in the facet.

two。 Develop classes that need to be intercepted.

3. Configure the aspect into xml, of course, we can also use the method of automatic scanning Bean. In that case, it is managed by the Spring AoP container.

You also need to reference aspectJ's jar package: aspectjweaver.jar aspectjrt.jar

Example:

User.java

Package com.bjsxt.model; public class User {private String username; private String password; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password }} / * interface class * / package com.bjsxt.dao; import com.bjsxt.model.User; public interface UserDAO {public void save (User user);}

Implement the interface:

Package com.bjsxt.dao.impl; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; @ Component ("u") public class UserDAOImpl implements UserDAO {public void save (User user) {System.out.println ("user save11d!"); / * throw new RuntimeException ("exception"); * / throw exception}}

Operation class:

Package com.bjsxt.service; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; @ Component ("userService") public class UserService {private UserDAO userDAO; public void init () {System.out.println ("init") } public void add (User user) {userDAO.save (user);} public UserDAO getUserDAO () {return userDAO;} @ Resource (name= "u") public void setUserDAO (UserDAO userDAO) {this.userDAO = userDAO;} public void destroy () {System.out.println ("destroy");}}

Join aop

Package com.bjsxt.aop; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @ Aspect @ Component public class LogInterceptor {@ Pointcut ("execution (public * com.bjsxt.service..*.add (..)") Public void myMethod () {}; / * @ Before ("execution (public void com.bjsxt.dao.impl.UserDAOImpl.save (com.bjsxt.model.User)") * / @ Before ("myMethod ()") public void before () {System.out.println ("method staet");} @ After ("myMethod ()") public void after () {System.out.println ("method after") } @ AfterReturning ("execution (public * com.bjsxt.dao..*.* (..)") Public void AfterReturning () {System.out.println ("method AfterReturning");} @ AfterThrowing ("execution (public * com.bjsxt.dao..*.* (..)") Public void AfterThrowing () {System.out.println ("method AfterThrowing");}}

Configuration file

Test class:

Package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; / / Dependency Injection / / Inverse of Control public class UserServiceTest {@ Test public void testAdd () throws Exception {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml"); UserService service = (UserService) ctx.getBean ("userService") System.out.println (service.getClass ()); service.add (new User ()); System.out.println ("#"); ctx.destroy ();}}

Results:

Class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784

Method staet

User save11d!

Method AfterReturning

Method after

# # #

Note:

@ Aspect: it means that this class is a facet class

@ Componet: since it needs to be managed by Spring as an aspect class, you need to initialize this class into the management of Spring during initialization

@ Befoe: logic of the pointcut (Advice)

Execution... Pointcut syntax

Second: xml configuration aop

Example is the same as above: only the configuration file is different

The following is the configuration tag of Spring, and there are several important attributes in beans:

Xmlns:

Is the default xml document parsing format, that is, spring's beans. The address is http://www.springframework.org/schema/beans.

By setting this property, all properties declared in beans can be used directly through, for example, and so on.

Xmlns:xsi:

Is the specification that xml needs to follow, as you can see through URL, is the unified specification of w3, and then locates all the parsing files through xsi:schemaLocation.

Xmlns:aop:

This is the point, some of the semantic specifications we need to use here, related to aspect-oriented AOP.

Xmlns:tx:

Transaction-related configuration content in Spring.

A XML file can only declare a default specification for semantic parsing.

For example, in the above xml, only beans is the default, and the others need to be used by specific tags, such as aop, which has many attributes. If you want to use it, you must add aop:xxx in front of it. Such as the aop:config above.

Similarly, if the default xmlns is configured with aop-related semantic parsing specifications, you can write config tags directly in xml.

On the java framework of the two spring aop configuration methods are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report