原文网址:Spring--getBean()与@Autowired的对比_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍getBean()与@Autowired的对比。
ApplicationContext#getBean()与@Autowired的简要对比
项 | @Autowired | getBean() |
是否触发依赖注入 | 是。 如果注入的对象还未注册到容器,则会先注册它。 | 否。 如果注入的对象还未注册到容器,不会去注册它,只会获得一个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的对比 - 自学精灵