一: 注入一个 testutils 类
แพ็คเกจ com.shop.sell.utils; นำเข้า com.shop.sell.dto.cartdto; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; @Configuration คลาสสาธารณะ TestUtils {@Bean (name = "testDemo") สาธารณะ cartdto กล่าวว่า () {cartdto cartdto = new cartdto (); cartdto.setProductId (789); cartdto.setProductQuantity (10); ส่งคืน cartdto; -二: 创建一个获取ถั่ว的公的公类
แพ็คเกจ com.shop.sell.utils; นำเข้า org.springframework.beans.beansexception; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.applicationContextaware; นำเข้า org.springframework.stereotype.component; @Component Public Class SpringUtil ใช้ ApplicationContextAware {ApplicationContext Private ApplicationContext ApplicationContext; @Override โมฆะสาธารณะ setApplicationContext (ApplicationContext ApplicationContext) พ่น beansexception {if (springUtil.applicationContext == null) {springUtil.applicationContext = ApplicationContext; }} แอปพลิเคชันสาธารณะคงที่ getApplicationContext () {return ApplicationContext; } วัตถุคงที่สาธารณะ getBean (ชื่อสตริง) {return getApplicationContext (). getBean (ชื่อ); } สาธารณะคงที่ <t> t getBean (คลาส <t> clazz) {return getApplicationContext (). getBean (clazz); } สาธารณะคงที่ <t> t getBean (ชื่อสตริง, คลาส <t> clazz) {return getApplicationContext (). getBean (ชื่อ, clazz); -三: 在控制器中获取ถั่ว测试结果
แพ็คเกจ com.shop.sell.controller; นำเข้า com.shop.sell.utils.resultvoutil; นำเข้า com.shop.sell.utils.springutil; นำเข้า com.shop.sell.vo.productinfovo; นำเข้า com.shop.sell.vo.productvo; นำเข้า com.shop.sell.vo.resultvo; นำเข้า com.shop.sell.dataobject.productcategory; นำเข้า com.shop.sell.dataobject.productinfo; นำเข้า com.shop.sell.dto.cartdto; นำเข้า com.shop.sell.from.orderform; นำเข้า com.shop.sell.service.categoryservice; นำเข้า com.shop.sell.service.productservice; นำเข้า org.springframework.beans.beanutils; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.restcontroller; นำเข้า java.util.arraylist; นำเข้า Java.util.Arrays; นำเข้า java.util.list; /** * 买家商品 */@RestController @RequestMapping ("/ผู้ซื้อ/ผลิตภัณฑ์") ระดับสาธารณะผู้ซื้อ ProductController {Private application ApplicationContext ApplicationContext; @AutoWired Productservice Productservice ผลิตภัณฑ์; @autowired หมวดหมู่ส่วนตัวหมวดหมู่บริการ; @getMapping (value = "/list") public cartdto list () {cartdto cartdto = (cartdto) springutil.getBean ("testDemo"); ส่งคืน cartdto; -四: 使用บุรุษไปรษณีย์测试结果
总结
以上所述是小编给大家介绍的关于 Springboot 获取 ioc 容器中注入的ถั่ว (推荐), 希望对大家有所帮助,, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!