Spring--getBean()与@Autowired的对比

原文网址:Spring--getBean()与@Autowired的对比_IT利刃出鞘的博客-CSDN博客

简介

说明

本文介绍getBean()与@Autowired的对比。

ApplicationContext#getBean()与@Autowired的简要对比

@AutowiredgetBean()
是否触发依赖注入

是。

如果注入的对象还未注册到容器,则会先注册它。

否。

如果注入的对象还未注册到容器,不会去注册它,只会获得一个null。

单例与多例

默认是单例。

就算是将bean加注解改为多例,此时注入仍为单例。

默认是单例。

但可将bean加注解改为多例,此时getBean()获取即为多例。

单例与多例

注意:这里可能是因为@Controller注入比较特殊,只有这时将bean加注解改为多例,此时注入仍为单例。不过还需要验证。

@Autowired实例

package com.example.controller;

import com.example.service.UserService;
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;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private UserService userService;

    @GetMapping("/test1")
    public String test1() {
        return userService.toString();
    }

    @GetMapping("/test2")
    public String test2() {
        return userService.toString();
    }
}
package com.example.service;

import org.springframework.stereotype.Component;

@Component
public class UserService {
}

测试(都是单例的)

http://localhost:8080/test1    结果:com.example.service.UserService@27a70937
http://localhost:8080/test2    结果:com.example.service.UserService@27a70937
再次http://localhost:8080/test2    结果:com.example.service.UserService@27a70937

将UserService改为多例

package com.example.service;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserService {
}

测试(还是单例的)

http://localhost:8080/test1    结果:com.example.service.UserService@12d4c297
http://localhost:8080/test2   结果:com.example.service.UserService@12d4c297
再次http://localhost:8080/test2    结果:com.example.service.UserService@12d4c297

ApplicationContext实例

package com.example.controller;

import com.example.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @GetMapping("/test1")
    public String test1() {
        UserService userService = applicationContext.getBean(UserService.class);
        return userService.toString();
    }

    @GetMapping("/test2")
    public String test2() {
        UserService userService = applicationContext.getBean(UserService.class);
        return userService.toString();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
package com.example.service;

import org.springframework.stereotype.Component;

@Component
public class UserService {
}

测试(都是单例的)

http://localhost:8080/test1    结果:com.example.service.UserService@27a70937
http://localhost:8080/test2    结果:com.example.service.UserService@27a70937
再次http://localhost:8080/test2    结果:com.example.service.UserService@27a70937

package com.example.service;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserService {
}

将UserService改为多例

package com.example.service;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserService {
}

测试(是多例了)

http://localhost:8080/test1    结果:com.example.service.UserService@393b1c
http://localhost:8080/test2     结果:com.example.service.UserService@23c2e5c2
再次http://localhost:8080/test2     结果:com.example.service.UserService@5e743c55

触发依赖注入

上边是文章的部分内容,为统一维护,全文已转移到此网址:Spring-getBean()与@Autowired的对比 - 自学精灵

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT利刃出鞘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值