如何使用自定义构造函数参数创建Autowire

时间:2016-09-18 11:46:44

标签: java spring

如何使用自定义构造函数参数在Spring中创建Autowire?以下是我的示例代码。我得到了无法自动装载的领域;没有默认构造函数发现错误。我不能引入默认构造函数,因为AbstractExecutor类没有默认构造函数。

IExecutor

public interface IExecutor {
    Object execute();
}

AbstractExecutor

public abstract class AbstractExecutor implements IExecutor {
    private IInputPropertyBean inputObject;
    private IOutputPropertyBean outputObject;
    public AbstractExecutor(IInputPropertyBean inputObject, IOutputPropertyBean outputObject) {
        this.inputObject = inputObject;
        this.outputObject = outputObject;
    }
    public abstract boolean isValidInput(IInputPropertyBean inputObject) ;
    public abstract  IInputPropertyBean getInputValues(IInputPropertyBean inputObject);
    public abstract  IOutputPropertyBean process(IInputPropertyBean attribute);
    public abstract  IOutputPropertyBean getOutputValues(IInputPropertyBean inputObject, IOutputPropertyBean outputObject;
    @Override
    public Object execute(){
        if(isValidInput(inputObject)){
            IInputPropertyBean inputAttribute = getInputValues(inputObject);
            IOutputPropertyBean outputAttribute = process(inputAttribute);
            outputObject = (IOutputPropertyBean) getOutputValues(outputAttribute, outputObject);
            return outputObject;
        }
    }
}

UserExecutor

@Repository("userExecutor")
public class UserExecutor extends AbstractExecutor { 
    public UserExecutor(@Qualifier("userInput") IInputPropertyBean inputObject, @Qualifier("userOutput")IOutputPropertyBean outputObject) {
        super(inputObject, outputObject);
    }

//override methods
}

IInputPropertyBean

public interface IInputPropertyBean { }

IOutputPropertyBean

public interface IOutputPropertyBean { }

UserInput

@Component("userInput")
public class UserInput implements IInputPropertyBean { //user sepecific properties }

UserOutput

@Component("userOutput")
public class UserOutput implements IOutputPropertyBean { //user sepecific properties }

UserServiceImpl

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    @Qualifier("userExecutor")
    private UserExecutor userExecutor;


    @Override
    public UserOutput userOperation(UserInput userInput, UserOutput userOutput) {
        // How to @Autowire UserExecutor with  userInput, userOutput comming in service parameter.

                //IExecutor executor = new UserExecutor(userInput, userOutput);
    //return (UserOutput) executor.execute();
    }
}

1 个答案:

答案 0 :(得分:1)

@Autowired构造函数中缺少UserExecutor。以下内容适用:

@Repository("userExecutor")
public class UserExecutor extends AbstractExecutor {
  @Autowired
  public UserExecutor(@Qualifier("userInput") UserInput input
    , @Qualifier("userOutput") UserOutput output) { super(input, output); }
}

Github上的工作代码。

您还可以使用泛型。

public interface IInput {}
public interface IOutput {}

public interface IExecutor<I extends IInput, O extends IOutput> {
  O execute(I i);
}

public class AbstractExecutor<I extends IInput, O extends IOutput>
       implements IExecutor<I, O> {
  private final I input;
  private final O output;

  protected AbstractExecutor(I input, O output) {
    this.input = input;
    this.output = output;
  }

  public O execute(I i) {
    ...
  }
}

@Component
public class UserInput implements IInput {}
@Component
public class UserOutput implements IOutput {}
@Repository
public class UserExecutor extends AbstractExecutor<UserInput, UserOutput> {
  @Autowired
  public UserExecutor(UserInput input, UserOutput output) {
    super(input, output);
  }
}

public interface IUserService {
  UserOutput execute(UserInput input, UserOutput output);
}

@Service
public UserService implements IUserService {
  private final UserExecutor executor;

  @Autowired
  public UserService(UserExecutor executor) {
    this.executor = executor;
  }

  public UserOutput execute(UserInput input) {
    return executor.execute(input);
  }
}

您将避免在任何地方使用@QualifierObject和打字。