Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

Commit

Permalink
added PostConstruct to DI #54
Browse files Browse the repository at this point in the history
  • Loading branch information
Legion2 committed Jun 6, 2018
1 parent b9dca65 commit c3df63a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
public interface ServiceLocator {

/**
* Instantiate the given class and inject dependencies. The object created
* in this way will not be managed by the DI.
* Instantiate the given class and inject dependencies. The object created in
* this way will not be managed by the DI.
*
* @param serviceClass
* @return
Expand All @@ -49,4 +49,11 @@ public interface ServiceLocator {
* @param instance
*/
void inject(Object instance);

/**
* init the instance
*
* @param instance
*/
void postConstruct(Object instance);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.unistuttgart.iaas.amyassist.amy.core.di.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Declare a init method
*
* @author Leon Kiefer
*/
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(java.lang.annotation.ElementType.METHOD)
public @interface PostConstruct {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package de.unistuttgart.iaas.amyassist.amy.core.di;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
Expand All @@ -28,7 +30,9 @@
import java.util.Set;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.PostConstruct;
import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Reference;
import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Service;

Expand Down Expand Up @@ -155,8 +159,8 @@ private <T> T get(Class<T> serviceType, Deque<Class<?>> stack, Map<Class<?>, Obj
}

/**
* Resolve the dependency of a Service implementation and create an instance
* of the Service
* Resolve the dependency of a Service implementation and create an instance of
* the Service
*
* @param serviceClass
* the implementation class of a Service
Expand All @@ -176,6 +180,7 @@ private <T> T resolve(Class<T> serviceClass, Deque<Class<?>> stack, Map<Class<?>
try {
T instance = serviceClass.newInstance();
this.inject(instance, stack, resolved);
this.postConstruct(instance);

resolved.put(serviceClass, instance);
stack.pop();
Expand Down Expand Up @@ -203,6 +208,19 @@ private void inject(Object instance, Deque<Class<?>> stack, Map<Class<?>, Object
}
}

@Override
public void postConstruct(Object instance) {
Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(instance.getClass(), PostConstruct.class);
for (Method m : methodsWithAnnotation) {
try {
m.invoke(instance);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private Class<?> getRequired(Class<?> serviceType) {
if (!this.register.containsKey(serviceType))
throw new ServiceNotFoundException(serviceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.PostConstruct;
import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Reference;
import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Service;
import de.unistuttgart.iaas.amyassist.amy.core.plugin.api.IStorage;
Expand Down Expand Up @@ -288,11 +289,12 @@ protected boolean editAlarm(String specificAlarm, String alarmTime) {
}

/**
* Init method for logic class. TODO call die method from DI
* Init method for logic class.
*
* @param core
* The core
*/
@PostConstruct
public void init() {
if (!this.storage.has(ALARMCOUNTER))
this.storage.put(ALARMCOUNTER, "0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class AlarmClockTest {
public void setup() {
this.mockService = this.framework.mockService(TaskSchedulerAPI.class);
this.acl = this.framework.setServiceUnderTest(AlarmClockLogic.class);
this.acl.init();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public <T> T mockService(Class<T> serviceType) {
public <T> T setServiceUnderTest(Class<T> serviceClass) {
if (serviceClass.isAnnotationPresent(Service.class)) {
this.dependencyInjection.register(serviceClass);
return this.dependencyInjection.create(serviceClass);
T instance = this.dependencyInjection.create(serviceClass);
this.dependencyInjection.postConstruct(instance);
return instance;
}
throw new RuntimeException();
}
Expand Down

0 comments on commit c3df63a

Please sign in to comment.