Springboot2
参考:https: www yuque com leifengyang springboot2
参考:https://www.yuque.com/leifengyang/springboot2
参考:https://www.bilibili.com/video/BV19K4y1L7MT?p=1&vd_source=0c3c1f43c75954a15fba4e42c1d7883e
(资料图片)
项目结构
1. pom.xml
4.0.0 com.example springboot-test 1.0-SNAPSHOT pom springboot-01-hello org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE org.springframework.boot spring-boot-starter-web 8 8 UTF-8 org.springframework.boot spring-boot-maven-plugin
2. MainApplication
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 主程序类 * @SpringBootApplication:这是一个springboot应用 */@SpringBootApplicationpublic class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); }}
3. HelloController
package com.example.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @RequestMapping("/hello") public String home() { return "Hello world"; }}
4. 启动服务,浏览器访问
org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE 点进spring-boot-starter-parent看到: org.springframework.boot spring-boot-dependencies 2.3.4.RELEASE 点进spring-boot-dependencies看到: 几乎声明了所有开发中常用的依赖的版本号
org.springframework.boot spring-boot-starter 2.3.4.RELEASE compile
5.1.43
org.springframework.boot spring-boot-starter-tomcat 2.3.4.RELEASE compile
配置Tomcat ???SpringBoot帮我们配置好了所有web开发的常见场景
dispatcherServlet字符编码:characterEncodingFilter文件上传:multipartResolver@SpringBootApplication等同于@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan("com.example")
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.ComponentScan;/**@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan("com.example")等价于@SpringBootApplication(scanBasePackages="com.example") 默认扫描主程序所在的包*/@SpringBootApplicationpublic class MainApplication { 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); } }}
package com.example.bean;public class User { private String name; private int age; private Pet pet; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } @Override public String toString() { return "User{" + "name="" + name + "\"" + ", age=" + age + ", pet=" + pet + "}"; }}
Petpackage com.example.bean;public class Pet { private String name; public Pet() { } public Pet(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name="" + name + "\"" + "}"; }}
MainConfigpackage com.example.config;import com.example.bean.Pet;import com.example.bean.User;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration(proxyBeanMethods=true) // 告诉springboot这是一个配置类public class MainConfig { @Bean public User userBean(){ User u = new User("zhangsan", 18); u.setPet(petBean()); return u; } @Bean public Pet petBean(){ Pet p = new Pet("cat"); return p; }}
MainApplicationpackage com.example;import com.example.bean.Pet;import com.example.bean.User;import com.example.config.MainConfig;import org.springframework.boot.SpringApplication;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.ComponentScan;/**@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan("com.example")等价于@SpringBootApplication(scanBasePackages="com.example") 默认扫描主程序所在的包*/@SpringBootApplicationpublic class MainApplication { 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); } // 3. 从容器中获取组件 User user = (User) run.getBean("userBean"); User user2 = (User) run.getBean("userBean"); System.out.println("组件默认是单例的:" + (user==user2)); // 4. 配置类也是组件 MainConfig bean = run.getBean(MainConfig.class); System.out.println("配置类本身也是组件:" + bean); // 5. proxyBeanMethods = true, mainConfig就是被增强的代理对象 // springboot总会检查组件是否在容器中存在 // 保持组件单实例 // 外部无论对配置类中的这个组件注册方法调用多少次,获取的都是之前注册容器中的单实例对象 User user3 = bean.userBean(); User user4 = bean.userBean(); System.out.println("主配置是否为代理对象: "+(user3==user4)); // full: proxyBeanMethods=true => 应用场景: 解决组件依赖 // lite: proxyBeanMethods=false => 配置类在容器中不会保存代理对象,在外边调用方法,每次都会产生一个新对象 // Full: 保证每个@Bean方法被调用多少次返回的组件都是单实例的。类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式 // Lite: 每个@Bean方法被调用多少次返回的组件都是新创建的。类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断 // 组件依赖必须使用Full模式默认。其他默认是否Lite模式 Pet pet = (Pet) run.getBean("petBean"); System.out.println("proxyBeanMethods组件依赖" + (user.getPet()==pet)); }}
package com.example.config;import com.example.bean.Pet;import com.example.bean.User;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MainConfig {}
Userpackage com.example.bean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;import javax.annotation.Resource;@Component("userBean")public class User { private String name; private int age; @Autowired private Pet pet; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } @Override public String toString() { return "User{" + "name="" + name + "\"" + ", age=" + age + ", pet=" + pet + "}"; }}
Petpackage com.example.bean;import org.springframework.stereotype.Component;@Component("petBean")public class Pet { private String name; public Pet() { } public Pet(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name="" + name + "\"" + "}"; }}
MainApplicationpackage com.example;import com.example.bean.Pet;import com.example.bean.User;import com.example.config.MainConfig;import org.springframework.boot.SpringApplication;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.ComponentScan;/** * @SpringBootConfiguration * @EnableAutoConfiguration * @ComponentScan("com.example") 等价于@SpringBootApplication(scanBasePackages="com.example") * 默认扫描主程序所在的包 */@SpringBootApplicationpublic class MainApplication { 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); } // 3. 从容器中获取组件 User user = (User) run.getBean("userBean"); User user2 = (User) run.getBean("userBean"); System.out.println("组件默认是单例的:" + (user == user2)); // 4. 配置类也是组件 MainConfig bean = run.getBean(MainConfig.class); System.out.println("配置类本身也是组件:" + bean); // 5. 组件依赖 Pet pet = (Pet) run.getBean("petBean"); System.out.println("组件依赖" + (user.getPet() == pet)); }}
@Import:给容器中自动创建出指定类型的组件、默认组件的名字就是全类名
@Conditional:条件装配,满足Conditional指定的条件,则进行组件注入
@ImportResource:原生配置文件引入
@ConfigurationProperties:配置文件中的配置项绑定给组件的属性
项目结构MainApplicationpackage com.example;import ch.qos.logback.core.db.DBHelper;import com.example.bean.Pet;import com.example.bean.User;import com.example.config.MainConfig;import org.springframework.boot.SpringApplication;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.ComponentScan;import java.util.Arrays;/** @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.example") 等价于@SpringBootApplication(scanBasePackages="com.example") 默认扫描主程序所在的包 *//** * @Import:给容器中自动创建出指定类型的组件、默认组件的名字就是全类名 * @Conditional:条件装配,满足Conditional指定的条件,则进行组件注入 * @ImportResource:原生配置文件引入 * @ConfigurationProperties:配置文件中的配置项绑定给组件的属性 */@SpringBootApplicationpublic class MainApplication { 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); } // 3. 获取组件 Boolean flag = run.containsBean("petBean"); System.out.println("容器中是否存在petBean组件:" + flag); Boolean flag2 = run.containsBean("userBean"); System.out.println("容器中是否存在userBean组件:" + flag2); // 4. 容器中组件数量 System.out.println("组件数量:" + run.getBeanDefinitionCount()); // 167 }}
mainApplication.xml
Userpackage com.example.bean;public class User { private String name; private int age; private Pet pet; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } @Override public String toString() { return "User{" + "name="" + name + "\"" + ", age=" + age + ", pet=" + pet + "}"; }}
Petpackage com.example.bean;public class Pet { private String name; public Pet() { } public Pet(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "name="" + name + "\"" + "}"; }}
Carpackage com.example.bean;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;// @Component // MainConfig中使用了@EnableConfigurationProperties(Car.class),则不用@Component@ConfigurationProperties(prefix = "car")public class Car { private String brand; private double price; public Car() { } public Car(String brand, double price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Car{" + "brand="" + brand + "\"" + ", price=" + price + "}"; }}
MainConfigpackage com.example.config;import ch.qos.logback.core.db.DBHelper;import com.example.bean.Car;import com.example.bean.Pet;import com.example.bean.User;import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.*;// @ConditionalOnBean(name="petBean") // 有petBean类里的组件注册才生效,否则都不生效@EnableConfigurationProperties(Car.class)// @EnableConfigurationProperties(Car.class)的作用:// 1. 开启Car配置绑定功能// 2. 把这个Car这个组件自动注册到容器中@ImportResource("classpath:mainApplication.xml")@Import({User.class, DBHelper.class})@Configuration(proxyBeanMethods=true) // 告诉springboot这是一个配置类public class MainConfig { @ConditionalOnBean(name="petBean") // 容器中存在petBean才执行userBean的组件注册 @Bean public User userBean(){ User u = new User("zhangsan", 18); u.setPet(petBean()); return u; } // @Bean public Pet petBean(){ Pet p = new Pet("cat"); return p; }}
@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})})public @interface SpringBootApplication {}
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Configuration // SpringBootConfiguration用来注解 配置类 => MainApplication是一个配置类(核心配置类)public @interface SpringBootConfiguration { @AliasFor( annotation = Configuration.class ) boolean proxyBeanMethods() default true;}
@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage@Import({AutoConfigurationPackages.Registrar.class}) // 给容器中导入一个组件// 指定了默认的包规则public @interface AutoConfigurationPackage {}// 利用Registrar给容器中导入一系列组件// 将指定的MainApplication所在包下的所有组件导入进来
@Import({AutoConfigurationImportSelector.class})1. 利用getAutoConfigurationEntry(annotationMetadata)给容器中批量导入一些组件2. 调用List configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类3. 利用工厂加载 Map> loadSpringFactories(@Nullable ClassLoader classLoader)得到所有的组件4. 从META-INF/spring.factories位置来加载一个文件。 默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件 spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories 文件里面写死了spring-boot一启动就要给容器中加载的所有配置类
1. 虽然我们127个场景的所有自动配置启动的时候默认全部加载。 META-INF/spring.factories文件中:xxxxAutoConfiguration2. 按照条件装配规则(@Conditional),最终会按需配置。 eg. @ConditionalOnClass({Gson.class})
关键词:
[ 相关文章 ]
参考:https: www yuque com leifengyang springboot2
张家楼“LOGO&吉祥物”投票开始!快来见证大奖归属
为贯彻落实自治区领导批示要求,进一步抓好秸秆综合利用工作,推动秸秆综合利用产业提质增效,4月11日,自治区农业农村厅党组成员、总农艺师李
汽车现在已经越来越普及,基本上都快实现每家每户都有汽车了,那么汽车这么多的情况之下,我们在用车的过程当中肯定也就会遇到各
农业农村部最新农情调度显示,截至昨天(11日),全国已春播粮食面积1 2亿亩,完成意向面积的12 6%,进度同比相当。目前,早稻育秧基本完成,播
1、个人测试过影音先锋和迅雷影音,前者播放1080p电影会有卡顿情况,使用迅雷影音则播放流畅,推荐用迅雷影音,和暴风影音
比亚迪日前正式公布旗下“云辇”系统将于4月10日正式发布。至于是一项什么技术,硬核工作站的小伙伴们进行了一系列的大胆猜想。不知道您认可哪
非牟利机构最新调查发现,全美1 5民众表示,家族中曾有人死于自杀在内的枪击案,相同比例的受访者表示,自己也曾受到枪支威胁。数据反映随着武
》》》五一天津限行政策(尾号限行+早晚高峰)尾号限行政策:根据天津市机动车限行交通管理措施规定,法定节假日期间,本市及外埠号牌机动车和
海口南港码头轮渡时间表2023年4月1日零时起,海南铁路轮渡南、北港实行新的航班时刻,计划每天开行航班16对,如下图所示。南港票价一、客运票
一、交通事故断了九根肋骨算几级伤残交通事故断了九根肋骨算九级伤残。找法网提醒您,需要赔付项目有:医疗费、住院伙食补助、交
此次左江流域考古发现的蛇类骨骼。广西文物保护与考古研究所供图 近日,中国科学院等机构的科研人员对广西左江流域距今约6000年的宝剑山遗址
人社部近日印发通知,实施2023年高校毕业生等青年就业创业推进计划,明确十项具体行动。和教育小微一图读懂——来源|中国劳动保障报微信
各有关考生:为方便注册会计师全国统一考试考生就近参加考试,进一步做好考生服务工作,经财政部考办批准,我区专业阶段考试在原有南宁、桂林
近日,河南周口警方抓获一名男性女主播小偷李某。他晚上穿戴假发假胸男扮女装做直播,凌晨三点又翻墙进入小区拉车门盗窃现金5000余元。目前,
霞浦县气象台发布雷电黄色预警【Ⅲ级 较重】
26家定点医药机构今年底前实现移动支付杭州日报讯为更好地满足参保人员对互联网诊疗、互联网购药和医保移动支付服务的需求,昨日,市医疗保障
4月10日早上7时48分,一架波音757全货机从温州起飞。当天,机上搭载的25吨眼镜、服装等商品就可以配送至日本大阪的消费者手中。同一架飞机返航
1、中华美德颂演讲稿各位老师,同学们,大家好!少年兴则国兴,少年强则国强。2、我们要努力学习,努力报国,勿忘国耻,振兴我
4月11日,中洲控股发布公司2023年度日常关联交易的公告。前次日常关联交易的预计和执行情况:关联交易类别关联人关联交易内容实际发生金额(万
[ 相关新闻 ]
Copyright 2015-2022 人人医院网 版权所有 备案号:粤ICP备18023326号-36 联系邮箱:8557298@qq.com