spring bean注解

Spring的bean注解

这4个注解都能把类扫描成bean

@Service
@Configuration
@Controller
@Component

分析

分析前三个注解的实现
发现实现都是@Component的实现

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
//都引入了此注解
@Component
public @interface Controller {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	@AliasFor(annotation = Component.class)
	String value() default "";

}

@Component的实现

ClassPathBeanDefinitionScanner 类

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, @Nullable ResourceLoader resourceLoader) {

		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		this.registry = registry;

		if (useDefaultFilters) {
		    //注意这个方法
			registerDefaultFilters();
		}
		setEnvironment(environment);
		setResourceLoader(resourceLoader);
	}

实现
filter的实现了对@Component的扫描,对这里不清楚的可以看下这里
spring bean 动态注入

protected void registerDefaultFilters() {
		this.includeFilters.add(new AnnotationTypeFilter(Component.class));
		ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
			logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
		}
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
			logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
的依赖注入,你会如何实现? 作为一个CSDN开发的ChitGPT,我会建议您使用Spring的依赖注入来实现显示器、键盘和鼠标三个类的注入。 首先,您需要添加注入注解(如@Autowired)到显示器、键盘和鼠标的实现类上,以便Spring能够识别它们作为Bean并将其注入到相关的类中。 例如,您可以在以下类中添加注入注解: 1. 显示器:Display.java ``` @Component // 声明为SpringBean public class Display { // 一些属性和方法 } ``` 2. 键盘:Keyboard.java ``` @Component // 声明为SpringBean public class Keyboard { // 一些属性和方法 } ``` 3. 鼠标:Mouse.java ``` @Component // 声明为SpringBean public class Mouse { // 一些属性和方法 } ``` 然后,在需要使用这三个类的类中,使用构造函数注入或属性注入将其注入。 例如,您可以在以下类中注入这三个类: ``` @Service // 声明为Spring的Service public class Computer { // 使用构造函数注入 private final Display display; private final Keyboard keyboard; private final Mouse mouse; public Computer(Display display, Keyboard keyboard, Mouse mouse) { this.display = display; this.keyboard = keyboard; this.mouse = mouse; } // 一些使用这三个类的方法 } ``` 或者,您也可以使用属性注入: ``` @Service // 声明为Spring的Service public class Computer { // 使用属性注入 @Autowired private Display display; @Autowired private Keyboard keyboard; @Autowired private Mouse mouse; // 一些使用这三个类的方法 } ``` 这样,当Spring需要实例化Computer类时,它将自动实例化并注入Display、Keyboard和Mouse类的实例,从而使您能够轻松地使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wending-Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值