Detailed introduction to using annotations to configure spring
1. Use annotations to configure spring
1. Steps
1.1 Guide package 4+2+spring-aop
4 represents:
//<bean name="user" class="cn.itcast.bean.User" />//@Component("user")// @Service("user") // service层// @Controller("user") // web层@Repository("user")// dao层
//指定对象的作用范围@Scope(scopeName="singleton")
@Value("tom") private String name;
@Value("tom") public void setName(String name) {this.name = name; }
//@Autowired //自动装配//问题:如果匹配多个类型一致的对象.将无法选择具体注入哪一个对象.//@Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象private Car car;
Recommended method:
@Resource(name="car")//手动注入,指定注入哪个名称的对象private Car car;
@PostConstruct //在对象被创建后调用.init-methodpublic void init(){ System.out.println("我是初始化方法!"); } @PreDestroy //在销毁之前调用.destory-methodpublic void destory(){ System.out.println("我是销毁方法!"); }
Step 1:
Step 2:
Step 3:
2. Directly use spring to install the plug-in eclipse
//帮我们创建容器@RunWith(SpringJUnit4ClassRunner.class)//指定创建容器时使用哪个配置文件@ContextConfiguration("classpath:applicationContext.xml")public class Demo {//将名为user的对象注入到u变量中@Resource(name="user")private User u;
@Testpublic void fun1(){ System.out.println(u); }
2. AOP concept in spring
3. The principle of spring implementing aop
3.2 cglib proxy (no interface) Third-party proxy technology, cglib proxy. Proxy can be generated for any class. The principle of proxy is to inherit the proxy for the target object. If the target object is final modified. Then This class cannot be proxied by cglib. 4.aop noun learning
public class UserServiceImpl implements UserService { @Overridepublic void save() { System.out.println("保存用户!");//int i = 1/0; } @Overridepublic void delete() { System.out.println("删除用户!"); } @Overridepublic void update() { System.out.println("更新用户!"); } @Overridepublic void find() { System.out.println("查找用户!"); } }
//通知类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 proceed = pjp.proceed();//调用目标方法System.out.println("这是环绕通知之后的部分!!");return proceed; }//异常通知public void afterException(){ System.out.println("出事啦!出现异常了!!"); }//后置通知public void after(){ System.out.println("这是后置通知(出现异常也会调用)!!"); } }
<?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/context http://www.springframework.org/schema/aop "><!-- 准备工作: 导入aop(约束)命名空间 --><!-- 1.配置目标对象 --><bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean><!-- 2.配置通知对象 --><bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean><!-- 3.配置将通知织入目标对象 --><aop:config><!-- 配置切入点 public void cn.itcast.service.UserServiceImpl.save() void cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.*() * cn.itcast.service.*ServiceImpl.*(..) * cn.itcast.service..*ServiceImpl.*(..)--><aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/><aop:aspect ref="myAdvice" ><!-- 指定名为before方法作为前置通知 --><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="afterException" pointcut-ref="pc"/><!-- 后置 --><aop:after method="after" pointcut-ref="pc"/></aop:aspect></aop:config></beans>
<?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/context http://www.springframework.org/schema/aop "><!-- 准备工作: 导入aop(约束)命名空间 --><!-- 1.配置目标对象 --><bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean><!-- 2.配置通知对象 --><bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean><!-- 3.开启使用注解完成织入 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
Notification class:
//通知类@Aspect//表示该类是一个通知类public class MyAdvice { @Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void pc(){}//前置通知//指定该方法是前置通知,并制定切入点@Before("MyAdvice.pc()")public void before(){ System.out.println("这是前置通知!!"); }//后置通知@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterReturning(){ System.out.println("这是后置通知(如果出现异常不会调用)!!"); }//环绕通知@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("这是环绕通知之前的部分!!"); Object proceed = pjp.proceed();//调用目标方法System.out.println("这是环绕通知之后的部分!!");return proceed; }//异常通知@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterException(){ System.out.println("出事啦!出现异常了!!"); }//后置通知@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void after(){ System.out.println("这是后置通知(出现异常也会调用)!!"); } }
The above is the detailed content of Detailed introduction to using annotations to configure spring. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PyCharm is a powerful integrated development environment (IDE), and PyTorch is a popular open source framework in the field of deep learning. In the field of machine learning and deep learning, using PyCharm and PyTorch for development can greatly improve development efficiency and code quality. This article will introduce in detail how to install and configure PyTorch in PyCharm, and attach specific code examples to help readers better utilize the powerful functions of these two. Step 1: Install PyCharm and Python

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

Title: How to correctly configure Git in PyCharm In modern software development, the version control system is a very important tool, and Git, as one of the popular version control systems, provides developers with powerful functions and flexible operations. As a powerful Python integrated development environment, PyCharm comes with support for Git, allowing developers to manage code versions more conveniently. This article will introduce how to correctly configure Git in PyCharm to facilitate better development during the development process.

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

PyCharm is a commonly used integrated development environment (IDE). In daily development, using Git to manage code is essential. This article will introduce how to configure Git in PyCharm and use Git for code management, with specific code examples. Step 1: Install Git First, make sure Git is installed on your computer. If it is not installed, you can go to [Git official website](https://git-scm.com/) to download and install the latest version of Git

How to configure a workgroup in Win11 A workgroup is a way to connect multiple computers in a local area network, which allows files, printers, and other resources to be shared between computers. In Win11 system, configuring a workgroup is very simple, just follow the steps below. Step 1: Open the "Settings" application. First, click the "Start" button of the Win11 system, and then select the "Settings" application in the pop-up menu. You can also use the shortcut "Win+I" to open "Settings". Step 2: Select "System" In the Settings app, you will see multiple options. Please click the "System" option to enter the system settings page. Step 3: Select "About" In the "System" settings page, you will see multiple sub-options. Please click

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.
