我是 Spring 的新手,我会将我的类接口“自动装配”到它在外部 jar 中的实现类。
这是一个罐子的代码:
@Component
public class MongoSpring implements IMongo {
MongoSpring() throws UnknownHostException {
System.out.println("mongo template builder");
}
@Required
public void save() {
System.out.println("mongo template save");
}
}
类接口:
@Component
public interface IMongo {
void save();
}
我制作自动连线的课程
@Component
public class Core {
@Autowired
public IMongo db ;
public void run () {
log.info("core ");
db.save();
}
主要班
public class MongoApp {
private static ApplicationContext context;
public static void main(String[] args) throws Exception {
context = new ClassPathXmlApplicationContext("path.xml");
Core c = (Core)context.getBean(Core.class);
c.run();
}
}
豆文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="org.spring.mongodb.example" />
</bean>
</beans>
当我运行它给我以下错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'core': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.spring.mongodb.example.IMongo org.spring.mongodb.example.Core.db;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.spring.mongodb.example.IMongo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
[...]
我能做些什么?我正在使用带有 STS 的 spring 3.2.0
我已经从界面和@Require 中删除了@Component,但没有改变。Jar 文件在类路径中,并且包是正确的。谢谢