JAR SPRING @autowired

时间:2018-05-15 06:26:17

标签: spring jar

我创建了一个java存档,我希望将它集成到我的应用程序中。

在我的主要申请表上:

ApplicationContext.xml:

<context:component-scan base-package="com.test.chomage" />

ChomageController.java:

@Controller
@Path("/chomage")
public class ChomageController {

   @Autowired
   ChomageService chomageService;

   @Autowired
   OrganisationService organisationService;
}

在我的java档案中:

appConfig.java

@Configuration
@ComponentScan("com.test.jarPatrimoine")
public class AppConfig {

}

OrganisationService.java

public interface OrganisationService {
  //Functions
}

OrganisationServiceImpl.java

@Service
@Transactional
public class OrganisationServiceImpl implements OrganisationService 
  //Functions
}

当我启动我的tomcat服务器时,我有下一个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'chomageController': 
Unsatisfied dependency expressed through field 'organisationService';
nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 
'com.test.jarPatrimoine.service.OrganisationService' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency 
annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

你能帮助我吗?

由于

2 个答案:

答案 0 :(得分:0)

是否还有其他类实施 OrganisationService ?如果需要,您需要添加@Qualifier注释以注入正确的实现。

@Autowire
     @Qualifier("thequalifyingbean") 
     OrganisationService organisationService;

也使用@Controller作为控制器类而不是@Component

答案 1 :(得分:0)

Spring正在寻找com.vnf.jarPatrimoine.service.OrganisationService,但扫描配置为@ComponentScan("com.test.jarPatrimoine")。你可能需要改变它。