Switching to jakarta inject api elements. #2

Merged
konrad merged 1 commits from feature/use_jakarta_cdi into main 2025-12-14 14:13:30 +01:00
6 changed files with 12 additions and 73 deletions

View File

@ -1,7 +1,7 @@
package de.neitzel.injection; package de.neitzel.injection;
import de.neitzel.injection.annotation.Component; import jakarta.inject.Inject;
import de.neitzel.injection.annotation.Inject; import jakarta.inject.Singleton;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -110,14 +110,14 @@ public class ComponentScanner {
} }
/** /**
* Scans the specified base package for classes annotated with {@link Component}. * Scans the specified base package for classes annotated with {@link Singleton}.
* Identified component classes are added to a collection for further processing. * Identified component classes are added to a collection for further processing.
* *
* @param basePackage the package to scan for component annotations * @param basePackage the package to scan for component annotations
*/ */
private void scanForComponents(String basePackage) { private void scanForComponents(String basePackage) {
Reflections reflections = new Reflections(basePackage); Reflections reflections = new Reflections(basePackage);
components.addAll(reflections.getTypesAnnotatedWith(Component.class)); components.addAll(reflections.getTypesAnnotatedWith(Singleton.class));
} }
/** /**
@ -196,8 +196,8 @@ public class ComponentScanner {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
for (Class<?> clazz : resolvableNow) { for (Class<?> clazz : resolvableNow) {
Component annotation = clazz.getAnnotation(Component.class); Singleton annotation = clazz.getAnnotation(Singleton.class);
ComponentData componentInfo = new ComponentData(clazz, annotation.scope()); ComponentData componentInfo = new ComponentData(clazz, Scope.SINGLETON);
resolved.add(clazz); resolved.add(clazz);
registerComponentWithSuperTypes(componentInfo, knownTypes); registerComponentWithSuperTypes(componentInfo, knownTypes);

View File

@ -1,37 +0,0 @@
package de.neitzel.injection.annotation;
import de.neitzel.injection.Scope;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that an annotated class is a "component" within a dependency injection framework.
* Classes annotated with {@code @Component} are recognized during the component scanning process
* as candidates for instantiation and management by the dependency injection framework.
* <p>
* This annotation is typically used on classes that represent application-specific components,
* such as service implementations, controllers, or other objects intended to be instantiated
* and injected into other components during runtime.
* <p>
* The annotation must be placed at the type level, and it is retained at runtime
* to allow for reflection-based scanning of classes within specified packages.
* <p>
* Usage of this annotation assumes that there exists a component scanning mechanism
* that processes annotated classes and identifies their roles, dependencies, and hierarchy
* within the application's structure.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
/**
* Defines the scope of a component within the dependency injection framework.
* The scope determines whether the component is instantiated as a singleton or
* as a prototype.
*
* @return the scope of the component, defaulting to {@code Scope.SINGLETON}.
*/
Scope scope() default Scope.SINGLETON;
}

View File

@ -1,24 +0,0 @@
package de.neitzel.injection.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a field is a candidate for dependency injection within
* a dependency injection framework. Fields annotated with {@code @Inject}
* are automatically populated with the required component instance during runtime,
* typically by the dependency injection container.
* <p>
* This annotation must be applied at the field level and is retained at runtime
* to enable reflection-based identification and assignment of dependencies.
* <p>
* The framework's dependency resolution mechanism identifies the appropriate
* instance to inject based on the field's type or custom configuration,
* ensuring loose coupling and easier testability.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}

View File

@ -1,8 +1,8 @@
package de.neitzel.injection.testcomponents.test1ok; package de.neitzel.injection.testcomponents.test1ok;
import de.neitzel.injection.annotation.Component; import jakarta.inject.Singleton;
@Component @Singleton
public class TestComponent1_1 extends SuperClass implements TestInterface1_2 { public class TestComponent1_1 extends SuperClass implements TestInterface1_2 {
public TestComponent1_1() { public TestComponent1_1() {
} }

View File

@ -1,11 +1,11 @@
package de.neitzel.injection.testcomponents.test1ok.sub; package de.neitzel.injection.testcomponents.test1ok.sub;
import de.neitzel.injection.annotation.Component;
import de.neitzel.injection.testcomponents.test1ok.SuperClass; import de.neitzel.injection.testcomponents.test1ok.SuperClass;
import de.neitzel.injection.testcomponents.test1ok.TestInterface1_1; import de.neitzel.injection.testcomponents.test1ok.TestInterface1_1;
import de.neitzel.injection.testcomponents.test1ok.TestInterface1_2; import de.neitzel.injection.testcomponents.test1ok.TestInterface1_2;
import jakarta.inject.Singleton;
@Component @Singleton
public class TestComponent1_2 extends SuperClass implements TestInterface1_1, TestInterface1_2 { public class TestComponent1_2 extends SuperClass implements TestInterface1_1, TestInterface1_2 {
public TestComponent1_2() { public TestComponent1_2() {
} }

View File

@ -1,11 +1,11 @@
package de.neitzel.injection.testcomponents.test2fail; package de.neitzel.injection.testcomponents.test2fail;
import de.neitzel.injection.annotation.Component; import jakarta.inject.Singleton;
/** /**
* TestComponent1 that should fail. * TestComponent1 that should fail.
*/ */
@Component @Singleton
public class TestComponent2_1 { public class TestComponent2_1 {
public TestComponent2_1(String test) { public TestComponent2_1(String test) {
} }