Home > other >  Is it possible to register with Container without using @Repository or @Bean?
Is it possible to register with Container without using @Repository or @Bean?

Time:04-22

public interface AccountRepository extends CrudRepository<AccountDBModel, Long> {

  @Modifying
  @Query(value = PortfolioQuery.ACCOUNT_INSERT)
  void insert(@Param("exchangeId") Long exchangeId, @Param("name") String name, @Param("siteAccount") String siteAccount,
      @Param("memo") String memo, @Param("createdAt") Long createdAt, @Param("updatedAt") Long updatedAt,
      @Param("isActive") Boolean isActive);

  @Modifying
  @Query(value = PortfolioQuery.ACCOUNT_UPDATE)
  void update(@Param("id") Long id, @Param("exchangeId") Long exchangeId, @Param("name") String name,
      @Param("siteAccount") String siteAccount, @Param("memo") String memo, @Param("updatedAt") Long updatedAt,
      @Param("isActive") Boolean isActive);

  @Query
  Optional<AccountDBModel> findByName(@Param("name") String name);
}
@Service
public class AccountService {

  private final AccountRepository repository;

  @Autowired
  public AccountService(AccountRepository repository) {
    this.repository = repository;
  }

  public void postAccount(AccountBaseModel baseModel) throws Exception {
    Long now = System.currentTimeMillis();
    this.repository.insert(baseModel.getExchangeId(), baseModel.getName(), baseModel.getSiteAccount(),
        baseModel.getMemo(), now, now, baseModel.getIsActive());
  }
}
@SpringBootTest
class WaveBackofficeApiApplicationTests {

  @Autowired
  private ApplicationContext applicationContext;

  @Test
  public void contextLoads() throws Exception {
    if (applicationContext != null) {
      String[] beans = applicationContext.getBeanDefinitionNames();

      for (String bean : beans) {
        System.out.println("bean : "   bean);

      }
    }
  }
}

bean : accountRepository

As you can see in AccountRepository interface I didn't use @Repository in AccountRepository interface.

But why is it registered as a bean in Spring Container?

There are no other class like AppConfig.

CodePudding user response:

The interface itself is not registered as a bean. spring framework provides existing implementation of a repository bean, which gets injected based on the specifications you provide in your interface.

In case you want to create your own implementation of a repository class, then you would need to annotate the class with @Repository in order for spring boot to identify it as a bean.

an example would be like this:

@Service
public class MyServiceImpl implements MyService {
    // code
}

public interface MyService {}


@RestController
public MyController() {
    @Autowired private MyService myService;
}

In the example above, we have our MyServiceImpl which is of type MyService, and the implemented class is registered as a bean. In the controller, we just tell that we want a bean of type MyService. Spring will inject MyServiceImpl since it matches the requirements. This is analogue to your example with repository.

CodePudding user response:

You created interface called AccountRepository and extended (thus inherited) CrudRepository.

Now just do Ctrl Left mouse click on CrudRepository, you will end up in it:

@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
    <S extends T> S save(S entity);

    <S extends T> Iterable<S> saveAll(Iterable<S> entities);

    Optional<T> findById(ID id);

    boolean existsById(ID id);

    Iterable<T> findAll();

    Iterable<T> findAllById(Iterable<ID> ids);

    long count();

    void deleteById(ID id);

    void delete(T entity);

    void deleteAllById(Iterable<? extends ID> ids);

    void deleteAll(Iterable<? extends T> entities);

    void deleteAll();
}

Intellij actually gives you oportunity to find the implementations of all those methods with arrow down mark on the left side. Look

So there is a huge class called SimpleJpaRepository that has all the implementations, the actual code.

AND THE THING IS...

SimpleJpaRepository.class does have @Repository in it:

@Repository
@Transactional(
    readOnly = true
)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID> {
  • Related