我在春天遇到模板设计模式的配置问题。
App.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "Spring-Module.xml" })
public class App {
@Autowired
@Qualifier("userExecutor")
private IExecutor userExecutor;
public void print(){
System.out.println(userExecutor.execute());
}
}
弹簧Module.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.ravi" />
</beans>
IExecutor.java
public interface IExecutor {
public Object execute();
}
AbstractExecutor.java
public abstract class AbstractExecutor implements IExecutor {
private IInputPropertyBean inputObject;
private IOutputPropertyBean outputObject;
public abstract IAttribute getInputValues(Object inputObject);
public abstract IAttribute process(IAttribute inputPropertyBean);
public abstract IOutputPropertyBean getOutputValues(IAttribute outputPropertyBean, IOutputPropertyBean outObject);
public AbstractExecutor(IInputPropertyBean inputObject, IOutputPropertyBean outputObject) {
this.inputObject = inputObject;
this.outputObject = outputObject;
}
@Override
public Object execute() {
IAttribute inputAttribute = getInputValues(inputObject);
IAttribute outputAttribute = process(inputAttribute);
outputObject = (IOutputPropertyBean) getOutputValues(outputAttribute, outputObject);
return outputObject;
}
}
AbstractUserExecutor.java
public abstract class AbstractUserExecutor extends AbstractExecutor {
public AbstractUserExecutor(IInputPropertyBean inputObject, IOutputPropertyBean outputObject) {
super(inputObject, outputObject);
}
@Override
public IAttribute getInputValues(Object inputObject) {
System.out.println("getInputValues");
return null;
}
@Override
public IOutputPropertyBean getOutputValues(IAttribute outputPropertyBean, IOutputPropertyBean outObject) {
System.out.println("getOutputValues");
return null;
}
}
UserExecutor.java
@Repository("userExecutor")
public class UserExecutor extends AbstractUserExecutor {
@Autowired
@Qualifier("userInput")
private IInputPropertyBean inputObject;
@Autowired
@Qualifier("userOutput")
private IOutputPropertyBean outputObject;
public UserExecutor(IInputPropertyBean inputObject, IOutputPropertyBean outputObject) {
super(inputObject, outputObject);
}
@Override
public IAttribute process(IAttribute inputPropertyBean) {
System.out.println("process");
return null;
}
}
IInputPropertyBean.java
public interface IInputPropertyBean {}
IOutputPropertyBean.java
public interface IOutputPropertyBean { }
UserInput.java
@Component("userInput")
public class UserInput implements IInputPropertyBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserOutput.java
@Component("userOutput")
public class UserOutput implements IOutputPropertyBean {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
IAttribute.java
public interface IAttribute { }
用户.java
@Component
public class User implements IAttribute{
}
答案 0 :(得分:1)
我在我的本地环境中尝试过您的代码。我遇到的问题是UserExecutor
,AbstractUserExecutor
和AbstractExecutor
类缺少默认构造函数。添加它后,它工作。
所以我认为你的问题是缺少默认构造函数。尝试添加它们,让我知道它是否有效。