2024年一、SpringBoot2基础入门--06-SpringBoot的一些底层注解,「高并发秒杀

Ending

Tip:由于文章篇幅有限制,下面还有20个关于MySQL的问题,我都复盘整理成一份pdf文档了,后面的内容我就把剩下的问题的目录展示给大家看一下

如果觉得有帮助不妨【转发+点赞+关注】支持我,后续会为大家带来更多的技术类文章以及学习类文章!(阿里对MySQL底层实现以及索引实现问的很多)

吃透后这份pdf,你同样可以跟面试官侃侃而谈MySQL。其实像阿里p7岗位的需求也没那么难(但也不简单),扎实的Java基础+无短板知识面+对某几个开源技术有深度学习+阅读过源码+算法刷题,这一套下来p7岗差不多没什么问题,还是希望大家都能拿到高薪offer吧。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

2、当配置类中proxyBeanMethods = false时,每次调用的宠物对象不是容器中的对象,是新创建的。

在这里插入图片描述

@Configuration(proxyBeanMethods = false)//告诉springboot这是一个配置类 == 配置文件

public class MyConfig {

@Bean//给springboot组件添加容器。方法名作为组件的id;返回值类型就是组件类型;返回的值就是组件在容器中的实例。

public User user01(){

User zhangsan = new User(“zhangsan”, 3);

//User组件依赖了Pet组件

zhangsan.setPet(tomcatPet());

return zhangsan;

}

@Bean(“pet01”)

public Pet tomcatPet(){

return new Pet(“tomcat”);

}

}

方法返回值是false,因为两者调用的不是同一个对象。

在这里插入图片描述

2、其他可以向容器中注册组件的注解

================================================================================

@Bean:给容器中添加组件

@Component:代表只一个组件

@Controller:表示是一个控制器

@Service:代表它是一个业务逻辑

@Repository:代表这是一个数据库层组件

以上注解跟以前在spring中的用法一致

3、@ComponentScan@Import

======================================================================================

/*

  • @ComponentScan:指定容器中包扫描规则。通常用在主程序类型上

  • @Import({User.class, DBHelper.class})

  •  给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名。
    

*/

@Import({User.class, DBHelper.class})

@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件

public class MyConfig {

}

@ComponentScan:

在这里插入图片描述@Import注解:

在这里插入图片描述

@Import 高级用法

4、@Conditional

=============================================================================

条件装配:满足Conditional指定的条件,则进行组件注入

ctrl+H:查看该注解继承树,如下所示:

在这里插入图片描述注意

1、在使用@ConditionalOnBean和@ConditionalOnMissingBean注解做判断时需要注意组件注入容器的先后顺序,结果会受组件注入容器的先后顺序影响的。

2、如果使用@ConditionalOnClass和@ConditionalOnMissingClass注解则不需要关注组件注入容器的先后顺序。

=条件装配,配置类中的代码======

@Configuration(proxyBeanMethods = true)

public class MyConfig {

@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例

//@ConditionalOnBean(name = “tom22”)//意思是容器中有tom22组件时才创建user01组件

@ConditionalOnMissingBean(name = “tom22”)//意思是容器中没有tom22组件时才创建user01组件

public User user01(){

User zhangsan = new User(“zhangsan”, 18);

//user组件依赖了Pet组件

zhangsan.setPet(tomcatPet());

return zhangsan;

}

@Bean(“tom22”)

public Pet tomcatPet(){

return new Pet(“tomcat”);

}

}

=条件装配,以下是主程序中的测试代码======

public static void main(String[] args) {

//1、返回我们IOC容器

ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

//2、查看容器里面的组件

String[] names = run.getBeanDefinitionNames();

for (String name : names) {

System.out.println(name);

}

boolean tom = run.containsBean(“tom”);

System.out.println(“容器中Tom组件:”+tom);

boolean user01 = run.containsBean(“user01”);

System.out.println(“容器中user01组件:”+user01);

boolean tom22 = run.containsBean(“tom22”);

System.out.println(“容器中tom22组件:”+tom22);

}

5、原生配置文件引入(原来spring的bean.xml配置文件)

================================================================================================

spring配置文件:beans.xml===

<?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”>

5.1、@ImportResource 导入资源


@ImportResource(“classpath:beans.xml”)/*允许用以前spring

配置文件的方式将组件扫描进容器内。相当于将类路径下的spring

配置文件重新解析,放到容器中。*/

public class MyConfig {

}

======springBoot主程序中测试:看是否将上述bean.xml中的组件创建到容器中=

@SpringBootApplication

public class MainApplication {

public static void main(String[] args) {

//1、返回我们的ioc容器

ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class,args);

boolean haha = run.containsBean(“haha”);

boolean hehe = run.containsBean(“hehe”);

System.out.println(“haha:”+haha);//true

System.out.println(“hehe:”+hehe);//true

}

}

6、配置绑定

=====================================================================

如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用。

public class getProperties {

public static void main(String[] args) throws FileNotFoundException, IOException {

Properties pps = new Properties();

pps.load(new FileInputStream(“a.properties”));

Enumeration enum1 = pps.propertyNames();//得到配置文件的名字

while(enum1.hasMoreElements()) {

String strKey = (String) enum1.nextElement();

String strValue = pps.getProperty(strKey);

System.out.println(strKey + “=” + strValue);

//封装到JavaBean。

}

}

}

6.1、将配置文件跟一个JavaBean进行绑定


方式一 @Component +@ConfigurationProperties

Car实体类

/**

  • 只有在容器中的组件,才会拥有SpringBoot提供的强大功能

*/

@Component

@ConfigurationProperties(prefix = “mycar”)//配置属性

public class Car {

private String brand;

private Integer price;

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

public Integer getPrice() {

return price;

}

public void setPrice(Integer price) {

this.price = price;

}

@Override

public String toString() {

return “Car{” +

“brand='” + brand + ‘’’ +

“, price=” + price +

‘}’;

}

}

application.properties配置文件:

mycar.brance=BYD

mycar.price=100000

控制层:

@RestController

public class HelloController {

@Autowired

Car car;

@RequestMapping(“/car”)

public Car car(){

return car;

}

}

最后运行SpringBoot主程序

在这里插入图片描述

方式二 @EnableConfigurationProperties + @ConfigurationProperties

需要在配置类上加注解,使JavaBean加载到容器中:

//1、开启Car配置绑定功能

//2、把这个Car这个组件自动注册到容器中

@EnableConfigurationProperties(Car.class)

public class MyConfig {

}

属性绑定:

/**

  • @Description :车辆实体类

  • @Author :lenovo

  • @Date :2021/2/18 15:33

*/

//@Component

@ConfigurationProperties(prefix = “mycar”)

public class Car {

private String brance;

private Integer price;

public String getBrance() {

return brance;

}

public void setBrance(String brance) {

this.brance = brance;

}

public Integer getPrice() {

return price;

}

public void setPrice(Integer price) {

this.price = price;

}

@Override

public String toString() {

return “Car{” +

“brance='” + brance + ‘’’ +

“, price=” + price +

最后

分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。

这些面试题相对应的技术点:

  • JVM
  • MySQL
  • Mybatis
  • MongoDB
  • Redis
  • Spring
  • Spring boot
  • Spring cloud
  • Kafka
  • RabbitMQ
  • Nginx

大类就是:

  • Java基础
  • 数据结构与算法
  • 并发编程
  • 数据库
  • 设计模式
  • 微服务
  • 消息中间件

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 数据结构与算法
  • 并发编程
  • 数据库
  • 设计模式
  • 微服务
  • 消息中间件

[外链图片转存中…(img-TeCVWIOi-1715050571505)]

[外链图片转存中…(img-9mrh1yfp-1715050571506)]

[外链图片转存中…(img-o3H9aLov-1715050571506)]

[外链图片转存中…(img-fk6AqHhu-1715050571507)]

[外链图片转存中…(img-J2mEpRbw-1715050571507)]

[外链图片转存中…(img-5NHOXoHE-1715050571508)]

[外链图片转存中…(img-bXx4JSzj-1715050571508)]

[外链图片转存中…(img-6Hiyug3P-1715050571508)]

[外链图片转存中…(img-rW221Bcl-1715050571509)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值