获取Bean的六种方式

目录

方式一

方式二

方式三

方式四

方式五

方式六


方式一

 

使用@Autowired注解,会提示不建议使用字段注入,不会影响什么。

import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired //不建议使用字段注入
    private UserService userService;

}

 

方式二

 

使用构造方法,代码会略显繁琐,建议加上final修饰。

import com.demo.service.UserService;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    private final UserService userService; //建议加上final修饰

    public UserController(UserService userService) {
        this.userService = userService;
    }

}

 

方式三

 

使用@RequiredArgsConstructor注解,推荐使用,必须加上final修饰。

import com.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService; //必须加上final修饰

}

方式四

 

使用IOC容器对象,调用getBean()方法,通过Bean的名称获取Bean对象。

注意导包org.springframework.context.ApplicationContext

import com.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final ApplicationContext applicationContext;

    @RequestMapping("/demo")
    public void demo() {

        UserService userService = (UserService) applicationContext.getBean("userService");

    }

}

 

方式五

 

使用IOC容器对象,调用getBean()方法,通过Bean的类型获取Bean对象。

import com.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final ApplicationContext applicationContext;

    @RequestMapping("/demo")
    public void demo() {

        UserService userService = (UserService) applicationContext.getBean(UserService.class);

    }

}

 

方式六

 

使用IOC容器对象,调用getBean()方法,通过Bean的名称和类型获取Bean对象。

import com.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final ApplicationContext applicationContext;

    @RequestMapping("/demo")
    public void demo() {

        UserService userService = (UserService) applicationContext.getBean("userService", UserService.class);

    }

}
  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值