我正在为$("#selectServices option:selected").closest('td').attr('id');
代码开发代码,到目前为止我开发了类似下面的代码,当我运行main方法时,我看到以下错误即将到来。
Injecting a prototype bean into a singleton bean
spring.xml
Jan 04, 2017 2:59:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ed322: startup date [Wed Jan 04 14:59:41 IST 2017]; root of context hierarchy
Jan 04, 2017 2:59:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Constructor:: RequestProcessor instance created!
Constructor:: RequestProcessor instance created!
Request ID : 1212
Exception in thread "main" java.lang.AbstractMethodError: com.injection.testing.RequestProcessor.createValidator()Lcom/injection/testing/RequestValidator;
at com.injection.testing.RequestProcessor.handleRequest(RequestProcessor.java:12)
at com.injection.testing.MainDemo.main(MainDemo.java:13)
RequestProcessor.java
<!-- Lookup way -->
<bean id="requestProcessor" class="com.injection.testing.RequestProcessor" >
<lookup-method name="getValidator" bean="validator" />
</bean>
<bean id="validator" class="com.injection.testing.RequestValidator" scope="prototype" />
RequestValidator.java
public abstract class RequestProcessor {
private RequestValidator validator;
public RequestProcessor(){
System.out.println("Constructor:: RequestProcessor instance created!");
}
public void handleRequest(String requestId){
System.out.println("Request ID : "+ requestId);
RequestValidator validator = createValidator(); //here Spring will create new instance of prototype bean
validator.validate(requestId);
}
public RequestValidator getValidator() {
return validator;
}
public void setValidator(RequestValidator validator) {
this.validator= validator;
}
protected abstract RequestValidator createValidator();
}
MainDemo.java
public class RequestValidator {
private List<String> errorMessages = new ArrayList<String>();
public RequestValidator() {
System.out.println("Constructor:: RequestValidator instance created!");
}
// Validates the request and populates error messages
public void validate(String requestId){
System.out.println("RequestValidator :"+requestId);
}
public List<String> getErrorMessages() {
return errorMessages;
}
}
的pom.xml
public class MainDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//RequestValidator requestValidator = (RequestValidator) context.getBean("validator");
RequestProcessor processor = (RequestProcessor) context.getBean("requestProcessor");
processor.handleRequest("1212");
System.out.println("------------------------");
processor.handleRequest("1213");
}
}
答案 0 :(得分:1)
将您的spring.xml
更新为以下
<bean id="requestProcessor" class="com.injection.testing.RequestProcessor">
<lookup-method name="createValidator" bean="validator" />
</bean>
基本上,您需要在abstract
xml属性中指定lookup-method
方法名称的名称。
在更正配置后我得到了以下输出
2017年1月4日下午4:11:20 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息:刷新org.springframework.context.support.ClassPathXmlApplicationContext@13c675d:启动日期[Wed Jan 04 16:11:20 IST 2017];上下文层次结构的根 2017年1月4日下午4:11:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO:从类路径资源[spring.xml]加载XML bean定义
构造函数:: RequestProcessor实例创建!
请求ID:1212
构造函数:: RequestValidator实例创建!
RequestValidator:1212
请求ID:1213
构造函数:: RequestValidator实例创建!
RequestValidator:1213