7.SpringBoot常用注解

 

SpringBoot常用注解

相关注解说明:

  1. @Configuration: 作用于类上,相当于一个xml配置文件;
  2. @Bean: 作用于方法上,相当于xml配置中的<bean>
  3. @Import: 在创建配置文件之后可以引入其它的配置文件
  4. @ComponentScan("com.example.demo"): 扫描配置
  5. @Qualifier: 通过这个标识,表明了哪个实现类才是我们所需要的,需要注意的是@Qualifier的参数名称必须为之前定义@Bean注解的名称之一

Demo1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.demo.module.vo;

public class ProjectDataSource {

private String driverClassName;
private String url;
private String username;
private String password;

public ProjectDataSource() {
super();
}

public ProjectDataSource(String driverClassName, String url, String username, String password) {
super();
this.driverClassName = driverClassName;
this.url = url;
this.username = username;
this.password = password;
}

public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "ProjectDataSource [driverClassName=" + driverClassName + ", url=" + url + ", username=" + username
+ ", password=" + password + "]";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.demo.module.vo.ProjectDataSource;

@Configuration //application-a.xml
public class DataSourceDevConfig {

@Bean("dataSourceDev")
public ProjectDataSource getDataSourceDev() {
ProjectDataSource dataSourceDev = new ProjectDataSource();

dataSourceDev.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSourceDev.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
dataSourceDev.setUsername("root");
dataSourceDev.setPassword("123456");

return dataSourceDev;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.demo.module.vo.ProjectDataSource;

@Configuration
public class DataSourceProdConfig {

@Bean("dataSourceProd")
public ProjectDataSource getDataSourceProd() {
ProjectDataSource dataSourceProd = new ProjectDataSource();

dataSourceProd.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSourceProd.setUrl("jdbc:mysql://192.168.10.132:3306/ping?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
dataSourceProd.setUsername("root");
dataSourceProd.setPassword("dv*3s;Qdc]p");

return dataSourceProd;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.module.vo.ProjectDataSource;

@SpringBootTest
class JavaConfigApplicationTests {

@Autowired
private ProjectDataSource dataSourceDev;

@Autowired
@Qualifier("dataSourceProd")
private ProjectDataSource dataSource;

@Test
void contextLoads() {
}

@Test
public void testJavaConfig() {
System.out.println("dataSourceDev = " + dataSourceDev.toString());
System.out.println("dataSourceProd = " + dataSource.toString());

}

}
1
2
dataSourceDev = ProjectDataSource [driverClassName=com.mysql.cj.jdbc.Driver, url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC, username=root, password=123456]
dataSourceProd = ProjectDataSource [driverClassName=com.mysql.cj.jdbc.Driver, url=jdbc:mysql://192.168.10.132:3306/ping?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC, username=root, password=dv*3s;Qdc]p]

默认扫描启动类所在的包及其子包

方式1: 添加包扫描@ComponentScan,启动类中设置包扫描范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.config"})
public class JavaConfigApplication {

public static void main(String[] args) {
SpringApplication.run(JavaConfigApplication.class, args);
}

}

方式2: 通过导入配置文件@Import,主配置文件里导入其他配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.demo.module.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.example.config.DataSourceDevConfig;
import com.example.config.DataSourceProdConfig;

@Configuration
@Import(value = {DataSourceDevConfig.class, DataSourceProdConfig.class})//<import resource="classpath:application-dao.xml">
public class DataSourceConfigAll {

}