Constructor Parameters
We are now dealing with constructor injection. So far all our implementations have a default constructor without any parameters. What if there are implementations with parameters in the constructor? This tutorial will show you how to do it. Also, the service module supports constructor parameters to be injected as well. You just need to make sure that parameters are injected early in the service.yml file.
Here is an interface.
public interface G {
String getName();
void setName(String name);
int getAge();
void setAge(int age);
}
And implementation
public class GImpl implements G {
String name;
int age;
public GImpl() {
}
public GImpl(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
}
Here is the service.yml binding. As you can see, we pass in two parameters to the implementation constructor.
- com.networknt.service.G:
- com.networknt.service.GImpl:
name: Sky Walker
age: 23
Here is the test case to use it.
@Test
public void testSingleWithProperties() {
G g = SingletonServiceFactory.getBean(G.class);
Assert.assertEquals("Sky Walker", g.getName());
Assert.assertEquals(23, g.getAge());
}