我正在使用Spring MVC控制器项目.下面是我的控制器,我有一个声明的构造函数,我专门用于测试目的.
@Controller
public class TestController {
private static KeeperClient testClient = null;
static {
// some code here
}
/**
* Added specifically for unit testing purpose.
*
* @param testClient
*/
public TestController(KeeperClient testClient) {
TestController.testClient = testClient;
}
// some method here
}
Run Code Online (Sandbox Code Playgroud)
每当我启动服务器时,我都会遇到异常 -
No default constructor found; nested exception is java.lang.NoSuchMethodException:
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除TestController构造函数,那么它没有任何问题.我在这做什么错?
但是,如果我添加这个默认构造函数,那么它开始工作正常 -
public TestController() {
}
Run Code Online (Sandbox Code Playgroud) 我遇到了我的Spring控制器问题 - 我没有找到默认的构造函数 - 但是他们确实有一个我试图通过applicationContext.xml创建的构造函数 - 以下是相关的一点:
<bean id="PcrfSimulator" class="com.rory.services.pcrf.simulator.PcrfSimulator" init-method="start">
</bean>
<bean id="CacheHandler" class="com.rory.services.pcrf.simulator.handlers.CacheHandler">
<constructor-arg index="0" type="com.rory.services.pcrf.simulator.CustomGxSessionIdCacheImpl">
<bean factory-bean="PcrfSimulator" factory-method="getGxSessionIdCache">
</bean>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
IE浏览器.我首先创建一个bean,然后尝试将该bean的方法调用结果传递给第二个bean的(CacheHandler)构造函数.
这是CacheHandler的开始:
@Controller
public class CacheHandler {
private final CustomGxSessionIdCacheImpl gxSessionIdCache;
public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) {
this.gxSessionIdCache = gxSessionIdCache;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheHandler' defined in URL [jar:file:/users/rtorney/Documents/apache-tomcat-7.0.25/webapps/PCRFSimulator-4.0/WEB-INF/lib/PCRFSimulator-4.0.jar!/com/rory/services/pcrf/simulator/handlers/CacheHandler.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.rory.services.pcrf.simulator.handlers.CacheHandler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.rory.services.pcrf.simulator.handlers.CacheHandler.<init>()
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢!
我试图按照Mkyong上有关Spring 的教程进行学习。我创建了两个简单的类,Customer和Person,如下所示:
客户:
public class Customer {
private Person person;
public Customer() {
}
//Getters and setters for person here
public String toString() {
return "Customer [person=" + person +"]";
}
}
Run Code Online (Sandbox Code Playgroud)
人物:
public class Person {
private String name;
private String address;
private int age;
public Person() {
}
public Person(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
//All getters and setters
public String …Run Code Online (Sandbox Code Playgroud) 我正在寻找通过构造函数注入使用Spring自动装配Scala类的"惯用"方法.我尝试过这样的事情:
@Component
class MyService @Autowired() ( val myDao: MyDao) extends Logging {
...
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:bean的实例化失败了; 嵌套的例外是org.springframework.beans.BeanInstantiationException:无法实例化bean类[为MyService]:没有发现默认的构造函数; 嵌套异常是java.lang.NoSuchMethodException:为MyService()在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:964)〜[弹簧豆-3.0.7.RELEASE.jar:3.0. 7.RELEASE]