I've created a utilities class that will house some functionality that doesn't belong in standard controllers, such as syncing and ingesting API data. I have a very simple Repository class that extends CrudRepository
:
//TransactionCategoryRepository.java
@Repository
public interface TransactionCategoryRepository extends CrudRepository<TransactionCategory, Integer> {
}
Then I have a service class which is also very straightforward at the moment:
//TransactionCategoryService.java
@Service
public class TransactionCategoryService {
private TransactionCategoryRepository transactionCategoryRepository;
@Autowired
public TransactionCategoryService(TransactionCategoryRepository repository) {
this.transactionCategoryRepository = repository;
}
public void saveTransactionCategory(TransactionCategory transactionCategory) {
transactionCategoryRepository.save(transactionCategory);
}
My utilities class leverages the TransactionCategoryService
and I've attempted to include it with @Autowired
:
//Utilities.java
@Controller
public class PlaidUtilities {
private Logger logger = LoggerFactory.getLogger(PlaidUtilities.class.getSimpleName());
private PlaidClient mPlaidClient;
@Autowired
TransactionCategoryService mTransactionCategoryService;
@Autowired
TransactionRepository mTransactionCategoryRepository;
....
When I use mTransactionCategoryService
to save a new entity, I get the null pointer exception. IntelliJ previously threw warnings on my @Autowired
annotations that the member classes needed to be declared in spring beans. Adding the @Controller
annotation made these go away, though I'm not sure if that is the right thing to do; I tried this because this is the only difference between this class and another class which @Autowires in exactly the same way but does not have this problem.
The other classes that use Autowired in this way are in a package called controllers
which is in the root directory. This Utilities class is in a package called util
which is also in the root directory.
What have I done wrong in this setup/configuration?