时才出现错误
CommentRepository.java
import julian.gonzalezlopez.aop.POJO.Comment;
import org.springframework.stereotype.Service;
@Service
public class CommentRepository implements CommentRepositoryInterface {
public void pie(Comment comment){
System.out.println(comment.getText());
System.out.println("Escrito por: " + comment.getOwner());
}
}
main.java
import julian.gonzalezlopez.aop.Comment.CommentRepository;
import julian.gonzalezlopez.aop.Comment.CommentRepositoryInterface;
import julian.gonzalezlopez.aop.POJO.Comment;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(ProjectConfig.class);
System.out.println(c);
CommentRepositoryInterface commentRepository = c.getBean(CommentRepository.class);
Comment comment = new Comment("hola","julian");
commentRepository.pie(comment);
}
}
在我们解决您的主要问题之前,一些提示: 您应该遵守命名惯例,例如不使用大写软件包名称,因为它们很容易与班级名称混淆。
Aspect
,但您可能要使用
Aspecto
LoggingAspect
.
。在您的屏幕截图中,我可以看到一个名为
CommentService
的类,但是在您的示例代码中,您使用classCommentRepository
带有@Service
注释。这完全令人困惑。你甚至还记得自己为什么这样做吗?
@Configuration
@EnableAspectJAutoProxy
public class ProjectConfig {}
I.E。,它缺少
@ComponentScan
注释。 其他可能性是您实际上确实添加了注释,但仍然得到同样的例外。在这种情况下,您可以修复主类,搜索接口类的豆子,...
CommentRepositoryInterface commentRepository = c.getBean(CommentRepositoryInterface.class);
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan
public class ProjectConfig {}
我热烈建议使用第一个选项,因为从设计的角度来看,它是清洁的。您已经拥有界面及其实现,这是一个干净的设计。利用它,不要围绕它工作。