如何在Spring Boot MVC Web应用程序中实现Bean注入

huangapple go评论108阅读模式
英文:

How to make bean injection in spring boot mvc web application

问题

以下是您要翻译的内容:

你能告诉我在哪里以及如何编写以下代码:

  1. final ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  2. final HelloController store = context.getBean("store", HelloController.class);

以便在应用程序启动时进行依赖注入并创建我的对象。我想在创建的对象上获取总价格。

HelloController.java

  1. @Controller
  2. public class HelloController {
  3. private List<Products> storeList = new ArrayList<Products>();
  4. private int totalPrice = 0;
  5. public List<Products> getStoreList() {
  6. return this.storeList;
  7. }
  8. public int getTotalPrice() {
  9. return this.totalPrice;
  10. }
  11. @RequestMapping("/")
  12. public String index() {
  13. return "index";
  14. }
  15. @PostMapping("/totalprice")
  16. public String countTotalPrice(@RequestParam("name") final String name, final Model model) {
  17. final String[] values = name.trim().split("\\s*,\\s*");
  18. final List<Integer> listIds = new ArrayList<Integer>();
  19. for (int i = 0; i < values.length; i++) {
  20. listIds.add(Integer.parseInt(values[i]));
  21. for (int j = 0; j < this.storeList.size(); j++) {
  22. if (listIds.get(i) == this.storeList.get(j).getId()) {
  23. this.totalPrice += this.storeList.get(j).getPrice();
  24. }
  25. }
  26. }
  27. model.addAttribute("name", this.totalPrice);
  28. return "totalprice";
  29. }
  30. public void setStoreList(final List<Products> storeList) {
  31. this.storeList = storeList;
  32. }
  33. }

MainApp.java - 运行应用程序

  1. @SpringBootApplication
  2. public class MainApp {
  3. public static void main(final String[] args) {
  4. SpringApplication.run(MainApp.class, args);
  5. }
  6. }

Products.java - 产品类

  1. public class Products {
  2. private String name;
  3. private int price;
  4. private int id;
  5. public Products(final String name, final int price, final int id) {
  6. this.name = name;
  7. this.price = price;
  8. this.id = id;
  9. }
  10. // ...
  11. }

我的 beans.xml 文件

  1. <beans
  2. ...
  3. <bean id="product1"
  4. class="... .Products">
  5. <constructor-arg name="name" value="Cherry" />
  6. <constructor-arg name="price" value="500" />
  7. <constructor-arg name="id" value="1" />
  8. </bean>
  9. <bean id="product2"
  10. class="... .Products">
  11. <constructor-arg name="name" value="Cucumber" />
  12. <constructor-arg name="price" value="1000" />
  13. <constructor-arg name="id" value="2" />
  14. </bean>
  15. <bean id="product3"
  16. class="... .Products">
  17. <constructor-arg name="name" value="Apple" />
  18. <constructor-arg name="price" value="3000" />
  19. <constructor-arg name="id" value="3" />
  20. </bean>
  21. <bean id="store"
  22. class="... .controller.HelloController">
  23. <property name="storeList">
  24. <list>
  25. <ref bean="product1"/>
  26. <ref bean="product2"/>
  27. <ref bean="product3"/>
  28. </list>
  29. </property>
  30. </bean>
  31. </beans>
英文:

Can you please tell where and how i have to write:

  1. final ApplicationContext context = new ClassPathXmlApplicationContext(&quot;beans.xml&quot;);
  2. final HelloController store = context.getBean(&quot;store&quot;, HelloController .class);

so that dependency injection takes place and my objects are created when the application starts. And i could get the total price of created objects.

HelloController.java

  1. @Controller
  2. public class HelloController {
  3. private List&lt;Products&gt; storeList = new ArrayList&lt;Products&gt;();
  4. private int totalPrice = 0;
  5. public List&lt;Products&gt; getStoreList() {
  6. return this.storeList;
  7. }
  8. public int getTotalPrice() {
  9. return this.totalPrice;
  10. }
  11. @RequestMapping(&quot;/&quot;)
  12. public String index() {
  13. return &quot;index&quot;;
  14. }
  15. @PostMapping(&quot;/totalprice&quot;)
  16. public String countTotalPrice(@RequestParam(&quot;name&quot;) final String name, final Model model) {
  17. final String[] values = name.trim().split(&quot;\\s*,\\s*&quot;);
  18. final List&lt;Integer&gt; listIds = new ArrayList&lt;Integer&gt;();
  19. for (int i = 0; i &lt; values.length; i++) {
  20. listIds.add(Integer.parseInt(values[i]));
  21. for (int j = 0; j &lt; this.storeList.size(); j++) {
  22. if (listIds.get(i) == this.storeList.get(j).getId()) {
  23. this.totalPrice += this.storeList.get(j).getPrice();
  24. }
  25. }
  26. }
  27. model.addAttribute(&quot;name&quot;, this.totalPrice);
  28. return &quot;totalprice&quot;;
  29. }
  30. public void setStoreList(final List&lt;Products&gt; storeList) {
  31. this.storeList = storeList;
  32. }
  33. }

MainApp.java - Run the Application

  1. @SpringBootApplication
  2. public class MainApp {
  3. public static void main(final String[] args) {
  4. SpringApplication.run(MainApp.class, args);
  5. }
  6. }

Products.java - my class for products

  1. public class Products {
  2. private String name;
  3. private int price;
  4. private int id;
  5. public Products(final String name, final int price, final int id) {
  6. this.name = name;
  7. this.price = price;
  8. this.id = id;
  9. }
  10. ...

My beans.xml file

  1. &lt;beans
  2. ...
  3. &lt;bean id=&quot;product1&quot;
  4. class=&quot;... .Products&quot;&gt;
  5. &lt;constructor-arg name=&quot;name&quot; value=&quot;Cherry&quot; /&gt;
  6. &lt;constructor-arg name=&quot;price&quot; value=&quot;500&quot; /&gt;
  7. &lt;constructor-arg name=&quot;id&quot; value=&quot;1&quot; /&gt;
  8. &lt;/bean&gt;
  9. &lt;bean id=&quot;product2&quot;
  10. class=&quot;... .Products&quot;&gt;
  11. &lt;constructor-arg name=&quot;name&quot; value=&quot;Cucumber&quot; /&gt;
  12. &lt;constructor-arg name=&quot;price&quot; value=&quot;1000&quot; /&gt;
  13. &lt;constructor-arg name=&quot;id&quot; value=&quot;2&quot; /&gt;
  14. &lt;/bean&gt;
  15. &lt;bean id=&quot;product3&quot;
  16. class=&quot;... .Products&quot;&gt;
  17. &lt;constructor-arg name=&quot;name&quot; value=&quot;Apple&quot; /&gt;
  18. &lt;constructor-arg name=&quot;price&quot; value=&quot;3000&quot; /&gt;
  19. &lt;constructor-arg name=&quot;id&quot; value=&quot;3&quot; /&gt;
  20. &lt;/bean&gt;
  21. &lt;bean id=&quot;store&quot;
  22. class=&quot;... .controller.HelloController&quot;&gt;
  23. &lt;property name=&quot;storeList&quot;&gt;
  24. &lt;list&gt;
  25. &lt;ref bean=&quot;product1&quot;/&gt;
  26. &lt;ref bean=&quot;product2&quot;/&gt;
  27. &lt;ref bean=&quot;product3&quot;/&gt;
  28. &lt;/list&gt;
  29. &lt;/property&gt;
  30. &lt;/bean&gt;
  31. &lt;/beans&gt;

答案1

得分: 1

你可以创建配置:

  1. @Configuration
  2. @ImportResource({"classpath*:beans.xml"})
  3. public class Config {
  4. }

并且可以像这样从 XML 中使用你的 beans:

  1. @Controller
  2. public class HelloController {
  3. @Autowired
  4. Store store;
  5. ...
  6. }
英文:

You can create configuration:

  1. @Configuration
  2. @ImportResource({&quot;classpath*:beans.xml&quot;})
  3. public class Config {
  4. }

And use your beans from xml like:

  1. @Controller
  2. public class HelloController {
  3. @Autowired
  4. Store store;
  5. ...

答案2

得分: 1

  1. @ImportResource("classpath:beans.xml")
  2. 将这个注解添加到SpringBootApplication注解旁边
  3. 然后在需要引用 'store' bean 的地方只需这样写
  4. @Autowired
  5. Store store;
英文:
  1. @ImportResource(&quot;classpath:beans.xml&quot;)

Add this annotation next to the SpringBootApplication annotation.

Then, where u need reference to the 'store' bean, just say

  1. @Autowired
  2. Store store;

huangapple
  • 本文由 发表于 2020年3月4日 04:28:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/60515015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定