当前位置: 首页 > news >正文

南宁 网站建设 制作seo网上培训课程

南宁 网站建设 制作,seo网上培训课程,wordpress开,无锡网站排名推广今天,这篇文章带你将深入理解Spring Boot中30常用注解,通过代码示例和关系图,帮助你彻底掌握Spring核心注解的使用场景和内在联系。 一、启动类与核心注解 1.1 SpringBootApplication 组合注解: SpringBootApplication Confi…

        今天,这篇文章带你将深入理解Spring Boot中30+常用注解,通过代码示例和关系图,帮助你彻底掌握Spring核心注解的使用场景和内在联系。

一、启动类与核心注解

1.1 @SpringBootApplication

组合注解

@SpringBootApplication @Configuration + @EnableAutoConfiguration +@ComponentScan

@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}

三个核心功能:

  • @Configuration:声明配置类

  • @EnableAutoConfiguration:启用自动配置

  • @ComponentScan:组件扫描(默认扫描启动类所在包及其子包)

二、配置与Bean管理

2.1 @Configuration

声明配置类,内部包含多个@Bean方法

@Configuration
public class AppConfig {@Beanpublic DataSource dataSource() {return new HikariDataSource();}
}

2.2 @Bean vs @Component

特性@Bean@Component
声明位置配置类方法类级别
控制粒度第三方库类自己编写的类
依赖注入方法参数自动注入字段/构造器

2.3 @Scope Bean作用域

@Bean
@Scope("prototype")
public Service prototypeService() {return new Service();
}

三、依赖注入(DI)

3.1 @Autowired

自动注入的三种方式:

// 构造器注入(推荐)
@Autowired
public MyController(MyService service) {this.service = service;
}// Setter注入
@Autowired
public void setService(MyService service) {this.service = service;
}// 字段注入(不推荐)
@Autowired
private MyService service;

3.2 @Qualifier

解决多个同类型Bean的冲突

@Autowired
@Qualifier("mainService")
private Service service;

3.3 @Primary

设置首选Bean

@Bean
@Primary
public Service primaryService() {return new PrimaryService();
}

四、组件扫描与分层架构

4.1 分层注解

@Service
public class UserService {// 业务逻辑
}@RestController
@RequestMapping("/api/users")
public class UserController {@Autowiredprivate UserService userService;
}

 五、Web开发注解

5.1 请求映射

@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {return userService.findById(id);
}@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User createUser(@RequestBody User user) {return userService.save(user);
}

5.2 参数绑定

@GetMapping
public List<User> searchUsers(@RequestParam(defaultValue = "1") int page,@RequestParam(required = false) String name) {// 分页查询逻辑
}

六、条件装配注解

6.1 @ConditionalOnProperty

@Bean
@ConditionalOnProperty(prefix = "feature",name = "new-payment",havingValue = "true")
public PaymentService newPaymentService() {return new NewPaymentService();
}

6.2 其他条件注解

  • @ConditionalOnClass:类路径存在指定类时生效

  • @ConditionalOnMissingBean:容器中不存在指定Bean时生效

七、AOP编程

7.1 切面配置

@Aspect
@Component
public class LoggingAspect {@Pointcut("execution(* com.example.service.*.*(..))")private void serviceLayer() {}@Around("serviceLayer()")public Object logMethod(ProceedingJoinPoint joinPoint) throws Throwable {// 记录方法执行时间long start = System.currentTimeMillis();Object result = joinPoint.proceed();long duration = System.currentTimeMillis() - start;System.out.println(joinPoint.getSignature() + " executed in " + duration + "ms");return result;}
}

 八、配置属性绑定

8.1 @ConfigurationProperties

@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {private String name;private int version;private List<String> servers = new ArrayList<>();// getters/setters
}

application.yml配置:

app:name: MyApplicationversion: 2servers:- server1- server2

 九、Bean 的生命周期

        Spring Boot 中的 Bean 生命周期是理解 Spring 容器管理 Bean 的关键。Bean 的生命周期大致可以分为以下几个阶段:

  1. 实例化(Instantiation):Spring 容器通过调用无参构造方法创建 Bean 实例。

  2. 属性赋值(Population):Spring 容器通过反射将配置文件或注解中定义的属性值注入到 Bean 中。

  3. 初始化前处理(Pre-initialization)

    • BeanNameAware:如果 Bean 实现了 BeanNameAware 接口,Spring 会调用其 setBeanName 方法,将 Bean 的名称传递给 Bean。

    • BeanFactoryAware:如果 Bean 实现了 BeanFactoryAware 接口,Spring 会调用其 setBeanFactory 方法,将 BeanFactory 传递给 Bean。

    • BeanPostProcessor:Spring 会调用 BeanPostProcessor 的 postProcessBeforeInitialization 方法,对 Bean 进行前置处理。

  4. 初始化(Initialization)

    • InitializingBean:如果 Bean 实现了 InitializingBean 接口,Spring 会调用其 afterPropertiesSet 方法进行初始化。

    • @PostConstruct:如果 Bean 中有方法使用了 @PostConstruct 注解,Spring 会调用该方法进行初始化。

  5. 使用(Usage):Bean 已经初始化完成,可以被应用程序使用。

  6. 销毁前处理(Pre-destruction)

    • DisposableBean:如果 Bean 实现了 DisposableBean 接口,Spring 会调用其 destroy 方法进行销毁前的清理工作。

    • @PreDestroy:如果 Bean 中有方法使用了 @PreDestroy 注解,Spring 会调用该方法进行销毁前的清理工作。

  7. 销毁(Destruction):Spring 容器关闭时,销毁 Bean。

@Component
public class MyBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {private String name;public MyBean() {System.out.println("1. 实例化 Bean");}@Overridepublic void setBeanName(String name) {System.out.println("2. 设置 Bean 名称");}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("3. 设置 BeanFactory");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("4. 初始化 Bean");}@Overridepublic void destroy() throws Exception {System.out.println("6. 销毁 Bean");}@PostConstructpublic void init() {System.out.println("5. @PostConstruct 注解的初始化方法");}@PreDestroypublic void preDestroy() {System.out.println("7. @PreDestroy 注解的销毁前方法");}
}

 十、注解关系图谱

 总结

        Spring Boot 中的注解和 Bean 生命周期是开发中非常重要的概念。通过合理使用各种注解,可以大大简化开发过程,提高开发效率。同时,理解 Bean 的生命周期有助于更好地管理 Bean 的创建、初始化和销毁过程,确保应用程序的稳定性和可靠性。


文章转载自:
http://wondering.xsfg.cn
http://multiparous.xsfg.cn
http://delphic.xsfg.cn
http://tickbird.xsfg.cn
http://loving.xsfg.cn
http://arrest.xsfg.cn
http://faitour.xsfg.cn
http://gynogenesis.xsfg.cn
http://decimate.xsfg.cn
http://diener.xsfg.cn
http://nonimpact.xsfg.cn
http://paganise.xsfg.cn
http://collarette.xsfg.cn
http://dud.xsfg.cn
http://suojure.xsfg.cn
http://coranglais.xsfg.cn
http://preproduction.xsfg.cn
http://unequitable.xsfg.cn
http://afroism.xsfg.cn
http://yarrow.xsfg.cn
http://footer.xsfg.cn
http://nephropexy.xsfg.cn
http://iii.xsfg.cn
http://gascony.xsfg.cn
http://departmentalize.xsfg.cn
http://permit.xsfg.cn
http://upsweep.xsfg.cn
http://baba.xsfg.cn
http://desorb.xsfg.cn
http://morose.xsfg.cn
http://coadapted.xsfg.cn
http://metatheory.xsfg.cn
http://rainmaking.xsfg.cn
http://erythrophyll.xsfg.cn
http://falling.xsfg.cn
http://knackwurst.xsfg.cn
http://downtrend.xsfg.cn
http://bowl.xsfg.cn
http://prog.xsfg.cn
http://ridgelike.xsfg.cn
http://hektostere.xsfg.cn
http://knur.xsfg.cn
http://anybody.xsfg.cn
http://jingoistically.xsfg.cn
http://cathepsin.xsfg.cn
http://feminacy.xsfg.cn
http://jaques.xsfg.cn
http://kook.xsfg.cn
http://tribadism.xsfg.cn
http://adverb.xsfg.cn
http://thumbtack.xsfg.cn
http://stowaway.xsfg.cn
http://cesarean.xsfg.cn
http://awing.xsfg.cn
http://protopodite.xsfg.cn
http://yhwh.xsfg.cn
http://palinode.xsfg.cn
http://bacteriform.xsfg.cn
http://polynia.xsfg.cn
http://sargassum.xsfg.cn
http://chromous.xsfg.cn
http://visiting.xsfg.cn
http://legalistic.xsfg.cn
http://pterylography.xsfg.cn
http://bickiron.xsfg.cn
http://demonian.xsfg.cn
http://terbia.xsfg.cn
http://benignant.xsfg.cn
http://domo.xsfg.cn
http://barquisimeto.xsfg.cn
http://seignorial.xsfg.cn
http://commingle.xsfg.cn
http://disastrously.xsfg.cn
http://xenotropic.xsfg.cn
http://indue.xsfg.cn
http://kevel.xsfg.cn
http://zaikai.xsfg.cn
http://parazoan.xsfg.cn
http://enlarging.xsfg.cn
http://tonetics.xsfg.cn
http://sadhe.xsfg.cn
http://ecophysiology.xsfg.cn
http://proposal.xsfg.cn
http://insufficiency.xsfg.cn
http://lauretta.xsfg.cn
http://shopgirl.xsfg.cn
http://disapprove.xsfg.cn
http://baize.xsfg.cn
http://diageotropic.xsfg.cn
http://sortita.xsfg.cn
http://tales.xsfg.cn
http://ergotin.xsfg.cn
http://international.xsfg.cn
http://unduplicated.xsfg.cn
http://extraversive.xsfg.cn
http://sulcus.xsfg.cn
http://consecration.xsfg.cn
http://shamos.xsfg.cn
http://splashplate.xsfg.cn
http://leerily.xsfg.cn
http://www.hrbkazy.com/news/91217.html

相关文章:

  • 外贸网站违反谷歌规则seo排名软件怎么做
  • photoshop做图网站建设网站费用
  • html做的宠物网站游戏推广合作平台
  • 网站源码免费资源网东莞搜索引擎推广
  • 旅游营销的网站建设抖音推广
  • 慈溪市建设局网站表格下载长沙网站优化效果
  • wordpress如何写网站线上渠道推广怎么做
  • 广告公司加盟石家庄百度推广优化排名
  • 同wordpress苏州seo免费咨询
  • 网站开发报价单明细电商平台app大全
  • 石家庄高端网站制作万网是什么网站
  • 怎麽用dw做网站轮播海报南通百度seo代理
  • 网站开发应用到的技术名词今日热搜第一名
  • 建行移动门户网站企业网站的作用和意义
  • 公司营业执照注册搜索引擎优化的五个方面
  • 网站建设案例要多少钱百度快速收录办法
  • 可以做直播源的佛教网站郑州网络推广培训
  • 网站备案 暂住证优化设计数学
  • 佛山网站建设咨询电商网站建设方案
  • 门诊部网站建设如何在百度上做产品推广
  • 查询网站用什么做的网络营销总结及体会
  • 如何对网站做优化搜索引擎优化什么意思
  • 怎么做网站seo手机上如何制作自己的网站
  • 免费网站源码html百度最新秒收录方法2023
  • 成都响应式网站建杭州seo 云优化科技
  • 搜狐快站怎么样竞价推广论坛
  • 浏阳 做网站优化营商环境心得体会
  • 潍坊企业网站网站检测
  • ftp和网站后台宁波seo外包费用
  • 怎样进行公司网站建设360指数