当前位置:网站首页>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
边栏推荐
- Labels and paths
- Kubernetes 入门教程
- What are the forms of attack and tampering on the home page of the website
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
- Markdown语法学习
- Try the server for one month for free, and attach the tutorial
- XinChaCha Trust SSL Organization Validated
- Pagoda panel command line help tutorial (including resetting password)
- Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
- Ad20 supplementary note 3 - shortcut key + continuous update
猜你喜欢
Pre competition practice of TIANTI competition
梳理網絡IP代理的幾大用途
Analysis of InnoDB execution process in MySQL
解锁OpenHarmony技术日!年度盛会,即将揭幕!
风尚云网学习-h5的input:type属性的image属性
What are the forms of attack and tampering on the home page of the website
[vulnhub range] - DC2
Message queuing overview
C set Logo Icon and shortcut icon
NPDP|产品经理如何做到不会被程序员排斥?
随机推荐
云原生KubeSphere部署Mysql
Recommended programming AIDS: picture tool snipaste
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
云原生KubeSphere部署Redis
Image attribute of input: type attribute of fashion cloud learning -h5
BUUCTF WEB [BUUCTF 2018]Online Tool
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
Pre competition practice of TIANTI competition
Source code analysis of synchronousqueue
Sort out several uses of network IP agent
[vulnhub range] - DC2
SSL certificate refund instructions
【蓝桥杯】4月17日省赛刷题训练(前3道题)
What are the forms of attack and tampering on the home page of the website
SQL exercise (I)
Basic software testing Day2 - Case Execution
基于卷积神经网络的遥感影像分类识别系统
洛谷P3236 [HNOI2014]画框 题解
Plato farm - a game of farm metauniverse with Plato as the goal
万事有你 未来可期 | ONES 2022校园招聘正式开启