【发布时间】:2020-01-16 05:28:10
【问题描述】:
我编写了一组简单的类来向朋友展示如何使用 Annotations for AOP(而不是 xml 配置)。我们无法让 @ComponentScan 工作,并且 AnnotationConfigApplicationContext getBean 行为也很不正常。我想了解两件事。请参阅下面的代码:
PersonOperationsI.java
package samples.chapter3;
import org.springframework.stereotype.Component;
@Component
public interface PersonOperationsI {
public String getName();
}
PersonOperations.java
/**
*
*/
package samples.chapter3;
import org.springframework.stereotype.Component;
@Component
public class PersonOperations implements PersonOperationsI {
public String getName() {
return "";
}
}
PersonOperationsConfigClass.java
package samples.chapter3;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
//question2 - Below Component Scan didnt work - Test Case failing in setup()
//@ComponentScan(basePackages = {"samples.chapter3"})
@EnableAspectJAutoProxy
public class PersonOperationsConfigClass {
}
PersonOperationsAdvice.java
/**
*
*/
package samples.chapter3;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class PersonOperationsAdvice {
/**
* execution( [Modifiers] [ReturnType] [FullClassName].[MethodName]
([Arguments]) throws [ExceptionType])
* @param joinPoint
* @return
*/
@Before("execution(public * samples.chapter3.PersonOperations.getName()))")
public String beforeGetName(JoinPoint joinPoint) {
System.out.println("method name = " + joinPoint.getSignature().getName());
return null;
}
}
PersonOperationsTest.java
package samples.chapter3;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { PersonOperationsConfigClass.class })
public class PersonOperationsTest {
//@Autowired
private PersonOperationsI obj;
@Before
public void setUp() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("samples.chapter3");
ctx.refresh();
obj = ctx.getBean(PersonOperationsI.class);
//obj = ctx.getBean(PersonOperations.class);//getBean of Child class not working - why ?
Assert.assertNotNull(obj);
ctx.close();
}
@Test
public void test() {
System.out.println(obj.getName());
}
}
问题 1 - 为什么 @componentscan 不起作用。如果我不在测试用例中使用 AnnotationConfigApplicationContext 而只依赖 @componentscan & autowired - 测试用例中的对象为空
Question2 - ctx.getBean(PersonOperations.class);//子类的getBean不起作用 - 为什么?
【问题讨论】:
-
您确定要使用
@Component注释您的界面吗? -
这只是一个试用版 - 我删除了它..
标签: java spring spring-aop