1.springboot的起步依赖原理

按住Ctrl点击pom.xml中的spring-boot-starter-parent,会跳转到了spring-boot-starter-parent的pom.xml
按住Ctrl点击pom.xml中的spring-boot-starter-dependencies,会跳转到了spring-boot-starter-dependencies的pom.xml
spring-boot-starter-dependencies的pom.xml中我们可以发现,一部分坐标的版本、依赖管理、插件管理已经定义好,所以我们的SpringBoot工程继承spring-boot-starter-parent后已经具备版本锁定等配置了。所以起步依赖的作用就是进行依赖的传递。


在spring-boot-starter-dependencies的pom.xml中定义了各类组件和插件的依赖版本,从而让开发人员不需要考虑版本问题
从以上的动作中,我们能够看出,springboot的起步依赖本质上就是在进行依赖的传递,通过以来传递,将自身需要的各种配置文件从父类中继承下来直接使用,从而省去程序员手动导包操作的过程

2.springboot的自动装配原理

@Configrution用于声明定义bean, 这也是springboot自动装配关键的注解之一 其实就是平常Spring配置文件中我们写的bean
@EnableAutoConfiguration 用来开启springboot自动配置的注解 ,这个也是自动装配中很重要的注解
@ConfigurationProperties 用来读取配置文件 并封装成实体类
@EnableConfigurationProperties 这个一般要配合@ConfigurationProperties使用 可以使@ConfigurationProperties封装成的实体类成功注入(前提是@ConfigurationProperties的实体类没有被@Component注解)
面试三-springboot的自动装配原理.png
@SpringBootApplication中有3个主要注解
1.@SpringBootConfiguration:这个注解 这个其实就是上面介绍过的@Configuration的注解 用于定义bean的,springboot的启动类其实也就是作为spring的一个bean注入到spring容器中。
2.@ComponentScan:是spring中的注解 主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中
3.@EnableAutoConfiguration :这个注解 这个是开启自动装配的核心注解,他的作用就是获取配置类 扫描并注入IOC容器中进行管理,他也是组合注解 我们点进去看又能看到

3.手写 一个简单的工厂模式 代码

  1. package com.ithiema.coffee;
  2. public class AmericanCoffee extends Coffee{
  3. public String getName(){
  4. return "美式咖啡";
  5. }
  6. }
  7. package com.ithiema.coffee;
  8. public class LatteCoffee extends Coffee{
  9. public String getName(){
  10. return "拿铁咖啡";
  11. }
  12. }
  13. package com.ithiema.coffee;
  14. public abstract class Coffee {
  15. public abstract String getName();
  16. //加糖
  17. public void addSugar(){
  18. System.out.println("加糖");
  19. }
  20. //加奶
  21. public void addMilk(){
  22. System.out.println("加奶");
  23. }
  24. }
  1. package com.ithiema.factory;
  2. import com.ithiema.coffee.AmericanCoffee;
  3. import com.ithiema.coffee.Coffee;
  4. import com.ithiema.coffee.LatteCoffee;
  5. public class CoffeeFactory {
  6. public Coffee getCoffee(String type) {
  7. //声明Coffee类型的变量,根据不同类型创建不同的Coffee的子类对象
  8. //初始化
  9. Coffee coffee = null;
  10. if ("amerian".equals(type)) {
  11. coffee = new AmericanCoffee();
  12. } else if ("latte".equals(type)) {
  13. coffee = new LatteCoffee();
  14. } else {
  15. throw new RuntimeException("对不起,你所点咖啡没有");
  16. }
  17. //加配料
  18. coffee.addMilk();
  19. coffee.addSugar();
  20. return coffee;
  21. }
  22. }
  1. package com.ithiema;
  2. import com.ithiema.coffee.AmericanCoffee;
  3. import com.ithiema.coffee.Coffee;
  4. import com.ithiema.coffee.LatteCoffee;
  5. import com.ithiema.factory.CoffeeFactory;
  6. public class CoffeeStore {
  7. public Coffee orderCoffee(String type) {
  8. CoffeeFactory factory = new CoffeeFactory();
  9. Coffee coffee = factory.getCoffee(type);
  10. return coffee;
  11. }
  12. }
  1. package com.ithiema;
  2. import com.ithiema.coffee.Coffee;
  3. public class coffeeTest {
  4. public static void main(String[] args) {
  5. //1.创建咖啡店类
  6. CoffeeStore store = new CoffeeStore();
  7. //2.点咖啡
  8. Coffee coffee = store.orderCoffee("latte");
  9. System.out.println(coffee.getName());
  10. }
  11. }

4.手写一个简单的单例模式 代码

  1. package com.itheima;
  2. //静态成员变量模式的单例
  3. public class danli {
  4. private danli() {
  5. }
  6. //创建一个自己对象
  7. private static danli instance = new danli();
  8. //创建一个公共调用的方法
  9. public static danli getInstance() {
  10. return instance;
  11. }
  12. }
  1. package com.itheima;
  2. //静态成员变量模式的单例
  3. public class danli {
  4. private danli() {
  5. }
  6. //创建一个自己对象
  7. private static danli2 instance;
  8. //静态代码块赋值
  9. static {
  10. instance = new danli2();
  11. }
  12. //公共调用方法
  13. public static danli2 getInstance(){
  14. return instance;
  15. }
  16. }
  1. package com.itheima;
  2. //静态成员变量模式的单例
  3. public class danli {
  4. private danli() {
  5. }
  6. //创建一个自己对象
  7. private static danli2 instance;
  8. //公共调用方法,添加锁
  9. public static synchronized danli2 getInstance(){
  10. if(instance==null){
  11. instance = new danli2;
  12. }
  13. return instance;
  14. }
  15. }

5.手写一段简单的动态代理模式代码

  1. /**
  2. * 业务接口
  3. * @author ngcly
  4. * @version V1.0
  5. * @since 2021/8/20 12:30
  6. */
  7. public interface IUserService {
  8. void add(String msg);
  9. void add();
  10. String get();
  11. }
  1. /**
  2. * 业务接口实现类
  3. * @author ngcly
  4. * @version V1.0
  5. * @since 2021/8/20 12:30
  6. */
  7. public class UserService implements IUserService{
  8. @Override
  9. public void add(String msg) {
  10. System.out.println("=============保存============="+msg);
  11. }
  12. @Override
  13. public void add() {
  14. System.out.println("=============保存=============");
  15. }
  16. @Override
  17. public String get() {
  18. System.out.println("=============获取=============");
  19. return "捉到啦";
  20. }
  21. }
  1. /**
  2. * 静态代理
  3. * @author ngcly
  4. * @version V1.0
  5. * @since 2021/8/20 12:33
  6. */
  7. public class UserServiceProxy implements IUserService{
  8. /**代理目标类*/
  9. private IUserService target;
  10. public UserServiceProxy(IUserService userService){
  11. this.target = userService;
  12. }
  13. @Override
  14. public void add(String msg) {
  15. System.out.println("----------事务开始----------");
  16. target.add(msg);
  17. System.out.println("----------事务结束----------");
  18. }
  19. @Override
  20. public void add() {
  21. System.out.println("***********事务开始***********");
  22. target.add();
  23. System.out.println("***********事务结束***********");
  24. }
  25. @Override
  26. public String get() {
  27. System.out.println("############get事务开始############");
  28. String result = target.get();
  29. System.out.println("############get事务结束############");
  30. return result;
  31. }
  32. }
  1. public class Main {
  2. public static void main(String[] args){
  3. IUserService staticProxy = new UserServiceProxy(new UserService());
  4. staticProxy.add("静态代理");
  5. }
  6. }