spring - NoSuchBeanDefinitionException despite being defined -
i have beans.xml definition:
<?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>
the problem products autowired quite well.
package com.example; import java.util.collection; import java.util.hashmap; import java.util.map; /** * created 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(); } }
however coinverter fails on autowiring with
description:
field coinconverter in com.example.fuckingcontroller required bean of type 'com.example.coinverter' not found.
action:
consider defining bean of type 'com.example.coinverter' in configuration.
here src:
package com.example; import java.util.arrays; import java.util.collections; import java.util.linkedlist; import java.util.list; /** * created 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); (int = 0; < 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(); (coin c : co) { li.add(collections.frequency(ct.lc, c)); } return li.toarray(new integer[0]); } }
any idea why happening?
autowiring:
@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"; } }
in order make spring's dispatcherservlet
pick , load the beans, need add beans.xml web.xml below:
<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 xml path correctly if not under web-inf --> </init-param> </servlet>
Comments
Post a Comment