Spring Data Project - Couchbase Integration - java

I went over the below tutorials for using couchbase db via Spring
http://projects.spring.io/spring-data-couchbase/#quick-start
I see the below auto wiring in service class. UserRepository is an interface. I assume there should be an implementation which implements this interface and is exposed a bean. I don't see any class implementing this interface or being exposed as a bean. Is it possible to help in explaining how this works?
#Autowired
public MyService(UserRepository userRepository) {
this.userRepository = userRepository;
}

The aim of the Spring-Data project is to create such implementations at runtime for you when you only define interfaces.
It will inspect the packages you configured for scan via reflection and discover xxRepository interfaces in them, at which point it will use a base class provided by the store-specific Spring Data subproject you chose (here Couchbase) to weave together a concrete implementation of xxRepository and inject it.

Related

When to use dependency Injection in spring mvc?

I am working on a Spring MVC project where I am dealing with different types of services,Repositories i.e classes annotated with #Service and #Repository. I am confused with a couple of questions:
When to use #AutoWired annotation?
I have seen various repositories using this:
CourseRepository crepo=new CourseRepository();
and I have seen this also
#AutoWired
private CourseRepository crepo;
Which one of the above options should be used to get an instance of
repository in Service class?
Can I use #AutoWired for classes which are not annotated with #Repository or
#Service?
I am a beginner in this java world.Any help will be highly appreciated.
Thanks
You use new for data objects, which in most modern architectures are passive (they're not "active records"). Everything else is a service object, and you should inject those. (The one place that you do use new is with an #Bean method, which is a "factory" that creates the service object; in this case you normally pass the dependencies as method parameters.)
Note that it is recommended to use constructor injection instead of field injection; it makes your code easier to test, and it eliminates the possibility of certain kinds of errors. In fact, if using constructor injection, it's not required to have any Spring annotations in your service classes at all; beans can be registered using #Import instructions or #Bean methods on a configuration class.
You should #Autowire the dependencies instead of instantiating it yourself. Doing so, service and repo layer will be loosely coupled. Moreover, a mock repository can be easily injected in service's JUnit test class if dependency is autowired. To conclude, use below:
#Autowired
private CourseRepository crepo;
A class not annotated with any of below stereotype annotations will not be in Spring's IoC (Inversion of Control) container. Hence, no point in autowiring in a class that is not annotated with any of below annotations.
#Component, #Controller, #Service, #Repository
Dependency injection means that the framework is the one who handles the classes instantiation and the object of that class is going to be injected (thanks to #Autowired annotation) in the class where you need it. In other words, you do not need to instantiate service and repository classes by yourself using new operator, you just need to tell the framework that those classes need to be injected and that's why you use #Autowired annotation.

How can I do CRUD operations through CrudRepository in Spring?

I'm working with PostgresSQL and I have the following interface:
#Repository
public interface ExampleRepository extends CrudRepository<ExampleEntity, Long> { }
Then I try to get the bean:
ExampleRepository repository = ctx.getBean(ExampleRepository.class);
Of course, I can't do that, because there's no implementation and eventually I get
NoSuchBeanDefinitionException: No qualifying bean of type 'ExampleRepository'
I know this is a wrong approach, but since I'm not enough experienced, I've got no idea how I can communicate with my database. Any example I searched only explained how to implement services & controllers in order to interact with db through Browser. But I want to do CRUD operation inside the java code.
Could anyone explain it to me? Any related sources would also be fine.
I am not sure how you are getting context (ctx) here.
But the common approach is #Repository is not needed instead, #EnableJPARepositories should be used in the #Configuration file. Then use #Autowired to inject the repository into your service class (where you want to execute operation from your repository bean)
You can refer below link for more details
https://mkyong.com/spring-boot/spring-boot-spring-data-jpa/
You don't need to create bean. It will created by the spring framework because you annotated your interface as #Repository .You need only #Autowired in your service class or where do you want to use this reference.
#Autowired
private ExampleRepository exampleRepository;

Running Spring integration tests with #NoRepositoryBean with no #Repository implementation

I have a situation where I have a few modules, with the primary module being datastore agnostic. There are child modules that depend on the primary module with repository implementations for JPA and Mongo.
Core-Module - All business logic, #NoRepositoryBean interfaces
Core-Mongo - Dependent on Core-Module with Mongo repositories/domain
Core-JPA - Dependent on Core-Module with JPA repositories/domain
Essentially, what I'd like to do is have integration tests in Core-Module use an H2 database to perform the tests and not be dependent on a #Repository implementation.
I realize I could make test-only #Repository interfaces, and that may be the route I have to go, but I'm wondering if there's a simpler way. Essentially, is there a way to "instantiate" a #NoRepositoryBean in tests or some other simple way to do this without having to declare test only repositories?
I just checked this interest question. I marked in my test project UserRepository by #NoRepositoryBean, after that in test i unable to autowired it. But in documentation spring-docs i found java creation repositories process description, and it's works perfect. Just before test create required repositories.
Piece of test code example:
private UserRepository userRepository;
#Autowired
private EntityManager entityManager;
#Before
public void init() {
JpaRepositoryFactory factory = new JpaRepositoryFactory(entityManager);
userRepository = factory.getRepository(UserRepository.class);
}

how to build Generic CRUD Controller using Spring Proxy & BeanPostProcessor

I'm planning to build a generic infrastructure for MVC CrudController and Generic CrudService using interface Proxies and custom annotation processing, because i have multiple endpoints in my project with the same CRUD pattern.
the bottom line is - defining interfaces of CrudController and CrudServices and create a ProxyBean for my interfaces and process cutsom annotation, i want to achieve the same pattern as spring data repositires where you need to define you repository interface only,
for example:
interface CrudController<T>{
//crud method...
}
interface CrudService<T>{
//crud method...
}
class GenericCrudControllerImpl<T>{
private CrudService<T> service;
//crud methods implementation and call service crud methods
}
the usage of this pattern would be like the following:
#CrudRestController(service=PersonCrudService.class) //PersonCrudService implements CrudService
class CrudPersonController implements CrudController<Person>{}
i know i should use Spring's FactoryBean<T> interfaces along with BeanPostProcessor, but the problem i don't know how to read the generic parameter of my CrudController, e.g. Person to be able to instansiate my generic implementations with the generic paramters, e.g. Person.
i need assistance how to use the ProxyBean pattern and BeanPostProcessor the right way to acheive the implementation above, i couldn't find any useful examples on the web.

How can I make Spring testcontext framework use multiple data sources?

I'm trying to integration test my application with Spring TestContext framework. I have done this by extending AbstractTransactionalJUnit4SpringContextTests, as usual. However, my application has three different data sources (with names like xDataSource, yDataSource, zdataSource), så when I try to run the test, the autowiring of data source in AbstractTransactionalJUnit4SpringContextTests won't work, since it looks for a Data Source with autowire-by-type, but finds three, so it does not know which one to choose.
Is there any way to get Spring TestContext Framework to use three data sources? If so; how?
OK, I figured it out. The answer to this question is twofold. Firstly, extending AbstractTransactionalJUnit4SpringContextTests won't work. This is because it needs a single data source for creating the SimpleJdbcTemplate for verifying stuff with simple JDBC queries in the test. Since I don't use this feature in this test, I could replace extends AbstractTransactionalJUnit4SpringContextTests with the collowing configuration:
#ContextConfiguration(locations = "classpath:applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class
})
#Transactional
public class IntegrationTest {
...
}
The combination of these annotations gives the same setup as extending AbstractTransactionalJUnit4SpringContextTests.
The second part was understanding that since I have three data sources, I also need all three so be referenced by the same PlatformTransactionManager. I have distributed transactions. This is impossible with a DataSourceTransactionManager, so I had to use a JtaTransactionManager.
The AbstractTransactionalJUnit4SpringContextTests class is autowired to a single data source only to allow the convenience of providing an injected JdbcTemplate object. You can override the setDataSource(DataSource dataSource) method from AbstractTransactionalJUnit4SpringContextTests in your test subclass and specify the data source to use like this:
#Resource(name = "dataSource")
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
You just have to provide the name of the one data source Spring should use for the jdbcTemplate convenience methods. If extending AbstractTransactionalJUnit4SpringContextTests is more convenient than other methods mentioned above, then you can force it to work by just choosing one of your data sources.
I found these details in Spring Jira ticket #SPR-4634.
You can define one of the data-sources as primary="true" in your xml, and it will be chosen.
If you need all threem then you cannot rely on autowiring - use ReflectionTestUtils to set it manually in your tests.

Categories