首页 > 编程知识 正文

spring自定义bean标签,spring自定义注解切面

时间:2023-05-04 19:25:50 阅读:28412 作者:1129

原文URL:springboot----评论--@ springbootapplication/@ mappers can /定制启动类评论_IT利刃出鞘的博客-CSDN博客

摘要本文介绍了SpringBoot启动类中的一些常用注释,以及如何自定义启动类的注释。

@ springbootapplication http://www.Sina.com /

示例:

@ springbootapplication (scanbasepackages=' com.test.chapter4' )scanBasePackages这个basepackages其实是3358 www.sisina

33558 www.Sina.com/@springbootapplication只扫描带@ springbootapplication注释标记的类包及其子包的类,并在类中添加@ compotapplication

注释定义中添加了@Component,因此注释也会扫描到@Controller、@Service、@Configuration

@映射扫描作用:定义扫描MyBatis接口。

对于SpringBoot,在http://www.Sina.com/: application.properties中指定Mapper无效。 my batis.mapper-locations=class path 3360 com

@ComponentScan

package com.example.demo; importorg.my batis.spring.annotation.mappers can; importorg.spring framework.boot.spring应用程序; importorg.spring framework.boot.auto configure.springbootapplication; @ springbootapplication @ mappers can (annotation class=mapper.class,base packages=' com.spring boot.chapter 15.mapppper

对于Mybatis-Plus,不需要写为annotationClass

package com.example.demo; importorg.my batis.spring.annotation.mappers can; importorg.spring framework.boot.spring应用程序; importorg.spring framework.boot.auto configure.springbootapplication; @ springbootapplication @ mappers can (base packages=' com.spring boot.chapter 15.mapper ' ) publicclassdemoapplication { }}

例如,com.spring boot.chapter 15.mapper,com.spring boot.chapter 16.section1. mapper,两种方法:

法1:@ mappers can (base packages=' com.spring boot.*.*.mapper ' ) ) ) ) ) ) ) )。

法2:@ mappers can (base packages={ ' com.spring boot.chapter 15.mapper ',' com.spring boot.chapter 16.section1. mmion

@MapperScan作用

启动Spring时,扫描所有映射器文件,生成代理类并由Spring容器管理;

@映射器作用

启动项目后,当您扫描由此注释标识的接口类时,将创建代理类并将其管理在spring容器中。 如果不添加@MapperScan注释,则必须为Mapper接口类添加@Mapper注释。

@存储库注释

@Repository是spring注释,它主动标识当前类由spring容器管理。 相当于@Component注释。

通常,如果添加@Mapper或@MapperScan注释,则不需要添加@Repository。 因为@Mapper注释(@MapperScan注释)为spring容器管理提供了更改接口的代理类。

但是,IDEA可能无法识别@Mapper注释,并且在自动编译项目时可以通过在项目上添加红色波浪线并添加@Respository注释来抑制此错误。

在自定义启动类注释微服务项目中,多个微服务可能会在启动类中添加相同的注释以启用功能。 例如,因为我使用的是SpringCloud,所以每个项目的启动类都必须包含@EnableDiscoveryClient。 麻烦。 解决方法是把使用的评论集中起来发表。

package com.example.com mon.com mon.annotation; importorg.my batis.spring.annotation.mappers can; importorg.spring framework.boot.auto configure.springbootapplication; importorg.spring framework.boot.auto configure.JDBC.datasourceautoconfiguration; importorg.spring framework.cloud.client.discovery.enablediscoveryclient; importorg.spring framework.cloud.open feign.enablefeignclients; importorg.spring framework.core.annotation.alias for; import java.lang.annotation.*; @ inherited @ target (element type.type ) retention ) retentionpolicy.runtime (映射扫描) ' com.example.** . mmmmm managed @ springbootapplication @ enablediscoveryclient @ enablefeignclients (com.example.com mon.feign ) )。 public @ interfacecommonapplication { @ alias for (annotation=springbootapplication.class (string ) scanbasepackages ) ) 例如,情况如下:

package com.example.order; import com.example.com mon.com mon.annotation.common application; importorg.spring framework.boot.spring应用程序; @ commonapplicationpublicclassorderapplication (publicstaticvoidmain ) string[]args ) springapplication.run ) order应用程序

package com.example.order; import com.example.com mon.com mon.annotation.common application; importorg.spring framework.boot.spring应用程序; importorg.spring帧. scheduling.annotation.enable scheduling; @ enable scheduling @ commonapplication publicclassorderapplication { publicstaticvoidmain { string [ ] args } { spring应用程序

springboot快速入门和@SpringBootApplication注释分析

《深入浅出Spring Boot 2.x》=3.2.1通过扫描组装您的Bean

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。