spring - Spring-多模块未加载bean-获取异常无法加载ApplicationContext
问题描述
我有一个多模块弹簧基础应用程序定义为并使用@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-Project
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) ;
}
忠诚度数据配置.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 {
}
解决方案
我认为你必须添加@EnableJpaRepositories
和@EntityScan
package com.a.b.loyalty.service;
@SpringBootApplication
@EnableJpaRepositories(basePackages="<repository package name>")
@EnableTransactionManagement
@EntityScan(basePackages="<entity package name>")
public class LoyaltyApplication {
public static void main(String[] args) {
SpringApplication.run(LoyaltyApplication.class, args);
}
}
推荐阅读
- python - 如何使用 Pandas 查找链式任务序列?
- javascript - 对象的对象的映射属性修剪名称属性(如果它太长)
- javascript - Tic Tac Toe 2 玩家追踪他们是否获胜
- r - 为什么某些准确度度量未显示在插入符号中( F1 、 Recall 和 precision )
- reactjs - 导出打字稿中的对象列表,并动态导入它们一个反应组件
- ubuntu - SED - 变量上的“文件名太长”
- android - Android中的布局响应问题
- c# - EF Core 5 - 使用 FromSqlRaw 执行存储过程会导致“已添加具有相同键的项目”
- c++ - SetProcessValidCallTargets 引发错误 87
- javascript - 我在模态组件中添加显示和隐藏,但它不起作用