【发布时间】:2021-10-07 05:34:13
【问题描述】:
我有一个实现 interfaceA 的 classA,带有一个 methodA,然后我有一个 classB,我在其中调用带有 @Autowired 的 classA 以便能够使用 methodA,但它给了我一个警告,我必须为它创建一个方法A类。为什么会这样?在这种情况下,@Autowired 不是这样工作的吗?我应该只实例化classA吗?非常感谢您的回答。
A类
@RequiredArgsConstructor
public class RepositoryImpl implements IRepository {
@Autowired
private final TransactionDataMapper transactionDataMapper;
@Autowired
private SpringDataColminvoice springDataColminvoice;
@Override
public <S extends TransactionDto> S save(S s) {
Colm colm = transactionDataMapper.toEntity(s);
//methodA
springDataColminvoice.save(colm);
return null;
}
}
接口A
public interface IRepository extends IRepository<TransactionDto, Integer> {}
B类
@Service
@RequiredArgsConstructor
public class ServiceImpl implements IInvoiceService {
@Autowired
private RepositoryImpl repositoryImpl;
@Override
public void save(CMessage cMessage) throws HandlerException {
try {
TransactionDto transactionDto = cMessage.getTransaction();
// methodA
repositoryImpl.save(transactionDto);
} catch (Exception e) {
throw new HandlerException(e.getMessage());
}
}
}
例外
Action:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field RepositoryImpl in com.st.ms.yyu.d.binvoce.infraestructure.rest.spring.services.impl.InvoiceServiceImpl required a bean of type 'com.st.ms.yyu.d.binvoce.infraestructure.db.springdata.repository.impl.ServiceImpl' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.st.ms.yyu.d.binvoce.infraestructure.db.springdata.repository.impl.RepositoryImpl' in your configuration.
【问题讨论】:
-
能否请您编写示例代码和收到的警告?
-
确保methodA有一个公共访问器,并且一定要添加代码示例
-
@geobreze 我已经添加了代码,谢谢。
-
@DmitriiBykov 我已经添加了代码,谢谢。
-
很难阅读您发布的代码,但您的 A 类上可能缺少
@Service注释。另外,您的 Spring 上下文是如何加载的?
标签: java spring spring-boot