Field vehicleRepository required a bean of type ..VehicleInterface that could not be found - rest

I'm trying to #autowire repository reference from Service implementation class but still I'm getting bean not found error
I've already tried moving App.java file in the parent
App.java
package com.trucker;
import org.springframework.context.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App
{
public static void main( String[] args ){
ApplicationContext applicationContext = SpringApplication.run(App.class,args);
}
}
VehicleService.java
package com.trucker.service;
import com.trucker.entity.Vehicle;
import com.trucker.repository.VehicleRepositoryInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class VehicleService implements VehicleServiceI {
#Autowired
VehicleRepositoryInterface vehicleRepository;
#Override
public Vehicle addVehicles(Vehicle vehicle) {
System.out.println(vehicle);
return vehicleRepository.save(vehicle);
}
}
VehicleRepositoryInterface.java
package com.trucker.repository;
import com.trucker.entity.Vehicle;
import org.springframework.context.annotation.Bean;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
public interface VehicleRepositoryInterface extends CrudRepository<Vehicle,String> {
}
VehicleController.java
package com.trucker.controller;
import com.trucker.entity.Vehicle;
import com.trucker.service.VehicleServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class VehicleController {
#Autowired
VehicleServiceI vehicleService;
#RequestMapping(path = "/vehicles", method = RequestMethod.PUT)
public String addVehicles(#RequestBody Vehicle vehicle) {
System.out.println("************IN THE CONTROLLER************"+vehicle);
vehicleService.addVehicles(vehicle);
return "";
}
}
Vehicle.java
package com.trucker.entity;
import javax.persistence.Entity;
import java.sql.Timestamp;
import javax.persistence.Id;
#Entity
public class Vehicle {
#Id
String vin;
String make;
String model;
int year;
int redlineRpm;
int maxFuelVolume;
Timestamp lastServiceDate;
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getRedlineRpm() {
return redlineRpm;
}
public void setRedlineRpm(int redlineRpm) {
this.redlineRpm = redlineRpm;
}
public int getMaxFuelVolume() {
return maxFuelVolume;
}
public void setMaxFuelVolume(int maxFuelVolume) {
this.maxFuelVolume = maxFuelVolume;
}
public Timestamp getLastServiceDate() {
return lastServiceDate;
}
public void setLastServiceDate(Timestamp lastServiceDate) {
this.lastServiceDate = lastServiceDate;
}
#Override
public String toString() {
return "Vehicle{" +
"vin='" + vin + '\'' +
", make='" + make + '\'' +
", model='" + model + '\'' +
", year=" + year +
", redlineRpm=" + redlineRpm +
", maxFuelVolume=" + maxFuelVolume +
", lastServiceDate=" + lastServiceDate +
'}';
}
}
application.properties
application.properties
server.port=8084
spring.datasource.url=jdbc:oracle:thin:#localhost:1521:xe
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.datasource.driver-class=oracle.jdbc.driver.OracleDriver
spring.datasource.username=system
spring.datasource.password=system
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
Directory Structure:
src
src/main
src/main/resources/application.properties
src/main/java
src/main/java/com.trucker
src/main/java/com.trucker/App.java
src/main/java/com.trucker.controller
src/main/java/com.trucker.controller.VehicleController
src/main/java/com.trucker.entity.Vehicle
src/main/java/com.trucker.repository
src/main/java/com.trucker.repository.VehicleRepositoryInterface
src/main/java/com.trucker.service
src/main/java/com.trucker.service.vehicleService

Take a look at this post it has clear answers
Spring boot autowiring an interface with multiple implementations
or
Spring autowire interface

Related

Multiple MongoDB Connectors with SpringBoot REST api

I have three databases and I used MongoTemplate to have possibility connect to these databases from my application.
but when I use for example POST Method in Postman, I have error 404, not found.
In the console anything is fine and when I try http://localhost:8080/, also I have this error There was an unexpected error (type=Not Found, status=404).
No message available.
I searched too much and read so many things for example like https://dzone.com/articles/multiple-mongodb-connectors-with-spring-boot, but I could not undesrtood how can I define multiple MongoDB Connectors and fix this problem.
The MultipleMongoConfige:
package spring.mongo.thesis.config;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClient;
import lombok.RequiredArgsConstructor;
#Configuration
#RequiredArgsConstructor
#EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {
private final MultipleMongoProperties mongoProperties = new MultipleMongoProperties();
#Primary
#Bean(name = "primaryMongoTemplate")
public MongoTemplate primaryMongoTemplate() throws Exception {
return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
}
#Bean(name = "secondaryMongoTemplate")
public MongoTemplate secondaryMongoTemplate() throws Exception {
return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
}
#Bean(name = "thirdMongoTemplate")
public MongoTemplate thirdMongoTemplate() throws Exception {
return new MongoTemplate(thirdFactory(this.mongoProperties.getThird()));
}
#Bean
#Primary
public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
mongo.getDatabase());
}
#Bean
public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
mongo.getDatabase());
}
#Bean
public MongoDbFactory thirdFactory(final MongoProperties mongo) throws Exception {
return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
mongo.getDatabase());
}
}
The PrimaryMongoConfig:
package spring.mongo.thesis.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
#Configuration
#EnableMongoRepositories(basePackages = "spring.mongo.thesis.repository.primary",
mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {
}
The MultipleMongoProperty:
package spring.mongo.thesis.config;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
#ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {
private MongoProperties primary = new MongoProperties();
private MongoProperties secondary = new MongoProperties();
private MongoProperties third = new MongoProperties();
public MongoProperties getPrimary() {
return primary;
}
public void setPrimary(MongoProperties primary) {
this.primary = primary;
}
public MongoProperties getSecondary() {
return secondary;
}
public void setSecondary(MongoProperties secondary) {
this.secondary = secondary;
}
public MongoProperties getThird() {
return third;
}
public void setThird(MongoProperties third) {
this.third = third;
}
}
The PlayerController:
package spring.mongo.thesis.controller.primary;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.mongo.thesis.repository.primary.PlayerModel;
import spring.mongo.thesis.repository.primary.PlayerRepository;
#RestController
#RequestMapping("/player")
public class PlayerController {
#Autowired
private PlayerRepository repo;
//Getting Player ID
#GetMapping("/{nickname}")
public ResponseEntity<?> getPlayerByID(#PathVariable String nickname){
try {
PlayerModel p = repo.findById(nickname).get();
return ResponseEntity.ok(p);
} catch (Exception e) {
return ResponseEntity.status(404).body("Not Found!");
}
}
//Get All Players
#GetMapping
public List<PlayerModel> getAllPlayers() {
return repo.findAll();
}
//Delete Players
#DeleteMapping
public String deleteAllPlayers(){
repo.deleteAll();
return "Deleted!";
}
//#ExceptionHandler(MethodArgumentNotValidException.class)
//public void handleMissingParams(MissingServletRequestParameterException ex) {
// String name = ex.getParameterName();
//System.out.println(name + " parameter is missing");
// Actual exception handling
//}
//Create Player
#PostMapping
public ResponseEntity<?> createPlayer(#RequestBody #Valid PlayerModel player) {
PlayerModel findplayer = repo.findByNickname(player.getNickname());
if(findplayer != null) {
return ResponseEntity.status(409).body("Conflict!");
}
repo.save(player);
return ResponseEntity.status(201).body("Created!");
}
//Delete player By ID
#DeleteMapping("/{nickname}")
public ResponseEntity<?> deletePlayerByID(#PathVariable String nickname){
try {
PlayerModel p = repo.findById(nickname).get();
repo.deleteById(nickname);
return ResponseEntity.ok(p);
} catch (Exception e) {
return ResponseEntity.status(404).body("Not Found!");
}
}
//Update Player By ID
#PutMapping("/{nickname}")
public ResponseEntity<?> updatePlayerByID(
#PathVariable("nickname")String nickname,
#RequestBody #Valid PlayerModel player){
PlayerModel findplayer = repo.findByNickname(player.getNickname());
if(findplayer == null)
return ResponseEntity.status(404).
body("There is not Player with indicated Nickname!");
else
player.setNickname(nickname);
repo.save(player);
return ResponseEntity.ok(player);
}
}
The PlayerRepository:
package spring.mongo.thesis.repository.primary;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface PlayerRepository extends MongoRepository<PlayerModel, String>{
PlayerModel findByNickname(String nickname);
}
The PrimaryMongoConfig:
package spring.mongo.thesis.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
#Configuration
#EnableMongoRepositories(basePackages = "spring.mongo.thesis.repository.primary",
mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {
}
and my Application:
package spring.mongo.thesis.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("Hello World!");
}
}
The Player Class:
package spring.mongo.thesis.repository.primary;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
#Data
#AllArgsConstructor
#NoArgsConstructor
#Document(collection = "player")
public class PlayerModel {
#Id
#NotBlank
private String nickname;
#NotBlank
private String firstname;
#NotBlank
private String lastname;
#NotBlank
private String email;
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return "PlayerModel [nickname=" + nickname + ", firstname=" + firstname + ", lastname=" + lastname + ", email="
+ email + "]";
}
}
The application.yml:
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
mongodb:
primary:
host: localhost
port: 27017
database: player-db
secondary:
host: localhost
port: 27017
database: game-db
third:
host: localhost
port: 27017
database: score-db

Error : Field job in com.example.partioner.DemoApplication required a bean of type 'org.springframework.batch.core.Job' that could not be found

I try a spring batch partitioning database program but I have this message when I try to run the batch:
APPLICATION FAILED TO START
Description: Field job in com.example.partioner.DemoApplication required a bean of type
'org.springframework.batch.core.Job' 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
'org.springframework.batch.core.Job' in your configuration.
This is my main class :
package com.example.partioner;
import java.util.Date;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#EnableBatchProcessing
public class DemoApplication implements CommandLineRunner {
#Autowired
private JobLauncher jobLauncher;
#Autowired
private Job job;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println("STATUS STARTED===================");
JobParameters jobParameters = new JobParametersBuilder()
.addString("JobId", String.valueOf(System.currentTimeMillis()))
.addDate("date", new Date())
.addLong("time",System.currentTimeMillis()).toJobParameters();
JobExecution execution = jobLauncher.run(job, jobParameters);
System.out.println("STATUS :: "+execution.getStatus());
}
}
This my jobConfig class :
package com.example.config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import com.example.mapper.CustomerRowMapper;
import com.example.model.Customer;
import com.example.partitioner.ColumnRangePartitioner;
#Configuration
public class JobConfiguration {
#Autowired
private JobBuilderFactory jobBuilderFactory;
#Autowired
private StepBuilderFactory stepBuilderFactory;
#Autowired
private DataSource dataSource;
#Bean
public ColumnRangePartitioner partitioner()
{
ColumnRangePartitioner columnRangePartitioner = new ColumnRangePartitioner();
columnRangePartitioner.setColumn("id");
columnRangePartitioner.setDataSource(dataSource);
columnRangePartitioner.setTable("customer");
return columnRangePartitioner;
}
#Bean
#StepScope
public JdbcPagingItemReader<Customer> pagingItemReader(
#Value("#{stepExecutionContext['minValue']}") Long minValue,
#Value("#{stepExecutionContext['maxValue']}") Long maxValue)
{
System.out.println("reading " + minValue + " to " + maxValue);
Map<String, Order> sortKeys = new HashMap<>();
sortKeys.put("id", Order.ASCENDING);
MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
queryProvider.setSelectClause("id, firstName, lastName, birthdate");
queryProvider.setFromClause("from customer");
queryProvider.setWhereClause("where id >= " + minValue + " and id < " + maxValue);
queryProvider.setSortKeys(sortKeys);
JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
reader.setDataSource(this.dataSource);
reader.setFetchSize(10);
reader.setRowMapper(new CustomerRowMapper());
reader.setQueryProvider(queryProvider);
return reader;
}
#Bean
#StepScope
public JdbcBatchItemWriter<Customer> customerItemWriter()
{
JdbcBatchItemWriter<Customer> itemWriter = new JdbcBatchItemWriter<>();
itemWriter.setDataSource(dataSource);
itemWriter.setSql("INSERT INTO NEW_CUSTOMER VALUES (:id, :firstName, :lastName, :birthdate)");
itemWriter.setItemSqlParameterSourceProvider
(new BeanPropertyItemSqlParameterSourceProvider<>());
itemWriter.afterPropertiesSet();
return itemWriter;
}
#Bean
public Step slaveStep()
{
return stepBuilderFactory.get("slaveStep")
.<Customer, Customer>chunk(10)
.reader(pagingItemReader(null, null))
.writer(customerItemWriter())
.build();
}
#Bean
public Step step1()
{
return stepBuilderFactory.get("step1")
.partitioner(slaveStep().getName(), partitioner())
.step(slaveStep())
.gridSize(4)
.taskExecutor(new SimpleAsyncTaskExecutor())
.build();
}
#Bean
public Job job()
{
return jobBuilderFactory.get("job")
.start(step1())
.build();
}
}
This is my partitioner class:
package com.example.partitioner;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
public class ColumnRangePartitioner implements Partitioner
{
private JdbcOperations jdbcTemplate;
private String table;
private String column;
public void setTable(String table) {
this.table = table;
}
public void setColumn(String column) {
this.column = column;
}
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
#Override
public Map<String, ExecutionContext> partition(int gridSize)
{
int min = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") FROM " + table, Integer.class);
int max = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") FROM " + table, Integer.class);
int targetSize = (max - min) / gridSize + 1;
Map<String, ExecutionContext> result = new HashMap<>();
int number = 0;
int start = min;
int end = start + targetSize - 1;
while (start <= max)
{
ExecutionContext value = new ExecutionContext();
result.put("partition" + number, value);
if(end >= max) {
end = max;
}
value.putInt("minValue", start);
value.putInt("maxValue", end);
start += targetSize;
end += targetSize;
number++;
}
return result;
}
}
I don't understand the reason of this message and can't find a solution. I think I have put all the necessary annotations. I am a beginner and I hope you will help me.
You DemoApplication is in package com.example.partioner while your job configuration class JobConfiguration is in package com.example.config.
In order for Spring Boot to find your job, you need to move your JobConfiguration class to the same package as your main class DemoApplication or a package underneath it.
Please refer to the Structuring Your Code section of the reference documentation.

Pointcut matching an annotation's parameter value

Suppose I have an annotation as following:
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface DBOperation
{
boolean isReadOperation() default true;
}
Then in the Aspect, how could I want to write two pointcuts, one for all the method annotated with #DBOperation(isReadOperation=true) and one for #DBOperation(isReadOperation=false)?
The syntax is actually pretty straightforward. Here is an MCVE:
Marker annotation:
package de.scrum_master.app;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
#Retention(RUNTIME)
#Target(METHOD)
public #interface DBOperation {
boolean isReadOperation() default true;
}
Driver application:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
Application application = new Application();
application.doSomething();
application.readValue("name");
application.writeValue("name", "John Doe");
}
public void doSomething() {}
#DBOperation
public int readValue(String fieldName) {
return 11;
}
#DBOperation(isReadOperation = false)
public String writeValue(String fieldName, Object value) {
return "dummy";
}
}
Aspect:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class DBOperationAspect {
#Before("execution(#de.scrum_master.app.DBOperation(isReadOperation=true) * *(..))")
public void interceptRead(JoinPoint joinPoint) throws Throwable {
System.out.println("Read operation: " + joinPoint);
}
#Before("execution(#de.scrum_master.app.DBOperation(isReadOperation=false) * *(..))")
public void interceptWrite(JoinPoint joinPoint) throws Throwable {
System.out.println("Write operation: " + joinPoint);
}
}
Console log:
Read operation: execution(int de.scrum_master.app.Application.readValue(String))
Write operation: execution(String de.scrum_master.app.Application.writeValue(String, Object))

Supporting Multiple data sources of Mongo using Spring data

I am trying to configure two different mongo data sources using spring data.According to my requirement I need to change my scanning of base package dynamically at run time. The user can select the data source for a particular entity via property file by providing datasource and entity mapping.
Eg: entity1:dataSource1, entity2:dataSource2. Depending on this mapping I need to package names and need to replace in the #EnableMongoRepositories(basePackages="xxx.xxx.xxxx") at run time. I tired a lot using both Xml configuration and java configuration.But I could not find a possible way.Could some one please provide a solution for the problem if it is there.I am pasting the Entity classes ,Repositories and Configuration both XML and Java Config
package com.account.entity;
import java.io.Serializable;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "SampleAccount")
public class Account {
#Id
private String acntName;
#Indexed
private Date startDate;
#Indexed
private String accntType;
private Long acntId;
private Byte byteField;
public String getAcntName() {
return this.acntName;
}
public void setAcntName(String acntName) {
this.acntName = acntName;
}
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getAccntType() {
return this.accntType;
}
public void setAccntType(String accntType) {
this.accntType = accntType;
}
public Long getAcntId() {
return this.acntId;
}
public void setAcntId(Long acntId) {
this.acntId = acntId;
}
public Byte getByteField() {
return this.byteField;
}
public void setByteField(Byte byteField) {
this.byteField = byteField;
}
}
public interface AccountRepository extends MongoRepository<Account, String>{
public Page<Account> findByAcntNameOrStartDate(String acntName, Date startDate, Pageable pageable);
public Page<Account> findByAccntTypeOrStartDate(String accntType, Date startDate, Pageable pageable);
public Account findByAcntName(String acntName);
public Page<Account> findByAcntNameIn(List<String> pkIdList, Pageable pageable);
}
package com.user.entity;
import java.io.Serializable;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "SampleUser")
public class User {
#Id
private String acntName;
#Indexed
private Date startDate;
#Indexed
private String accntType;
private Long acntId;
private Byte byteField;
public String getAcntName() {
return this.acntName;
}
public void setAcntName(String acntName) {
this.acntName = acntName;
}
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getAccntType() {
return this.accntType;
}
public void setAccntType(String accntType) {
this.accntType = accntType;
}
public Long getAcntId() {
return this.acntId;
}
public void setAcntId(Long acntId) {
this.acntId = acntId;
}
public Byte getByteField() {
return this.byteField;
}
public void setByteField(Byte byteField) {
this.byteField = byteField;
}
}
package com.user.repo;
import java.util.Date;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import com.account.entity.Account;
import com.user.entity.User;
public interface UserRepository extends MongoRepository<User, String>{
public Page<Account> findByAcntNameOrStartDate(String acntName, Date startDate, Pageable pageable);
public Page<Account> findByAccntTypeOrStartDate(String accntType, Date startDate, Pageable pageable);
public Account findByAcntName(String acntName);
public Page<Account> findByAcntNameIn(List<String> pkIdList, Pageable pageable);
}
JavaConfig Files:
MongoConfig.class
package com.Config;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
#Configuration
#EnableMongoRepositories(basePackages = {"xxx.xxx.xxx"},mongoTemplateRef="primaryTemplate")
public class MongoConfig {
#Resource
Environment environment;
public #Bean(name = "primarydb")
MongoDbFactory mongoDbFactory() throws Exception {
MongoCredential credential = MongoCredential.createCredential(
(environment.getProperty("xxx.datastore.mongo.server.userName")),
environment.getProperty("xxx.datastore.mongo.server.database"),
environment.getProperty("xxx.datastore.mongo.server.password").toCharArray());
List<MongoCredential> mongoCredentials = new ArrayList<>();
mongoCredentials.add(credential);
MongoClientOptions options = MongoClientOptions.builder()
.connectionsPerHost(
Integer.parseInt(environment.getProperty("xxx.datastore.mongo.server.connectionsPerHost")))
.build();
List<ServerAddress> serverAddress = new ArrayList<>();
prepareServerAddress(serverAddress);
MongoClient mongoClient = new MongoClient(serverAddress, mongoCredentials, options);
return new SimpleMongoDbFactory(mongoClient,
environment.getProperty("xxx.datastore.mongo.server.database"));
}
private void prepareServerAddress(List<ServerAddress> serverAddress) throws UnknownHostException {
String serverAddressList[] = environment.getProperty("XDM.datastore.mongo.server.address")
.split("\\s*,\\s*");
for (String svrAddress : serverAddressList) {
String address[] = svrAddress.split("\\s*:\\s*");
String host = Objects.nonNull(address[0]) ? address[0] : "127.0.0.1";
Integer port = Objects.nonNull(address[1]) ? Integer.valueOf(address[1]) : 27017;
serverAddress.add(new ServerAddress(host, port));
}
}
#Primary
public #Bean(name = "primaryTemplate")
MongoTemplate mongoTemplate(#Qualifier(value = "primarydb") MongoDbFactory factory) throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(factory);
return mongoTemplate;
}
}

Why #Autowired not working all the time?

Im trying to setup mongodb with SpringBoot and im not using context.xml but trying to configure with configuration class and annotations.
package com.configuration;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
#Configuration
#EnableMongoRepositories(basePackages = "com.mongo")
public class MongoConfig extends AbstractMongoConfiguration {
#Override
protected String getDatabaseName() {
return "mongodbname";
}
#Override
public Mongo mongo() throws Exception {
return new MongoClient("127.0.0.1", 27017);
}
#Override
protected String getMappingBasePackage() {
return "com.mongo";
}
}
My repository looks like this :
package com.mongo.repositories;
import com.mongo.documents.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository("sequenceRepository")
public class SequenceRepository{
#Autowired
private MongoTemplate mongoTemplate;
public Sequence findOne(Query query){
return this.mongoTemplate.findOne(query,Sequence.class);
}
public List<Sequence> find(Query query){
return this.mongoTemplate.find(query,Sequence.class);
}
public void save(Sequence object){
this.mongoTemplate.save(object);
}
public void delete(Sequence object){
this.mongoTemplate.remove(object);
}
}
If i use like this in the rest controller it is working properly :
package com.controllers;
import com.mongo.documents.Sequence;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class IndexController {
#Autowired
private SequenceRepository sequenceRepository;
#RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
sequenceRepository.save(sequence);
return "index";
}
}
But if i want to use the SequenceRepository inside the Sequence document like this :
package com.mongo.documents;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document
public class Sequence {
#Autowired
private SequenceRepository sequenceRepository;
#Id
private String id;
private String className;
private int actual;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getActual() {
return actual;
}
public void setActual(int actual) {
this.actual = actual;
}
public void save(){
this.sequenceRepository.save(this);
}
public void delete(){
this.sequenceRepository.delete(this);
}
}
After that I change the code in the controller to :
package com.controllers;
import com.mongo.documents.Sequence;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class IndexController {
#Autowired
private SequenceRepository sequenceRepository;
#RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
sequence.save();
return "index";
}
}
I got a nullPointerExceeption at this point in the save() method.
There is no need to inject your Repository inside your Domain object Sequence as it is really bad design to have a dependency between the domain objects and your repository classes.
To follow some good practices, your Service classes or if you don't need a Service layer, your Controller classes should inject your Repository beans.
In your last code snippet your are already doing it, but the .save() method should be called on your repository SequenceRepository and not on the domain object Sequence.
An example could be the following:
#RestController
public class IndexController {
#Autowired
private SequenceRepository sequenceRepository;
#RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
// now let's save it
sequenceRepository.save(sequence);
return "index";
}
}
You can only autowire Spring-managed components into other Spring-managed components. Your sequence object is not a component (not annotated with #Component, #Service, #Repository or #Controller etc.)
I suggest you follow rieckpil's advice.