[英]Can i call a method from another project that use an @Autowired?
我试图从我依赖导入的第二个项目中调用方法。
该方法包装在一个类中,该类在我的项目中使用@Bean注释实例化。 这个类里面的问题是我有一个@Autowired来获取第二个项目的另一个类,当我尝试运行它时,它无法解决此连接。
有什么叫法吗?
UPDATE
这是一个例子:
在我的项目A中,我有下一个课程:
@Component
public class Logger {
private static final Logger logger =
LoggerFactory.getLogger(Logger.class);
@Autowired
private RabbitTemplate template;
@Autowired
private DirectExchange exchange;
}
从项目B中获得具有Maven依赖项的项目A。
在项目B中,我有:
public class OperatorSdkAmqpPollTests {
private static final Logger logger =
LoggerFactory.getLogger(OperatorSdkAmqpPollTests.class);
@Autowired
private Logger logger;
}
当我运行它时,出现以下错误:
Field exchange in com.minsait.cybersec.netvote.common.core.Logger required a bean of type 'org.springframework.amqp.core.DirectExchange'
再加上在Spring Boot下运行im以便自动定义RabbitTemplate,当我在项目A中运行测试时,它会遇到问题,DirectExchange在项目A中的此类中定义:
@SpringBootApplication
public class AmqpConfig {
@Bean
public Queue worksQueue() {
return new Queue("queue");
}
@Bean
public DirectExchange exchange() {
return new DirectExchange("exchange");
}
}
在你为项目B类Logger
是不是所谓的豆,因为注释@Component
定义类Logger
项目A.范围的解决方案很简单:添加您的bean定义Logger
的XML配置文件类在Spring然后在您的项目B中将其称为Bean,并且一切正常。
因此,在MyOwnappricationContext.xml中的某个位置执行此操作:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/p"
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">
...
<bean id="logger" name="logger" class="com.minsait.cybersec.my.package.from.project.a.Logger"/>
</beans>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.