如何在spring中解决这个“class not found”异常?

velaa5lx  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(750)

我的spring程序中有一个错误:“class not found exception”。我如何解决它?

package com.app.sathya.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.app.sathya.bean.EmpService;
import com.app.sathya.config.AppConfig;

public class Test {
     public static void main(String[] args) {

              AnnotationConfigApplicationContext ac=
                     new AnnotationConfigApplicationContext(AppConfig.class);

              EmpService E=ac.getBean("empService",EmpService.class);
              System.out.println(E);

              ac.close();
     }
}
jdzmm42g

jdzmm42g1#

这是因为empservice没有定义,比如@service或@component,或者它不在components范围内。

ajsxfq5m

ajsxfq5m2#

所以基本上这里您需要了解javabean和springbean之间的区别。
springbean是由spring框架容器示例化、配置和管理的对象。因此,为了将任何类标记为springbean,您可以使用如下注解对其进行注解 @service, @component or @repository ,这将使spring认识到它需要管理该对象的生命周期。
尝试注解 EmpService 与…同班 @service 注解,应该可以解决问题。

相关问题