我是Spring的新手,我会在外部jar中“自动”我的类接口到它的实现类。
这是一个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();
}
}
bean文件:
<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文件在classpath中,包是正确的。谢谢