@Component @Bean @Configuration 用法

@Component @Bean @Configuration 用法,第1张

@Component @Bean @Configuration 用法

@Component @Bean @Configuration 用法
  • @Component
    • 官方定义
    • 作用
      • 其他很多注解里面都包含有@Component注解
    • 示例
      • 组件类
      • 注入@Component类的实例
        • 说明
  • @Bean
    • 官方定义
    • 示例
      • Moon类
      • @Configuration类
      • 注入moon实例
        • 说明
  • @Configuration
    • 官方定义

@Component 官方定义

Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

野鸡翻译:
在spring中带有@Component 注解的类表名该类是一个组件类。spring采用基于注解和类路径扫描的方式时,这些组件类就会被自动检测到。

作用

Sping扫描到了@Component类之后就会将其纳入spring容器进行管理,方便在其他地方使用,如:将@Component类的实例直接注入到另外一个类里。等

其他很多注解里面都包含有@Component注解



示例

将@Component类的实例直接注入到另外一个类里

组件类
package com.xl.test.springtest.pojo;

import org.springframework.stereotype.Component;

@Component
public class Moon {
	
	private String fullLevel;
	
	private String brightLevel;

	public String getFullLevel() {
		return fullLevel;
	}

	public void setFullLevel(String fullLevel) {
		this.fullLevel = fullLevel;
	}

	public String getBrightLevel() {
		return brightLevel;
	}

	public void setBrightLevel(String brightLevel) {
		this.brightLevel = brightLevel;
	}
}
注入@Component类的实例
package com.xl.test.springtest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xl.test.springtest.pojo.Moon;
import com.xl.test.springtest.test.InvocationInjection;

@RestController
public class ConfigurationController {
	
	@Autowired
	Moon moon;
	
	@GetMapping("/configTest")
	public String configTest() {
		System.out.println(moon.getBrightLevel());
		return null;
	}
	
	@RequestMapping("/inject")
	public String InjectionTest() {
		InvocationInjection ii = new InvocationInjection();
		String result = ii.test();
		return result;
	}
	
}

说明


@Bean 官方定义

Indicates that a method produces a bean to be managed by the Spring container.

野鸡翻译:
@Bean用于注解一个方法表示该方法将会产生一个bean,该bean会被spring容器管理。

@Bean Methods in @Configuration Classes
Typically, @Bean methods are declared within @Configuration classes. In this case, bean methods may reference other @Bean methods in the same class by calling them directly. This ensures that references between beans are strongly typed and navigable. Such so-called 'inter-bean references' are guaranteed to respect scoping and AOP semantics, just like getBean() lookups would. These are the semantics known from the original 'Spring JavaConfig' project which require CGLIB subclassing of each such configuration class at runtime. As a consequence, @Configuration classes and their factory methods must not be marked as final or private in this mode. For example:
@Configuration
 public class AppConfig {

     @Bean
     public FooService fooService() {
         return new FooService(fooRepository());
     }

     @Bean
     public FooRepository fooRepository() {
         return new JdbcFooRepository(dataSource());
     }

     // ...
 }

野鸡翻译:
一般来说,@Bean 注解的方法都是声明在@Configuration注解的类里面的。

示例

注入一个Moon实例:此时可以不用在Moon类上添加@Component注解。直接在@Configuration类上使用@Bean方法来标识一个Moon实例即可。

Moon类
package com.xl.test.springtest.pojo;

import org.springframework.stereotype.Component;

public class Moon {
	
	private String fullLevel;
	
	private String brightLevel;

	public String getFullLevel() {
		return fullLevel;
	}

	public void setFullLevel(String fullLevel) {
		this.fullLevel = fullLevel;
	}

	public String getBrightLevel() {
		return brightLevel;
	}

	public void setBrightLevel(String brightLevel) {
		this.brightLevel = brightLevel;
	}
}
@Configuration类
package com.xl.test.springtest.configclass;

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

import com.xl.test.springtest.pojo.Couple;
import com.xl.test.springtest.pojo.Moon;

@Configuration
public class ConfigurationAnnotationTest {
	
	@Bean
	public Moon testBeanAnnotation(Couple couple) {
		Moon moon = new Moon();
		moon.setBrightLevel(couple.getNames()+couple.getLocation()+" say: moon is so bright!");
		moon.setFullLevel("圆");
		return moon;
	}
	
	@Bean
	public Couple setInfo() {
		Couple couple = new Couple();
		couple.setLocation("under the tree");
		couple.setNames("Lily && Lucy");
		return couple;
	}
	
}

注入moon实例
package com.xl.test.springtest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xl.test.springtest.pojo.Moon;
import com.xl.test.springtest.test.InvocationInjection;

@RestController
public class ConfigurationController {
	
	@Autowired
	Moon moon;
	
	@GetMapping("/configTest")
	public String configTest() {
		System.out.println(moon.getBrightLevel());
		return null;
	}
	
	@RequestMapping("/inject")
	public String InjectionTest() {
		InvocationInjection ii = new InvocationInjection();
		String result = ii.test();
		return result;
	}
	
}

说明

@Configuration 官方定义

Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime

野鸡翻译:

@Configuration注解的类表示可以在类中申明一个或者多个@Bean方法,然后就可以有spring容器来处理:在运行的时候为这些@Bean方法生成的bean提供bean的定义以及服务请求

 @Configuration
 public class AppConfig {

     @Bean
     public MyBean myBean() {
         // instantiate, configure and return bean ...
     }
 }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5677095.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存