Код: Выделить всё
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {}
< /code>
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface View {}
< /code>
@View
А и вот компонент-карт-вершина :
public class ComponentScanner {
public static Set> components = ComponentScanner.scan(basePackage);
beanContainer.registerComponents(components);
for (Class componentClass : components)
System.out.println("ComponentScanner found: "+componentClass.getName());
}
public T getBean(Class tClass) {
return beanContainer.getBean(tClass);
}
}
< /code>
The issue is that this ComponentScanner.scan()[/code] работает нормально для тестовых кодов, но не в фактическом использовании ...
ВЫБОР:
Код: Выделить всё
public class App {
public static void run(Class mainClass, String[] args) {
final String BASE_PACKAGE = mainClass.getPackageName();
ApplicationContext context = new ApplicationContext(BASE_PACKAGE);
// other setting codes...
}
}
< /code>
package com.example.test;
public class Main {
public static void main(String[] args) {
App.run(Main.class, args);
}
}
@Component
public class MyComponent {}
@View
public class MyView {}
< /code>
If I run the main method, App.run()
Код: Выделить всё
ComponentScanner found: com.example.test.MyComponent
< /code>
This is my test code:
public class WebTest {
static final String BASE_PACKAGE = "com.johndoe.myproject";
static final String TEMPLATE_ROOT = "src/test/resources/templates";
static ApplicationContext context = new ApplicationContext(BASE_PACKAGE);
@Test
public void testView() throws IOException {
TestView view = context.getBean(TestView.class);
Assertions.assertNotNull(view);
Assertions.assertEquals("hello", view.hello());
}
}
< /code>
This test actually [b]passed[/b]. So, I believe this indicates that ComponentScanner.scan()
Код: Выделить всё
@View
public class TestView {
public String hello() {
return "hello";
}
}
< /code>
This is under the same package with WebTest
Я попробовал все открытые шаги, которые я сказал, и все они потерпели неудачу. Итак, мой вопрос; Почему мой тестовый код работает, но не фактический код? Я старался изо всех сил, чтобы упростить код, чтобы вы могли увидеть только Essentials
Подробнее здесь: https://stackoverflow.com/questions/795 ... n-scanning