尽管定义了NoSuchBeanDefinitionException

时间:2016-11-06 03:09:42

标签: spring spring-boot javabeans autowired

我有Beans.xml定义:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">




    <bean id="Products" class="com.example.Products">
        <property name="products">
            <map key-type="java.lang.String" value-type="com.example.Product">
                <entry key="Coke">
                    <bean class="com.example.Product">
                        <property name="name" value="Coke"/>
                        <property name="price" value="2.0"/>
                    </bean>
                </entry>
                <entry key="Crisps">
                    <bean class="com.example.Product">
                        <property name="name" value="Crisps"/>
                        <property name="price" value="4.29"/>
                    </bean>
                </entry>
                <entry key="Snickers">
                    <bean class="com.example.Product">
                        <property name="name" value="Snickers"/>
                        <property name="price" value="1.69"/>
                    </bean>
                </entry>
                <entry key="Beer">
                    <bean class="com.example.Product">
                        <property name="name" value="Beer"/>
                        <property name="price" value="3.6"/>
                    </bean>
                </entry>

            </map>

        </property>

    </bean>

    <bean id="coinVerter" class="com.example.CoinVerter" />


</beans>

问题是产品能够很好地自动装配。

package com.example;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by oneat on 11/5/16.
 */
public class Products {

    public Map<String, Product> products = new HashMap<>();

    Products() {

    }


    public final void add(Product p) {
        products.put(p.name, p);
    }

    public void setProducts(Map<String, Product> products) {
        this.products = products;
    }

    public Map<String, Product> getProducts() {
        return products;
    }

    public Collection<Product> toArray(){
        return products.values();

    }

}

然而CoinVerter真的无法用

进行自动装配
  

说明

     

com.example.FuckingController中的字段coinConverter需要一个bean   类型&#39; com.example.CoinVerter&#39;无法找到。

     

动作:

     

考虑定义类型&#39; com.example.CoinVerter&#39;在你的   配置。

这是他的src:

package com.example;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by oneat on 11/6/16.
 */
public class CoinVerter {


    public CoinVerter() {
    }

    public CoinTainer ArrayToCoinTainer(Integer[] i) throws Exception {
        CoinTainer ct = new CoinTainer();
        Integer[] conv = Arrays.copyOf(i, i.length);
        for (int a = 0; a < conv.length; a++) {
            while (conv[a]-- > 0) ct.add(new Coin(Coin.values[a]));
        }
        return ct;
    }


    public Integer[] CoinTainerToArray(CoinTainer ct) throws Exception {
        List<Integer> li = new LinkedList<>();
        Coin[] co = Coin.returnAll();
        for (Coin c : co) {
            li.add(Collections.frequency(ct.lc, c));
        }
        return li.toArray(new Integer[0]);
    }

}

知道为什么会这样吗?

自动装配:

@Controller
public class FuckingController {

    @Autowired
    CoinVerter coinConverter;

    @Autowired
    Products products;






    @RequestMapping("/")
    public String index(@RequestParam(value="yourcoins", required=false) Integer[] values, Model m) throws Exception{

        m.addAttribute("coins",Coin.returnAll());
        m.addAttribute("products", products.toArray());
        m.addAttribute("coinvalues", products.toArray());


        return "index";
    }
}

1 个答案:

答案 0 :(得分:0)

为了让Spring DispatcherServlet选择并加载bean,您需要将 Beans.xml 添加到 web.xml 如下:

<servlet>
        <servlet-name>spring-web</servlet-name>
        <servlet-class>
                       org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/Beans.xml</param-value>
            <!-- Change this xml path correctly if NOT under WEB-INF -->
        </init-param>
    </servlet>