一:注入一个testutils类
パッケージcom.shop.sell.utils; com.shop.sell.dto.cartdtoをインポートします。 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.setProductuantity(10); cartdtoを返します。 }}二:创建一个获取豆的公共类
パッケージcom.shop.sell.utils; Import org.springframework.beans.beansexception; org.springframework.context.applicationcontextをインポートします。 org.springframework.context.applicationcontextawareをインポートします。 org.springframework.stereotype.componentをインポートします。 @component public class springutilを実装するapplicationcontextaware {private static applicationcontext applicationContext; @Override public void setApplicationContext(applicationContext ApplicationContext)throws beansexception {if(springutil.applicationContext == null){springutil.applicationContext = applicationContext; }} public static applicationContext getApplicationContext(){return applicationContext; } public staticオブジェクトgetBean(string name){return getApplicationContext()。getBean(name); } public static <t> t getBean(class <t> clazz){return getApplicationContext()。getBean(clazz); } public static <t> t getBean(string name、class <t> clazz){return getApplicationContext()。getBean(name、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をインポートします。 Import org.springframework.beans.factory.annotation.autowired; org.springframework.context.applicationcontextをインポートします。 Import 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( "/buyer/product")public class buyerproductcontroller {private static applicationcontext applicationContext; @AutowiredプライベートProductservice Productservice; @Autowired Private CategoryService CategoryService; @getMapping(value = "/list")public cartdto list(){cartdto cartdto =(cartdto)springutil.getbean( "testdemo"); cartdtoを返します。 }}四:使用郵便配達員测试结果
总结
以上所述是小编给大家介绍的关于スプリングブート获取IOC容器中注入的豆(推荐)、希望对大家有所帮助、如果大家有任何疑问请给我留言、小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!