提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、依赖管理
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
二、自动配置(按需加载)
SpringBoot帮我们配置好了所有web,tomcat,SpringMVC开发的常见场景
默认扫描路径是主程序所在包
想要改变扫描路径
想要改变扫描路径
@SpringBootApplication(scanBasePackages=“com.lun”)
@ComponentScan 指定扫描路径
@SpringBootApplication
public class MainApplication {
public static void main(String[] args){
ConfigurableApplicationContext run=SpringApplication.run(MainApplication.class, args);
String [] names=run.getBeanDefinitionNames();
for (String name:names){
System.out.println(name);
}
}
}
三、注册组件
1.@Configuration详解
@Configuration(proxyBeanMethods = true)//配置类
public class MyConfig {
@Bean给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatpet());
return zhangsan;
}
@Bean("tom")
public Pet tomcatpet(){
return new Pet("tom");
}
}
配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
@SpringBootApplication(proxyBeanMethods =false)
配置 类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式(默认)
@SpringBootApplication(proxyBeanMethods = true)
@SpringBootApplication(proxyBeanMethods = true)
public class MainApplication {
public static void main(String[] args){
ConfigurableApplicationContext run=SpringApplication.run(MainApplication.class, args);
String [] names=run.getBeanDefinitionNames();
for (String name:names){
System.out.println(name);
}
//从容器中获取组件
Pet tom01=run.getBean("tom",Pet.class);
Pet tom02 = run.getBean("tom", Pet.class);
System.out.println("组件:"+(tom01 == tom02));
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);
//外部无论对配置类中这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
//如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
//保持组件单实例
User user = bean.user01();
User user1 = bean.user01();
System.out.println(user == user1);
User user01 = run.getBean("user01", User.class);
Pet tom = run.getBean("tom", Pet.class);
System.out.println("用户的宠物:"+(user01.getPet() == tom));
}
}
2.@Import详解
@Import({User.class, DBHelper.class})
//获取组件
String[] beanNamesForType = run.getBeanNamesForType(User.class);
for (String s : beanNamesForType) {
System.out.println(s);
}
DBHelper bean1 = run.getBean(DBHelper.class);
System.out.println(bean1);
3.@Conditional条件装配
@ConditionalOnMissingBean(name = “tom”)//没有tom名字的Bean时,MyConfig类的Bean才能生效
boolean tom22 = run.containsBean("tom");
System.out.println("容器中tom22组件:"+tom);//true
4.@ImportResource导入资源
若使用bean.xml文件生成配置bean,你为了省事,想继续复用bean.xml,则使用@ImportResource
ImportResource(“classpath:beans.xml”)
5.@ConfigurationProperties配置绑定
使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用
//组件必须在容器中
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
...
}
在配置文件中配置
mycar.brand=BYD
mycar.price=100000
@Autowired
car c;
@RequestMapping("/car")
public com.atguigu.boot.bean.car c(){
return c;
}
另一种方法:
@EnableConfigurationProperties(Car.class)
public class MyConfig {
...
}
开启Car配置绑定功能
把这个Car这个组件自动注册到容器中