当前位置:网站首页>SSM framework series - annotation development day2-2
SSM framework series - annotation development day2-2
2022-04-23 12:42:00 【Concise programming】
SSM Frame series —— Annotation development
Annotation development
Why use annotations
To replace the heavy spring To configure bean, Annotations are widely used
Since this article only explains annotation development , So I won't show you the implementation of annotations , I will be in SpringBoot The implementation principle of annotation in the series
Spring Overview of common annotations
| annotation | explain |
|---|---|
| @Component | Used on a class to instantiate Bean |
| @Controller | Use in web Layer class is used to instantiate Bean |
| @Service | Use in service Layer class is used to instantiate Bean |
| @Repository | Use in dao Layer class is used to instantiate Bean |
| @Autowired | Used on fields to inject depending on type dependency |
| @Qualifier | combination @Autowired— Used for dependency injection by name |
| @Resource | amount to @Autowired+@Qualifier, Inject by name |
| @Value | Inject common properties |
| @Scope | mark Bean Role model country |
| @PostConstruct | Use to mark the method on a method that is Bean How to destroy |
| @PreDestroy | Use to mark the method on a method that is Bean How to destroy |
Spring Common overview of extended annotations
The use of the above annotations is not a complete substitute for xml The configuration file , You also need to use annotations to replace the configuration as follows
- Non custom Bean Configuration of :
<bean> - load properties Configuration of files :
<contextproperty-placeholder> - Configuration of component scanning :
<context:component-scan> - Introduce other files :
<import> - etc.
| annotation | explain |
|---|---|
| @Configuration | Used to specify that the current class is a Spring Configuration class , Annotations are loaded from this class when the container is created |
| @ComponentScan | Is used to specify the Spring The package to scan when initializing the container . Function and in Spring Of xml In the configuration file < context:component-scan base-package=“com.itheima”/ > equally |
| @Bean | Use it in a way , The annotation stores the return value of the method to Spring In the container |
| @PropertySource | Used for loading .properties Configuration in file |
| @lmport | Used to import other configuration classes |
| @Test | junit test |
Annotation replaces
@Configuration Flag core configuration class
@Configuration
public class WebConfig implements WebMvcConfigurer {
}
@ComponentScan Configure component scanning
Can replace
<context:component-scan base-package="com.example"></context:component-scan>
Sample code 1( annotation + The configuration file )
As soon as I use the annotation, I'm excited , After all, the fried chicken is cooked
Catalog

applicationContext.xml
The component scanning location needs to be configured in the configuration file
<!-- Configure component scanning -->
<context:component-scan base-package="com.example"></context:component-scan>
Full configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Configure component scanning -->
<context:component-scan base-package="com.example"></context:component-scan>
</beans>
baseMapper
among @Component(“baseMapper”) Instead of <bean id="baseMapper" class=" com.example.mapper.BaseMapper"></bean>
package com.example.mapper;
import org.springframework.stereotype.Component;
@Component("baseMapper")
public class BaseMapper {
public void show() {
System.out.println("baseMapper sout");
}
}
baseService
package com.example.service;
public interface BaseService {
void showBaseMapper();
}
baseServiceImpl
Of course you can use @Resource Instead of @Autowired and @Qualifier
package com.example.service.impl;
import com.example.mapper.BaseMapper;
import com.example.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service(value = "baseService")
public class BaseServiceImpl implements BaseService {
@Autowired
@Qualifier("baseMapper")
private BaseMapper baseMapper;
@Override
public void showBaseMapper() {
baseMapper.show();
}
}
baseController
package com.example.controller;
import com.example.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
public class BaseController {
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
BaseService baseService = (BaseService) app.getBean("baseService");
baseService.showBaseMapper();
}
}
result

Corresponding relation ( important )
Look at the lines of different colors, you should be able to understand

Sample code 2( Pure annotation ! I recommend this )
I'll just list and sample code here 1 Different places
Catalog

Remove applicationContext.xml Component scan in
change to the use of sth. @ComponentScan("com.example") See below baseConfiguration
baseConfiguration
package com.example.configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class BaseConfiguration {
}
baseController
Load configuration class
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(BaseConfiguration.class);
package com.example.controller;
import com.example.configuration.BaseConfiguration;
import com.example.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
public class BaseController {
public static void main(String[] args) {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(BaseConfiguration.class);
BaseService baseService = (BaseService) app.getBean("baseService");
baseService.showBaseMapper();
}
}
result

Corresponding relation
The previous correspondence is the same , here BaseConfiguration Instead of the sample code 1 in baseController Dependency profile correspondence

版权声明
本文为[Concise programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231239441857.html
边栏推荐
- PHP generates JSON to process Chinese
- [unity note] basic lighting in l4unity
- 万事有你 未来可期 | ONES 2022校园招聘正式开启
- RT-thread中关键词解释及部分API
- Zigbee之CC2530最小系统及寄存器配置(1)
- On using go language to create websocket service
- NBIOT的AT指令
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- Number of nodes of complete binary tree
- leetcode-791. 自定义字符串排序
猜你喜欢

Redis deployment of cloud native kubesphere

XinChaCha Trust SSL Organization Validated

Please help me see what this is, mysql5 5. Thanks

航芯技术分享 | ACM32 MCU安全特性概述

Unlock openharmony technology day! The annual event is about to open!

一个平面设计师的异想世界|ONES 人物

Introduction to metalama 4 Use fabric to manipulate items or namespaces

Worder font page font comparison table

Remote sensing image classification and recognition system based on convolutional neural network

Qt绘制文字
随机推荐
Web17——EL与JSTL的使用
Buuctf Web [bjdctf2020] zjctf, but so
万事有你 未来可期 | ONES 2022校园招聘正式开启
QT double buffer drawing
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
Kubernetes 入門教程
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
XinChaCha Trust SSL Organization Validated
Lesson 26 static member functions of classes
解决disagrees about version of symbol device_create
NPDP | how can product managers not be excluded by programmers?
【微信小程序】z-index失效
航芯技术分享 | ACM32 MCU安全特性概述
Kubernetes 入门教程
如何防止网站被黑客入侵篡改
Pagoda panel command line help tutorial (including resetting password)
Keyword interpretation and some APIs in RT thread
Please help me see what this is, mysql5 5. Thanks
Object. The disorder of key value array after keys
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box