以下是详细讲解“Spring Boot注册Bean的三种方法”的攻略。
简介
在Spring Boot应用程序中,可以使用三种方法注册Bean:
@ComponentScan
+@Component
注册:使用注解扫描机制,标记bean组件并创建自动扫描Spring Boot应用程序中的bean。可以在类上使用@Component
、@Service
、@Repository
、@Controller
等注解标记bean。这些注解被统称为@Component
注解。@ComponentScan
注解则是指定扫描的包路径。@Bean
注册:使用@Bean
注解来指定bean的创建逻辑,可以在任意配置类上使用该注解来定义bean。@Configuration
+@Import
注册:创建配置类,并使用@Import
注解将配置类注入到Spring Boot应用程序中。该注解同时会激活配置类中标注了@Bean
注解的bean的创建逻辑。
详细步骤
1. @ComponentScan
+ @Component
注册
步骤1:创建一个标记为@Component
的bean组件
package com.example.demo.bean;
import org.springframework.stereotype.Component;
@Component
public class UserService {
public void printName() {
System.out.println("Hello, I'm a user!");
}
}
步骤2:创建一个配置类并扫描bean组件
package com.example.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example.demo.bean")
public class AppConfig {
}
步骤3:启动Spring Boot应用程序并测试
package com.example.demo;
import com.example.demo.config.AppConfig;
import com.example.demo.bean.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
UserService userService = context.getBean(UserService.class);
userService.printName();
}
}
2. @Bean
注册
步骤1:创建一个@Bean实例化的bean组件
package com.example.demo.bean;
public class Student {
private String name;
public void printName() {
System.out.println("Hello, My name is " + name);
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo.config;
import com.example.demo.bean.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public Student student() {
Student student = new Student();
student.setName("Tom");
return student;
}
}
步骤2:启动Spring Boot应用程序并测试
package com.example.demo;
import com.example.demo.config.AppConfig;
import com.example.demo.bean.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
Student student = context.getBean(Student.class);
student.printName();
}
}
3. @Configuration
+ @Import
注册
步骤1:创建需要注入的Bean组件
package com.example.demo.bean;
public class Course {
private String name;
public void printName() {
System.out.println("Hello, My course name is " + name);
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo.config;
import com.example.demo.bean.Course;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CourseConfig {
@Bean
public Course java() {
Course java = new Course();
java.setName("Java");
return java;
}
}
步骤2:在主配置类中使用@Import
注解注入CourseConfig配置类
package com.example.demo.config;
import com.example.demo.bean.Student;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({CourseConfig.class})
public class AppConfig {
@Bean
public Student student() {
Student student = new Student();
student.setName("Tom");
return student;
}
}
步骤3:启动Spring Boot应用程序并测试
package com.example.demo;
import com.example.demo.config.AppConfig;
import com.example.demo.bean.Student;
import com.example.demo.bean.Course;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
Course course = context.getBean(Course.class);
course.printName();
Student student = context.getBean(Student.class);
student.printName();
}
}
以上就是Spring Boot注册Bean的三种方法的完整攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springboot注册bean的三种方法 - Python技术站