@Bean给容器添加组件
- 新建Maven工程:hello。
- 修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
- 新建类com.example.boot.bean.User。
package com.example.boot.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
private String name;
private Integer age;
}
- 新建配置类com.example.boot.config.MyConfig。
package com.example.boot.config;
import com.example.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public User user01(){
return new User("zhangsan",22);
}
}
- 新建主程序类com.example.boot.MainApplication。
package com.example.boot;
import com.example.boot.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
String[] beanNamesForType = run.getBeanNamesForType(User.class);
for (String s : beanNamesForType) {
System.out.println(s);
}
}
}
- 启动应用,查看容器中的组件。
当前只注册了user01
这个组件到容器中。
@Import给容器添加组件
在配置类MyConfig上添加@Import({User.class, DBHelper.class})
,添加User类组件、DBHelper组件,如下。
package com.example.boot.config;
import ch.qos.logback.core.db.DBHelper;
import com.example.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({User.class, DBHelper.class})
public class MyConfig {
@Bean
public User user01(){
return new User("zhangsan",22);
}
}
package com.example.boot;
import ch.qos.logback.core.db.DBHelper;
import com.example.boot.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
String[] beanNamesForType = run.getBeanNamesForType(User.class);
for (String s : beanNamesForType) {
System.out.println(s);
}
String[] beanNamesForType1 = run.getBeanNamesForType(DBHelper.class);
for (String s : beanNamesForType1) {
System.out.println(s);
}
}
}
重启应用,查看容器中的组件,控制器中打印出如下信息:
com.example.boot.bean.User
user01
ch.qos.logback.core.db.DBHelper
其中,user01,是通过@Bean往容器中添加的组件。
com.example.boot.bean.user和ch.qos.logback.core.db.DBHelper,是通过@Import往容器中添加的组件。