当前位置:网站首页>Annotation development
Annotation development
2022-07-26 02:26:00 【lwj_ 07】
As we mentioned earlier, when we didn't use annotation to develop definitions , The code is written in the following form :
spring.xml file :

BookDaoImpl:

Program testing :

reflection : How can the above code be implemented in the form of annotation development : The demonstration is as follows
One 、 Annotation development definition bean

The code demo is shown below :

spring Of xml The configuration file :( All you need to do here is add context Of xmlns and http label , Then start annotation scanning ) The purpose of scanning is to find out whether there is @Component("#") annotation , The annotation is to get bean Of

BookDaoImpl:( All you need to do here is to add annotation components )

Finally, add a value to the annotation , By this value (bookDao) You can get IOC The managed objects in the container /bean 了 ( It's the same as not using annotation theory )
Two 、 Pure annotation development mode
Pure annotation development mode : That is to say, use java Class instead of spring Of xml The configuration file 【 in other words : We no longer have spring Of xml Profile , We use java Class is replaced by 】

The code is shown as follows : ( Be careful :spring Of xml The configuration file has been used by our java Class replaced )
java Class substituted spring Of xml The configuration file :

package com.itheima.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// Declare the current class as Spring Configuration class
/*
@Configuration Annotation equivalent spring Of xml The following contents in the configuration file :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
* */
/*
@ComponentScan({"com.itheima.service","com.itheima.dao"}) Annotation equivalent spring Of xml The following contents in the configuration file :
<!-- a key : Turn on annotation scanning ( That means scanning com.itheima Is there a class under the package that uses annotation )-->
<context:component-scan base-package="com.itheima"/>
*/
@Configuration
// Set up bean Scan path , Multiple paths are written in string array format
@ComponentScan({"com.itheima.service","com.itheima.dao"}) // You can use arrays , Multi annotation scanning form ( Scan the package for @Component("#") Annotations )
public class SpringConfig {
}
The scanned ones are added with acquisition bean Object annotation BookDaoImpl class :

adopt The scanned ones are added bean The value in the annotation of the object , You can get what should be managed BookDaoImpl object :
3、 ... and 、 Annotation development bean Scope and lifecycle management
java Class substitution spring Of xml Profile class :

com.itheima Annotations are configured under the package bean Of BookDaoImpl:


3.1、 Before developing with annotations , When we want to turn it into a non singleton form , Is in spring Of xml In the configuration file , Add non singleton attributes ( By default, it is a singleton ), Now we use the pure annotation development form , So how to turn the code into a non singleton form is as follows :
You only need to configure non singleton annotations :@Scope("#") prototype: Non singleton singleton: Single case

3.2、 Before development without annotations , We are in spring Of xml Add properties to the configuration file , Then judge the life cycle ,( That is, life start cycle and life destruction cycle , And remember to close the hook in the life destruction cycle , Because the code goes to save() Method After the end java The virtual machine shuts down The method of life cycle destruction will not be implemented ), So when we use annotation development , How to operate it , The code is shown as follows :
Be careful 1: Parameterless construction is performed first
Be careful 2: Methods to implement the life destruction cycle , You must close the hook and ensure that IOC Containers are singleton forms
The code is shown as follows :
java Code instead of spring Of xml Profile class :

com.itheima The package contains bean Annotated BookDaoImpl class :

Sequencing test :

Four 、 Dependency injection
4.1、 Inject dependencies of reference types
Dependency format of reference type :private BookDao bookDao; // Dependency relationship
The code is shown as follows :
java Instead of the spring Of xml Profile class :

Before, when we didn't use annotation development , Handling dependencies is done in spring Of xml Add the processing dependency tag in the configuration file , And then through setter Injection mode or constructor injection mode ,
Now we use annotation development Just use automatic assembly ( Simple 、 Brutal )
com.itheima The package contains bean annotation @Service Of BookServiceImpl class :

BookDaoImpl Dependent class ( Dependent class BookDao When there's only one , Then automatic assembly will automatically find the dependent class , Then deal with dependencies , You can also call save The method , If there are multiple dependent classes , Then there may be small problems ):

Sequencing test :

4.1.1、 But the use of annotations in the form of automatic assembly , There will be the following questions :

Because the dependency is BookService and BookDao Dependencies between , If there are two implementations in the dependent class BookDao When relying on the class of the object , Then we don't know which annotation to assemble for us , So there's a problem :


Running results :( In a nutshell , I don't know how to choose these two implementations for automatic assembly BookDao Which of the classes of the object , So it's better to report the error to you directly )

4.1.2、 So how to solve the problem of assembling one of multiple dependent classes :
First step : Give the dependent classes bean The value of the annotation


The second step : add @Qualifer Annotate and annotate the dependent classes you want to assemble bean Value
Because in dependent classes @Repository("bookDao") // It's equivalent to getting : <bean id="bookDao" class="com.itheima.dao.Impl.BookDaoImpl" /> Managed objects
That is to say, add @Qualifer Annotate and annotate the dependent classes you want to assemble bean The value of is equivalent to getting the object of the dependent class , That is, the dependency relationship is handled : private BookDao bookDao = new BookDaoImpl(); Format


4.2、 Inject simple type dependencies
Before, there was no development with annotations , Handle simple type dependencies , We do it through spring Of xml In the configuration file, the tag attribute is used for dependency processing , Now let's use annotations to demonstrate the following :
Just use in simple types @Value("#") Annotations can be :
java Instead of spring Medium xml Class of configuration file :

BookServiceImpl:

Dependent on BookDao object :( There is simple type injection )

The test program :

4.3、 How to use annotation to load properties file
First step : First of all, now java Instead of the spring Of xml Add @PropertySource("#") Note form
Be careful 1: The annotation can be in the form of an array , Configure multiple properties file ( Use an array to configure multiple properties When you file , We must ensure these properties File exists )
Be careful 2: Wildcard format is not supported in this annotation ( That is, we did not use annotations to load when developing properties File form )

jdbc.properties file :

The second step : Add... To simple type injection @Value("#") annotation , And use ${ Place holder } 【 Place holder :properties The left attribute name in the file 】


边栏推荐
- Implementation of Ti am335x industrial control module network and file system nfs
- Sword finger offer 28. symmetric binary tree
- GAMES101复习:光栅化
- scipy.sparse.vstack
- Wechat applet decryption and unpacking to obtain source code tutorial
- prometheus+redis-exporter+grafana 监控redis服务
- 微信小程序解密并拆包获取源码教程
- 19_ Request forms and documents
- obsidian移动端PC段同步
- Get hours, minutes and seconds
猜你喜欢
![[C]详解语言文件操作](/img/12/4affa1d3fb3e4ee126e1c1e3872d9b.png)
[C]详解语言文件操作

(CVPR 2019) GSPN: Generative Shape Proposal Network for 3D Instance Segmentation in Point Cloud

Prometheus+blackbox exporter+grafana monitoring server port and URL address

商业智能BI全解析,探寻BI本质与发展趋势

1. Mx6ul core module serial WiFi test (VIII)

图解B+树的插入过程
![[xxl-job] xxl-job learning](/img/2c/d3872983e4228a3ef52a9d1bef836e.png)
[xxl-job] xxl-job learning

Implementation of Ti am335x industrial control module network and file system nfs

Illustration of the insertion process of b+ tree

Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
随机推荐
Games101 review: rasterization
uni-app跨域配置
scipy.sparse.vstack
[pyqt5 packaged as exe]
GAMES101复习:光栅化
18.删除链表的倒数第n个节点
Audio and video technology development weekly | 254
如何加速矩阵乘法
I came to the library applet one click sign in and one click grab location tool
Adruino basic experimental learning (I)
Postman报Json序列化错误
Yum install MySQL FAQ
MySQL建Websites数据表
MySQL(4)
scipy.sparse.csr_matrix
Business Intelligence BI full analysis, explore the essence and development trend of Bi
Adruino 基础实验学习(一)
1. Mx6ul core module use serial -rs485 test (x)
2022.7.25-----leetcode.919
18_ Request file