Enable dependencies for default Java 8 interface methods

I have an interface that defines setter / getter using the java 8 default methods, but I get an error when trying to connect it in Spring. I really want to avoid using an abstract class, and I don't want to duplicate code. That's what I'm doing:

public interface MyProcessor { public static final WeakHashMap<Function1D, Integer> paramMap = new WeakHashMap<>(); default void setParam(int param) { paramMap.put(this, param); } default int getParam() { return paramMap.get(this); } default double doSomthingWithParam(double x) { return doSomething() * getParam(); } double doSomething(); } public class MyProcessorImp extends SomeClass implements MyProcessor { double doSomething() {....} } <bean class="....MyProcessorImp"> <property name="param" value="3"/></bean> 

Bean The 'param' property is not writable or has an invalid setter method.

+6
source share
1 answer

In my own projects, I get the developer to provide me with the dependency provided by the spring DI container.

Recycling the code above, it will look like this:

 public interface MyProcessor { // get the implementor to get the hash map WeakHashMap<Function1D, Integer> getParamMap(); default double doSomthingWithParam(double x) { return doSomething() * getParam(); } // uses the implementor getParamMap() to get params default int getParam() { return getParamMap().get(this); } double doSomething(); } public class MyProcessorImp extends SomeClass implements MyProcessor { final WeakHashMap<Function1D, Integer> paramMap = new WeakHashMap<>(); void setParam(int param) { paramMap.put(this, param); } @Override WeakHashMap<Function1D, Integer> getParamMap() { return paramMap; } @Override double doSomething() { // elided } } 
0
source

Source: https://habr.com/ru/post/986594/


All Articles