我們可以在控制器中注入一個沒有默認構造函數的Service bean嗎?注入控制器中沒有默認構造函數的bean
我有以下控制器
@Controller
public class ArticleController {
@Autowired
private WithConstructorService withConstructorService;
...
}
我的服務是:
@Service
public class WithConstructorServiceImpl implements WithConstructorService {
String name;
String address;
public WithConstructorServiceImpl(String name, String address) {
super();
this.name = name;
this.address = address;
}
}
我得到異常
SEVERE: Servlet /springheat threw load() exception
java.lang.NoSuchMethodException: WithConstructorServiceImpl.<init>()
更新:
我在這裏猜測,但我們可以做一些AOP魔術,並仍然使用帶註釋的構造函數arg服務方法嗎?
從技術上講,這不是一個java「豆」,如果它沒有默認構造函數。 – digitaljoel
我從spring bean的角度提及它。 – tintin