@Component注解 @Bean注解 @Autowired注解

目录

1.@Service说明

2.获取bean的方式

3.其他的bean的注入

4.Autowired注解


1.@Service说明

注解本质上就是一个类,开发中我们可以使用注解取代xml配置文件。
@component是spring中的一个注解,它的作用就是实现bean的注入。在Java的web开发中,提供3个@Component注解衍生注解(功能与@component一样)分别是:

1、@Controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层。

2、@Service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理

3、@Repository(实现dao访问) 用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件

而@Component泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

下面写这个是引入component的扫描组件 (这是在配置文件中的书写格式,如spring mvc中的applicationcontent.xml,在spring boot中的话,因采用的是零配置所以要直接在类上加入@component注解就可以了)

<context:component-scan base-package=”com.mmnc”>
上面的这个例子是引入Component组件的例子,其中base-package表示为需要扫描的所有子包。
共同点:被@controller 、@service、@repository 、@component 注解的类,都会把这些类纳入进spring容器中进行管理

2.获取bean的方式

@Slf4j
@Service
public class BmsBillMemoServiceImpl implements IBmsBillMemoService {
    @Autowired
    private BmsBillMemoMapper bmsBillMemoMapper;
    @Autowired
    private OrderSourceStrategyHelper orderSourceStrategyHelper;
package com.example.demo;

import com.example.demo.service.IBmsBillMemoService;
import com.example.demo.service.impl.BmsBillMemoServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class DemoApplication {

//    @Bean
//    public RestTemplate getRestTemplate(){
//        return new RestTemplate();
//    }
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
        Object bean = ctx.getBean("bmsBillMemoServiceImpl");
        System.out.println(bean);
        IBmsBillMemoService bean1 = ctx.getBean(IBmsBillMemoService.class);
        System.out.println(bean1);
        BmsBillMemoServiceImpl bean2 = ctx.getBean("bmsBillMemoServiceImpl", BmsBillMemoServiceImpl.class);
        System.out.println(bean2);
    }

}

在springboot的启动类中,run方法返回一个容器对象,当直接使用@Service进行注入时,可以通过getBean方法进行bean的获取,也就是说,当使用@Service时,spring扫描到了之后,会将其放到spring容器中,这样才能在其他地方通过@Autowire注解进行注入。

3.其他的bean的注入

下面这个例子是restTemplate的注入

SpringBoot 推荐使用JAVA配置的方式来完全代替XML配置,JAVA配置是通过 @Configration 和 @Bean 注解实现的。
(1)@Configration 注解:用于声明当前类是一个配置类,相当于Spring中的一个 XML 文件;相当XML文件中的标签
(2)@Bean 注解:作用在方法上,声明当前方法的返回值是一个Bean对象;相当XML文件中的标签。
Spring的@Bean注解标注在方法上,用于告诉方法去产生一个Bean对象,然后把这个Bean对象交给Spring容器来管理,产生这个Bean对象的方法Spring只会调用一次。可以使用@Autowired或者@Resource注解获取Bean。(通过byTYPE方式(@Autowired)、byNAME方式(@Resource))
注解@Bean被声明在方法上,该方法都需要有一个返回类型,这个方法的返回类型就是注册到IOC容器中的类型,接口和类都是可以作为返回类型,介于面向接口原则,提倡返回类型为接口。该注解主要用在@Configuration注解的类的方法上,也可以用在@Component注解的类的方法上,添加的bean的id为方法名。

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author linaibo
 * @version 1.0
 * Create by 2022/11/13 15:39
 */
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate get(){
        return new RestTemplate();
    }
}

除了上面这种方式,还有好多方式,但不是特别的常用,比较底层,这里就不介绍了

4.Autowired注解

注入机制:首先根据类型去查找,如果根据类型能找到多个的话,就需要在指定一下名字(如一个service接口有多个实现类时),通过@Qualifier(value = "charles")注解来实现根据名字来注入,详细如下:

下面这个错误代码,根据类型查找到了两个bean

Field person in com.demos.springboot.SpringbootApplication required a single bean, but 2 were found:
......

 可以通过修改bean的名字来进行注入

/**
 * 将 Person 注入进来
 */
@Autowired
private Person charles;

 也可以添加@Qualifier注解指定bean的名字完成注入

/**
 * 将 Person 注入进来
 */
@Autowired
@Qualifier(value = "charles")
private Person person;

参照博客

注解 @Autowired 的注入机制_江湖人称小程的博客-CSDN博客_autowired注解根据什么注入

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值