Java 如果所有类不在同一个包中,Spring@autowired将不起作用
我有四个包裹:Java 如果所有类不在同一个包中,Spring@autowired将不起作用,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有四个包裹: com.spring.org 文件:HomeController.java com.spring.org.dao 文件:SubscriberDao.java,SubscriberDaoImpl.java com.spring.org.model 文件:Subscriber.java com.spring.org.service 文件:SubscriberService.java,SubscriberServiceImpl.java 我将所有控制器类放在com.spring.org
HomeController.java
SubscriberDao.java
,SubscriberDaoImpl.java
Subscriber.java
SubscriberService.java
,SubscriberServiceImpl.java
@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" />
你能告诉我怎么解决这个问题吗
如果您需要查看任何其他文件,请告诉我
更新
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);
}
@服务
公共接口订阅服务{
公共无效添加订阅方(订阅方);
public void updateSubscriber(订户);
公共订户getSubscriberById(int subId);
公共列表listSubs();
公共int-removeSubscriber(int-subId);
}
根本原因
org.springframework.beans.factory.BeanCreationException:创建名为“homeController”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private com.spring.service.SubscriberService com.spring.org.HomeController.subService;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[com.spring.service.SubscriberService]的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true),@org.springframework.beans.factory.annotation.Qualifier(value=)}
org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.PostProcessPropertyValue(AutoWiredNotationBeanPostProcessor.java:292)
文件夹
如果我把我所有的代码都粘贴在这里,那就很难理解了,所以我把我的整个项目都上传到了这里。我正在使用Eclipse IDE。您只需要指定基本包:
<context:component-scan base-package="com.spring.org"/>
我认为您应该注释实现类而不是接口。问题是您有多个SubscriberService
接口的实现
当您编写以下代码时:
@Autowired
private SubscriberService subService;
Spring将寻找SubscriberService的一个实现,因为您将拥有多个实现,所以Spring不知道要注入哪个实现
解决方法是使用@Qualifier
来区分不同的实现
有关@Qualifier
的更多信息和演示,请访问链接
或者,如果您正在为SubscriberService
使用单个实现,请确保服务和实现都属于您为spring上下文中的扫描提供的包
希望有帮助。尝试用逗号分隔包,如下所示:
<context:component-scan
base-package="com.spring.org,com.spring.org.dao,com.spring.org.model,com.spring.org.service" />
尝试在SubscriberServiceImpl上添加@Component
基本上是@Service、@Repository、@Component等注释。它们都有相同的用途:
使用基于注释的配置和类路径时自动检测
扫描
根据我的经验,我总是在接口上使用@Service注释,或者在抽象类和注释上使用@Component和@Repository来实现它们@我在那些服务于基本目的的类上使用的组件注释,简单的Springbeans,仅此而已@我在DAO层中使用的存储库注释,例如,如果我必须与数据库通信,进行一些事务等。如下指定基本扫描,并从接口中删除注释,仅保留在实现类中,例如@Service、@Repository、@Component等
<context:component-scan base-package="com.spring.org"/>
编辑:
我查看了您的代码。您已将组件扫描作为
但是您的SubscriberService.java在包com.spring.service中。请将包更改为com.spring.org.service。您的结构应该是这样的
订阅服务接口
package com.spring.org.service;
public interface SubscriberService {
}
SubscriberServiceImpl.java
package com.spring.org.service;
@Component
@Qualifier("Subscriber")
public class SubscriberServiceImpl implements SubscriberService {
}
package com.spring.org.service;
@Component
@Qualifier("Subscriber1")
public class SubscriberServiceImpl1 implements SubscriberService {
}
@Controller
public class HomeController {
@Autowired
@Qualifier("Subscriber")
private SubscriberService subService;
}
“SubscriberServiceImpl1”是一个组件,它实现“SubscriberService”
SubscriberServiceImpl1.java
package com.spring.org.service;
@Component
@Qualifier("Subscriber")
public class SubscriberServiceImpl implements SubscriberService {
}
package com.spring.org.service;
@Component
@Qualifier("Subscriber1")
public class SubscriberServiceImpl1 implements SubscriberService {
}
@Controller
public class HomeController {
@Autowired
@Qualifier("Subscriber")
private SubscriberService subService;
}
我设置了一个Spring上下文,用于扫描这两个包,查找标有“@Component”的bean
servlet context.xml
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.spring"/>
请参阅此。
希望这能帮助你
编辑
根据您的包结构,您的SubscriberServiceImpl类位于包com.spring.org.service下,只需使用com.spring更改您的基本包即可
<context:component-scan base-package="com.spring"/>
首先,您必须将此标记放入XML(应用程序上下文文件)中:
因为Spring搜索类型为SubscriberService
(在您的例子中)的bean,如果找到这样的bean,它会将其注入到这个方法中。如果它找到两个这样的bean,您将得到一个异常(在
@Resource(name="redBean")
private SubscriberService subService