我有一个Facade,用于通过@Service注释公开的持久性子模块:
@EnableMongoRepositories
@Service
public class MongoFacade {
@Autowired
private ShopsRepository shopsRepository;
public List<Shop> findShopsWithin(double centerLatitude, double centerLongitude, double radiusInKm) {
List<ShopEntity> shopEntities = shopsRepository.findByLocationWithin(new Circle(centerLongitude, centerLatitude, radiusInKm/111.12));
List<Shop> shops = new ArrayList<>();
for (ShopEntity shopEntity : shopEntities) {
shops.add(new Shop(shopEntity.getPicture(), shopEntity.getName(), shopEntity.getEmail(), shopEntity.getCity(),
new Location(shopEntity.getLocation().getY(), shopEntity.getLocation().getX())));
}
return shops;
}
}
我通过@ComponentScan和@Autowired annotations将这个Facade的实例注入到域层的数据库适配器中,我希望这些注释可以作为bean公开,以便能够注入它,如下所示: / p>
@ComponentScan(basePackages = "com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.tech_services.persistence")
@Component
class MongoShopsProviderAdapter implements IShopsProviderAdapter {
@Autowired
private MongoFacade mongoFacade;
@Override
public List<Shop> findShopsWithin(GeographicalCircleArea geographicalCircleArea) {
return mongoFacade.findShopsWithin(geographicalCircleArea.getCenter().getLatitude(),
geographicalCircleArea.getCenter().getLongitude(),
geographicalCircleArea.getRadius());
}
}
然后我尝试将此适配器的实例注入域类,如下所示:
public class GeographicalCircleArea {
private Location center;
private double radius;
@Autowired
private IShopsProviderAdapter shopsProviderAdapter;
GeographicalCircleArea(double centerLatitude, double centerLongitude, double radiusInKm) {
this.center = makeCenter(centerLatitude, centerLongitude);
this.radius = radiusInKm;
}
List<Shop> getShopsWithin() {
return shopsProviderAdapter.findShopsWithin(this);
}
public Location getCenter() {
return center;
}
public double getRadius() {
return radius;
}
private Location makeCenter(double latitude, double longitude) {
return new Location(latitude, longitude);
}
}
当我在适配器实现中使用@ComponentScan和@Component时,我得到一个错误,指出即使我已经使用@Service注释,spring也无法自动装入mongo Facade。请指出我错过的内容以及Spring boot autowiring的工作方式。