У меня есть многомодульное приложение на базе пружин, определенное как и использовавшее @Import для импорта конфигурации другого модуля, но все же я получаю исключение ниже -
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loyaltyService': Unsatisfied dependency expressed through field 'loyalistRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.a.b.loyalty.data.repository.LoyalistRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.a.b.loyalty.data.repository.LoyalistRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
1) Spring-Data -Проект
loyalty-data
main
java
com.a.b.loyalty.data.config
com.a.b.loyalty.data.entity
com.a.b.loyalty.data.repository
LoyalistRepository. java
package com.a.b.loyalty.data.repository;
@Repository
public interface LoyalistRepository extends JpaRepository<Loyalist, Integer> {
Loyalist findLoyalistByLoyaltyId(String loyaltyId) ;
}
LoyaltyDataConfiguration. java
package com.a.b.loyalty.data.config;
@Configuration
@ComponentScan(basePackages = "com.a.b.loyalty.data")
//@EnableJpaRepositories(basePackages = "com.a.b.loyalty.data.repository")
//@EntityScan(basePackages ="com.a.b.loyalty.data.entity" )
@Getter
@Setter
public class LoyaltyDataConfiguration {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.hikari.username}")
private String hikariUserName;
@Value("${spring.datasource.hikari.password}")
private String hikariPassword;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().driverClassName(driverClassName).url(url).username(hikariUserName).password(hikariPassword).build();
}
}
Лоялист. java
package com.a.b.loyalty.data.entity;
@Getter
@Setter
@Entity
@Table(name = "LOYALTY_DETAILS")
public class Loyalist {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOYALTY_DETAILS_ID")
private int loyaltyDetailsId;
@Column(name = "LOYALTY_ID")
private String loyaltyId;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "DOB")
private Date birthDate;
@Column(name = "IS_NEW")
private boolean new_;
@Column(name = "TIER")
private String gameTier;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
2) Spring-Boot-Service
loyalty-service
main
java
com.a.b.loyalty.service
com.a.b.loyalty.service.config
com.a.b.loyalty.service.model
com.a.b.loyalty.service.rest
test
java
com.a.b.loyalty.service
com.a.b.loyalty.service.config
com.a.b.loyalty.service.model
com.a.b.loyalty.service.rest
LoyaltyApplication . java
package com.a.b.loyalty.service;
@SpringBootApplication
public class LoyaltyApplication {
public static void main(String[] args) {
SpringApplication.run(LoyaltyApplication.class, args);
}
}
LoyaltyServiceConfig. java
@Configuration
@ComponentScan(basePackages = "com.a.b.loyalty.service")
@Import({LoyaltyDataConfiguration.class})
public class LoyaltyServiceConfig {
}
LoyaltyService. java
package com.a.b.loyalty.service;
/**
* Loyalty Service
*/
@Service
public class LoyaltyService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private DroolsLoyaltyProvider droolsLoyaltyProvider;
@Autowired
private LoyalistRepository loyalistRepository;
@Autowired
private LoyaltyPointRepository loyaltyPointRepository;
// public LoyaltyService(DroolsLoyaltyProvider droolsLoyaltyProvider, LoyalistRepository loyalistRepository, LoyaltyPointRepository loyaltyPointRepository) {
// this.droolsLoyaltyProvider = droolsLoyaltyProvider;
// this.loyalistRepository = loyalistRepository;
// this.loyaltyPointRepository = loyaltyPointRepository;
// }
public LoyaltyResponse calculatePoints(LoyaltyRequest loyaltyRequest) {
logger.debug(".calculatePoints() Invoked: " + loyaltyRequest);
LoyaltyProviderRequest loyaltyProviderRequest = LoyaltyProviderRequest.builder().loyalist(PlayerTransformation.toPlayerBO(loyaltyRequest.getLoyalist())).build();
LoyaltyProviderResponse loyaltyProviderResponse = droolsLoyaltyProvider.execute(loyaltyProviderRequest);
LoyaltyResponse loyaltyResponse = new LoyaltyResponse();
loyaltyResponse.setPoints(PointsTransformation.toPointsDO(loyaltyProviderResponse.getPoints()));
logger.debug(".calculatePoints() Done");
return loyaltyResponse;
}
/**
* @return Points
*/
public LoyaltyResponse retrievePoints(LoyaltyRequest loyaltyRequest) {
com.a.b.loyalty.data.entity.LoyaltyPoints loyaltyPoints = loyaltyPointRepository.findLoyaltyPointsByLoyaltyIdOrderByModifyDateDesc(loyaltyRequest.getLoyalist().getLoyaltyId());
LoyaltyResponse loyaltyResponse = new LoyaltyResponse();
LoyaltyPoints points = new LoyaltyPoints();
points.setPoints(loyaltyPoints.getTotalEarnPoints());
return loyaltyResponse;
}
public LoyaltyResponse awardPoints(LoyaltyRequest loyaltyRequest) {
logger.debug(".calculatePoints() Invoked: " + loyaltyRequest);
LoyaltyProviderRequest loyaltyProviderRequest = LoyaltyProviderRequest.builder().loyalist(PlayerTransformation.toPlayerBO(loyaltyRequest.getLoyalist())).build();
LoyaltyProviderResponse loyaltyProviderResponse = droolsLoyaltyProvider.execute(loyaltyProviderRequest);
if (loyaltyProviderResponse.getPoints().getEarnedPointForCurrentTxn() > 0) {
Loyalist loyalist = loyalistRepository.findLoyalistByLoyaltyId(loyaltyProviderRequest.getLoyalist().getLoyaltyId());
com.a.b.loyalty.data.entity.LoyaltyPoints loyaltyPointsEntity = new com.a.b.loyalty.data.entity.LoyaltyPoints();
com.a.b.loyalty.data.entity.LoyaltyPoints loyaltyPoints = new com.a.b.loyalty.data.entity.LoyaltyPoints();
loyaltyPoints.setCurrentPoints(loyaltyProviderResponse.getPoints().getCurrentPoints() + loyaltyProviderResponse.getPoints().getEarnedPointForCurrentTxn());
loyaltyPoints.setLoyalist(loyalist);
loyaltyPoints.setLoyaltyEvent("EARN_POINT");
loyaltyPoints.setLoyaltyId(loyalist.getLoyaltyId());
loyaltyPoints.setTierDuringPointsEarned(loyalist.getGameTier());
loyaltyPoints.setTotalEarnPoints(loyaltyProviderResponse.getPoints().getEarnedPointForCurrentTxn());
loyaltyPoints.setTransactionDate(Date.valueOf(LocalDate.now()));
loyaltyPointRepository.save(loyaltyPoints);
}
LoyaltyResponse loyaltyResponse = new LoyaltyResponse();
loyaltyResponse.setPoints(PointsTransformation.toPointsDO(loyaltyProviderResponse.getPoints()));
logger.debug(".calculatePoints() Done");
return loyaltyResponse;
}
}
Тестовые классы LoyaltyServiceTest. java
package com.a.b.loyalty.service;
@SpringBootTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {LoyaltyTestServiceConfig.class})
class LoyaltyServiceTest {
static private LoyaltyRequest loyaltyRequest;
static private Loyalist loyalist;
@InjectMocks
LoyaltyService loyaltyService;
@Mock
DroolsLoyaltyProvider droolsLoyaltyProvider;
@Mock
LoyaltyProviderResponse loyaltyProviderResponse;
@BeforeAll
public static void init() {
loyalist = new Loyalist();
loyaltyRequest = new LoyaltyRequest();
loyaltyRequest. setLoyalist(loyalist);
}
@Test
void calculatePointsWhenPlayerIsNew() {
loyalist.setNew_(true);
Mockito.when(droolsLoyaltyProvider.execute(Mockito.any())).thenReturn(loyaltyProviderResponse);
Mockito.when(loyaltyProviderResponse.getPoints()).thenReturn(LoyaltyPoints.LoyaltyPointsBuilder.aLoyaltyPoints() .withPoints(Mockito.anyInt()).build());
LoyaltyResponse loyaltyResponse = loyaltyService.calculatePoints(loyaltyRequest);
Assertions.assertNotNull(loyaltyResponse);
Assertions.assertNotNull(loyaltyResponse.getPoints().getPoints() > 0);
}
@Test
void calculatePointsWhenPlayerIsNotNew() {
loyalist.setNew_(false);
Mockito.when(droolsLoyaltyProvider.execute(Mockito.any())).thenReturn(loyaltyProviderResponse);
Mockito.when(loyaltyProviderResponse.getPoints()).thenReturn(LoyaltyPoints.LoyaltyPointsBuilder.aLoyaltyPoints() .withPoints(Mockito.anyInt()).build());
LoyaltyResponse loyaltyResponse = loyaltyService.calculatePoints(loyaltyRequest);
Assertions.assertNotNull(loyaltyResponse);
Assertions.assertTrue(loyaltyResponse.getPoints().getPoints() == 0);
}
}
LoyaltyTestServiceConfig. java
package com.a.b.loyalty.service.config;
@EnableAutoConfiguration
public class LoyaltyTestServiceConfig extends LoyaltyServiceConfig {
}