Spring @autowired does not work if all classes are not in the same package

I have four packages:

  • com.spring.org

    Files: HomeController.java

  • com.spring.org.dao

    Files: SubscriberDao.java,SubscriberDaoImpl.java

  • com.spring.org.model

    Files: Subscriber.java

  • com.spring.org.service

    Files: SubscriberService.java,SubscriberServiceImpl.java

I put all my controller classes in com.spring.org and others in different packages based on its type. If I run my application, I get the following error message:

HTTP status 500 - Servlet.init () for the appServlet servlet throws an exception. No qualified bean of type [com.spring.org.service.SubscriberService] found for the dependency was found: at least 1 bean is expected that qualifies as an auto-connect candidate for this dependency .....

FYI: , :

@Autowired
private SubscriberService subService;

com.spring.org, .

servlet-context.xml , :

<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring.org.**" />
<context:component-scan base-package="com.spring.org.dao" />
<context:component-scan base-package="com.spring.org.model" />
<context:component-scan base-package="com.spring.org.service" />

:

<context:component-scan base-package="com.spring.org" />

servlet-context.xml http://postimg.org/image/s6bnjccrn/

, ?

, , .

Update

SubscriberService:

@Service
public interface SubscriberService {

 public void addSubscriber(Subscriber subscriber);
 public void updateSubscriber(Subscriber subscriber);
 public Subscriber getSubscriberById(int subId);
 public List<Subscriber> listSubs();
 public int removeSubscriber(int subId);    

}

org.springframework.beans.factory.BeanCreationException: bean "homeController": ; - org.springframework.beans.factory.BeanCreationException: autowire: private com.spring.service.SubscriberService com.spring.org.HomeController.subService; - org.springframework.beans.factory.NoSuchBeanDefinitionException: bean [com.spring.service.SubscriberService], : 1 bean, autwire . : {@org.springframework.beans.factory.annotation.Autowired(required = true), @org.springframework.beans.factory.annotation.Qualifier(value =)}   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues ​​(AutowiredAnnotationBeanPostProcessor.java:292)

, , https://www.mediafire.com/?crxe7vt7uwyqwtl. Eclipse IDE.

+4
7

SubscriberService

package com.spring.org.service;

public interface SubscriberService {

}

SubscriberServiceImpl.java

package com.spring.org.service;

@Component
@Qualifier("Subscriber")
public class SubscriberServiceImpl implements SubscriberService {

}

'SubscriberServiceImpl1 SubscriberService.

SubscriberServiceImpl1.java

package com.spring.org.service;

@Component
@Qualifier("Subscriber1")
public class SubscriberServiceImpl1 implements SubscriberService {

}

Spring, beans "@Component".

servlet-context.xml

<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring"/>

HomeController.java

@Controller
public class HomeController {

    @Autowired
    @Qualifier("Subscriber")
    private SubscriberService subService;

}

. , ....

SubscriberServiceImpl com.spring.org.service, com.spring. >

<context:component-scan base-package="com.spring"/>
+3

, SubscriberService.

:

@Autowired
private SubscriberService subService;

Spring SubscriberService, , spring , .

@Qualifier .

@Qualifier .

, SubscriberService, , , spring.

, .

+2

:

<context:component-scan base-package="com.spring.org"/>

, .

0

, :

<context:component-scan 
base-package="com.spring.org,com.spring.org.dao,com.spring.org.model,com.spring.org.service" />
0

@Component SubscriberServiceImpl.

, @Service, @Repository, @Component .., :

.

, @Service , @Component @Repository . @ , , Spring beans, . @Repository , DAO, . , ..

0

, . @Service, @Repository, @Component ..

<context:component-scan base-package="com.spring.org"/> 

EDIT:

.

SubscriberService.java com.spring.service. , com.spring.org.service.

0

XML ( ):

<context:component-scan base-package="com.spring.org"/>

, :

@Autowired
@Qualifier("Subscriber")
private SubscriberService subService

Spring bean SubscriberService ( ), bean, . beans, ( ).

f (@Autowired @Qualifier), @Resource :

@Resource(name="redBean")
private SubscriberService subService
0

All Articles