我有beans.xml的定義:NoSuchBeanDefinitionException儘管被定義
<?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真的無法與自動裝配
說明:
場coinConverter在com.example.FuckingController所需類型的 'com.example.CoinVerter' 豆 可能不被發現。
操作:
考慮您的 配置中定義類型 'com.example.CoinVerter' 的豆。
這裏是他的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.您確定'Product'自動裝配到控制器上嗎? 2.如何將'beans.xml'導入Spring Boot應用程序? –
不幸的是,maven似乎有重建問題。 清潔後,建立一切工作正常。 – oneat