gpt4 book ai didi

java - 模板设计模式的 Spring 配置问题

转载 作者:行者123 更新时间:2023-11-30 10:41:34 25 4
gpt4 key购买 nike

我在 Spring 使用模板设计模式时遇到配置问题。

应用程序.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());
}
}

Spring-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();
}

抽象执行器.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;
}

}

抽象用户执行器.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;
}

}

用户执行器.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 { }

用户输入.java

@Component("userInput")
public class UserInput implements IInputPropertyBean {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

用户输出.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{
}

最佳答案

我已经在我的本地环境中尝试了您的代码。我遇到的问题是 UserExecutorAbstractUserExecutorAbstractExecutor 类缺少默认构造函数。添加这些后它起作用了。

所以我认为您的问题是缺少默认构造函数。尝试添加它们,让我知道它是否有效。

关于java - 模板设计模式的 Spring 配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com