一 : ut testutils 类
패키지 com.shop.sell.utils; import com.shop.sell.dto.cartdto; import org.springframework.context.annotation.bean; org.springframework.context.annotation.configuration; @configuration public class testutils {@bean (name = "testdemo") public cartdto said () {cartdto cartdto = new cartdto (); cartdto.setProductID (789); cartdto.setProductquantity (10); 반환 cartdto; }}二 : 的公 Bean 创建一个获取 쿨
패키지 com.shop.sell.utils; org.springframework.beans.beansexception import; import org.springframework.context.applicationcontext; org.springframework.context.applicationcontextaware import; org.springframework.stereotyp.component import; @Component Public Class SpringUtil은 ApplicationContextAware {private static ApplicationContext ApplicationContext를 구현합니다. @override public void setApplicationContext (ApplicationContext ApplicationContext)는 beansexception {if (springUtil.applicationContext == null) {springUtil.applicationContext = applicationContext; }} public static ApplicationContext getApplicationContext () {return ApplicationContext; } public static 객체 getBean (문자열 이름) {return getApplicationContext (). getBean (이름); } public static <t> t getBean (class <t> clazz) {return getApplicationContext (). getBean (Clazz); } public static <t> t getBean (문자열 이름, 클래스 <t> clazz) {return getApplicationContext (). getBean (이름, Clazz); }}三 : 测试结果 Bean 在控制器中获取
package com.shop.sell.controller; import com.shop.sell.utils.resultvoutil; import com.shop.sell.utils.springutil; com.shop.sell.vo.productinfovo import; import com.shop.sell.vo.productvo; import com.shop.sell.vo.resultvo; com.shop.sell.dataobject.productcategory import; import com.shop.sell.dataobject.productinfo; import com.shop.sell.dto.cartdto; com.shop.sell.from.orderform import; com.shop.sell.service.categoryservice import; com.shop.sell.service.productservice import; org.springframework.beans.beanutils import; org.springframework.beans.factory.annotation.autowired; import 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 가져 오기; import java.util.arrays; Java.util.list 가져 오기; /** * 买家商品 */@RestController @requestmapping ( "/buyer/product") 공개 클래스 구매 구매 제품 콘트롤러 {private static ApplicationContext ApplicationContext; @autowired private productservice productservice; @autowired private categoryservice categoryService; @getMapping (value = "/list") public cartdto list () {cartdto cartdto = (cartdto) springUtil.getBean ( "testdemo"); 반환 cartdto; }}四 : man 우편 测试结果
总结
SpringBoot ot IOC 容器中注入的 Bean (推荐 推荐), 希望对大家有所帮助, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!