SpringBoot常用注解
SpringBoot摒弃了xml配置方式,改为了全注解驱动。
准备
创建SpringBoot模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.louis</groupId><artifactId>boot3-03</artifactId><version>0.0.1-SNAPSHOT</version><name>boot3-03</name><description>boot3-03</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.18</version></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
1、组件注册
@Configuration、@SpringBootConfiguration
@Bean
@Controller、@Service、@Repository、@Component
@Import
@ComponentScan
组件注册注解使用
场景:使用Bean的方式给容器中放组件
1、使用@Configuration编写一个配置类
2、在配置类中,自定义方法给容器中注册组件。配合@Bean或使用@Import导入第三方组件
创建实体类User
package com.louis.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private String name;private Integer age;private String pwd;
}
创建一个配置类UserConfig
package com.louis.config;import com.louis.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 组件默认是单实例的,即获取两次之后的值都是相等的,如果想要设置多实例,可以使用注解@Scope*/
@Configuration//这是一个配置类,代替以前的配置文件,配置类本身那个也是容器中的组件
public class UserConfig {@Bean//替代以前配置文件中的bean标签,组件在容器中的名字默认是方法名,可以直接修改注解的值。@Bean("haha")public User testUser(){User user = new User();user.setName("louie");user.setAge(23);user.setPwd("1111");return user;}
}
也可以使用注解
@SpringBootConfiguration
,它和@Configuration注解的作用是一样的,@Configuration代表普通配置类,@SpringBootConfiguration代表springBoot的配置类。
测试在ioc容器中是否有相关组件
package com.louis;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Boot303Application {public static void main(String[] args) {var ioc = SpringApplication.run(Boot303Application.class, args);String[] names = ioc.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}}
}
除了@Bean(组件的名字是方法名)在ioc容器中放入组件外,还可以使用@Import(组件的名字是全类名)注解导入第三方组件。
2、条件注解
@ConditionalOnXxx
功能:如果指定的条件成立,则触发指定行为。
如:
条件注解 | 功能 |
---|---|
@ConditionalOnClass | 如果类路径中存在这个类,则触发指定行为。 |
@ConditionalOnMissingClass | 如果类路径中不存在这个类,则出发指定行为。 |
@ConditionalOnBean | 如果容器中存在这个Bean(组件),则触发指定的行为。 |
@ConditionalOnMissingBean | 如果容器中不存在这个Bean(组件),则触发指定行为。 |
场景:
1、如果存在FastsqlException这个类,给容器中放一个Cat组件,名为cat01;否则就给容器中放一个dog组件,名为dog01
2、 如果系统中有dog01这个组件,就给容器中放一个User组件,名为zhangsan,否则就放一个User,名叫lisi
条件注解使用
创建实体类Dog
package com.louis.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {private String name;private String age;
}
创建实体类Cat
package com.louis.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Cat {private String name;private String color;
}
创建配置类Appconfig
package com.louis.config;import com.louis.bean.Cat;
import com.louis.bean.Dog;
import com.louis.bean.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;@SpringBootConfiguration
public class AppConfig {@ConditionalOnClass(name="com.alibaba.druid.FastsqlException")@Beanpublic Cat cat01(){return new Cat();}@ConditionalOnMissingClass(value = "com.alibaba.druid.FastsqlException")@Beanpublic Dog dog01(){return new Dog();}@ConditionalOnBean(value = Dog.class)@Beanpublic User zhangsan(){return new User();}@ConditionalOnMissingBean(value = Dog.class)@Beanpublic User lisi(){return new User();}}
测试
import com.louis.bean.User;
import com.louis.bean.Cat;
import com.louis.bean.Dog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Boot303Application {public static void main(String[] args) {var ioc = SpringApplication.run(Boot303Application.class, args);for (String s : ioc.getBeanNamesForType(Cat.class)) {System.out.println("cat = " + s);}for (String s : ioc.getBeanNamesForType(Dog.class)) {System.out.println("dog = " + s);}for (String s : ioc.getBeanNamesForType(User.class)) {System.out.println("user = " + s);}}}
放在类级别,如果注解判断生效,则整个配置类才生效,放在方法级别,判断生效则这个配置方法生效。
3、属性绑定
注解 | 功能 |
---|---|
@ConfigurationProperties | 声明组件的属性和配置文件哪些前缀开始项进行绑定 |
@EnableConfigurationProperties | 快速注册注解,一般用于第三方写好的组件进行属性绑定(springBoot默认只扫描主程序所在包及其子包) |
将容器中任意组件(Bean)的属性值给配置文件的配置项的值进行绑定
属性绑定使用
实例1:
@ConfigurationProperties+@Component
注意:
1、需要先放在容器中再使用@ConfigrationProperties才会有用
2、使用@ConfigurationProperties声明组件和配置文件的那些配置项进行绑定
创建实体类Pig
创建实体类Pig
package com.louis.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pig {private Integer id;private String name;
}
在配置文件application.properties中添加配置
pig.id=11
pig.name=GGBond
获取配置中的值
Pig类注册到容器中,并使用@ConfigurationProperties绑定值。
package com.louis.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "pig")//prefix表示前缀,将前缀是pig的值绑定
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pig {private Integer id;private String name;
}
测试
package com.louis;import com.louis.bean.Pig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Boot303Application {public static void main(String[] args) {var ioc = SpringApplication.run(Boot303Application.class, args);Pig bean = ioc.getBean(Pig.class);System.out.println("pig = " + bean);}}
//pig = Pig{id=11, name='GGBond'}
如果使用的中文出现了乱码情况,可以在settings中搜索file encodings, 设置global、project Encoding,以及下面的default Encoding…为UTF-8,并勾选Transparent…的选框。
实例2:
@ConfigurationProperties + @EnableConfigurationProperties
EnableConfigurationProperties有两个作用:
1、开启属性绑定2、默认会将这个组件放在容器中
创建实体类Sheep
package com.louis.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;/*** @author XRY* @date 2023年07月10日15:37*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@ConfigurationProperties(prefix = "sheep")
public class Sheep {private Long id;private String name;private Integer age;
}
在application.properties中添加内容
sheep.id=1
sheep.name=美羊羊
sheep.age=2
测试
package com.louis;import com.louis.bean.Person;
import com.louis.bean.Pig;
import com.louis.bean.Sheep;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;@SpringBootApplication
@EnableConfigurationProperties(Sheep.class)
//EnableConfigurationProperties有两个作用,1、开启sheep的属性绑定2、默认会将这个组件放在容器中
public class Boot303Application {public static void main(String[] args) {var ioc = SpringApplication.run(Boot303Application.class, args);Sheep sheep = ioc.getBean(Sheep.class);System.out.println("sheep = " + sheep);}}
//sheep = Sheep(id=1, name=美羊羊, age=2)