上一章
SpringBoot自学第1天——IDEA搭建一个SpringBoot的Maven项目
文章目录
前言
1. 自动注入的一些方式以及常用注解
2. 配置文件
3. 项目发布流程
提示:以下是本篇文章正文内容,下面案例可供参考
一、Bean的注入
创建相关文件
在po文件夹下创建一个Bean 类名为Animal
那一般情况下应该是用配置xml文件来注入bean,但是这里是用函数来做配置,这是一种更方便的方法吗?反正用起来会感觉还不错,毕竟函数比xml文件的配置看起来顺眼多了。
在Config文件夹下创建一个配置类 类名为MyConfig (用注解表明这是个配置类)
//MyConfig.java
@Configuration
public class MyConfig {
}
1. @Import
注解 利用Bean的无参构造函数来创建Bean对象
在配置类上加上注解@Import(Animal.class)
如下:
@Configuration
@Import(Animal.class)
public class MyConfig {
}
```
然后修改main函数如下:<font color=#999AAA >(主类里)
//MrzsApplication.java
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(MrzsApplication.class, args);
System.out.println("===========================\n");
String[] AnimalNames = run.getBeanNamesForType(Animal.class); //获取所有注册的Bean的名字
for (String name:AnimalNames){
System.out.println(name); //打印已有Bean的名字
}
}
运行结果:
Bean自动注入成功。
2. @Bean
注解 自定义方法来创建Bean对象
好处是自定义返回的对象,而不仅是的无参构造
代码如下:
//MyConfig.java
@Configuration
@Import(Animal.class)
public class MyConfig {
@Bean //放在函数上 自定义返回任意Animal对象
public Animal MyAnimal(){
return new Animal(); //return new Animal("dog");
}
}
运行结果:
3. @Component + @ConfigurationProperties
注解
利用配置文件注入
放在Animal类上方
@Component
@ConfigurationProperties(prefix = "animal")
并且在application.properties里面添加一行配置 animal.type = dog
@ConfigurationProperties
注解的作用类似于XML配置文件给注入的Bean赋值,达到注入参数的效果。创建的对象名默认为prefix的值
运行结果:
4. @EnableConfigurationProperties+ @ConfigurationProperties
与上面的第3种一样,就是将@EnableConfigurationProperties
写在了MyConfig上,从而代替掉了@Component
这种没看懂是什么作用,以后再看用在什么场景。
5. 总结注解
1. @Import
放在配置类上 默认利用无参构造注入 方便
2. @Bean
放在配置类里的方法上,自定义方法来配置和创建Bean对象
3. @Component + @ConfigurationProperties
放在在Bean类上方 是结合.properties
文件来注入对象属性
6. 使用注入后的Bean
添加一个controller的映射页/animal
@Controller
public class MyController {
@Autowired //自动注入注解
private Animal animal;
@ResponseBody
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "mrzs") String name) {
return String.format("Hello %s!", name);
}
@ResponseBody //返回数据而非视图 并自动将对象转化为json返回
@GetMapping("/animal")
public Animal animal() {
return animal;
}
}
运行程序:访问:http://localhost:8080/animal
网页返回了一个json,那我们发现这个自动注入的Bean注入的是@Component + @ConfigurationProperties
注解所创建的带有dog参数的对象,而不是用@Bean
注解创建的自定义的无参对象,这里可以看出上面多个注解同时使用时,他们的优先级顺序。
二、发布项目
1. 打包项目
打包成功提示有个 √:
2. 找到打包好的项目文件夹
进入target目录,找到下面的一个jar包
在该目录按住shift并点击右键,找到powershell终端窗口。
3. 输入java -jar .\mrzs-0.0.1-SNAPSHOT.jar
来运行打包好的项目
可以看到,IDEA里面的项目没有在运行:
但是我们在网址上输入localhost:8080/hello
访问成功,项目打包以及在终端运行成功。
总结
主要是学习了一些注解。 如:@Configuration
注解,放在类上,表示这个类是一个配置类,可以配置一些自动注入的东西,以后肯定还有许多配置需要在这里写。@Import
注解,利用无参构造注入Bean对象@Bean
注解,自定义函数创建Bean对象@Component + @ConfigurationProperties(prefix="animal")
注解,结合配置文件创建Bean对象,这样有利于Bean类中属性的初始值的统一管理。项目发布
主要是将项目打包成了一个jar包,直接用java运行该jar包就可以运行整个项目。(jar包内自带了tomcat依赖,springboot相当于封装了一下打包流程)
@Import与@Bean
同时生效时,在Controller中使用@Autowired自动注入Bean时会提示报错
@Component+@ConfigurationProperties(prefix = "animal")
与@Bean
同时生效则没这个问题
@Component+@ConfigurationProperties(prefix = "animal")
与@Impoert
同时使用时,只有前者生效
//MyController中错误的animal请求,需要请求一个Bean,但是容器中找到了两个。
Field animal in com.mrzs.controller.MyController required a single bean, but 2 were found:
- com.mrzs.bean.Animal: defined in null
- Animal: defined by method 'animal' in class path resource [com/mrzs/config/MyConfig.class]
Action:
//IDEA推荐的解决方案: 考虑将Bean上添加@Primary,更新用户接收多个beans 或 @Qualifier来标识要使用的Bean
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans,
or using @Qualifier to identify the bean that should be consumed
写文章好浪费时间,我寻思着还是多看一些再一次写出来吧,毕竟有些东西说不定当时理解是错误的,晚一些写可以提高容错率。而且有些东西语言根本没法描述…
还有,这一章无聊的注解们很劝退。现在可以只做了解,知道有这么几个东西,到后面越用就会越熟练的。