1

这些是我在项目中所做的以下配置。

@Configuration
public class AppMongoConfig {
        
    @Autowired private MongoDbFactory mongoDbFactory;
        
    @Autowired private MongoMappingContext mongoMappingContext;
        
    @Bean
    public MappingMongoConverter mappingMongoConverter() {
        
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));
        
        return converter;
    }
}
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {
    
    private final MultipleMongoProperties mongoProperties;
    
    @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
    @Primary
    public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClientURI(mongo.getUri()));
    }
    
    @Bean
    public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
        return new SimpleMongoDbFactory(new MongoClientURI(mongo.getUri()));
    }
}
@Data
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {
    private MongoProperties primary = new MongoProperties();
    private MongoProperties secondary = new MongoProperties();
}
@Configuration
@EnableMongoRepositories(
    basePackages = {"com.student.repository.primary"},
    mongoTemplateRef = "primaryMongoTemplate")
    public class PrimaryMongoConfig {}
@Configuration
@EnableMongoRepositories(
    basePackages = {"com.student.repository.secondary"},
    mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {}

存储库代码:

public interface StudentDAO {
        
    Student save(StudentInfo studentInfo); 
} 
@Repository 
public class StudentDAOImpl implements StudentDAO {
       
    @Autowired   @Qualifier("primaryMongoTemplate")   
    private MongoTemplate mongoTemplate;
        
    @Override   public StudentInfo save(StudentInfo userWatchlist) {
        return mongoTemplate.save(userWatchlist);   
    } 
}

集成测试代码:

@RunWith(SpringRunner.class)
@DataMongoTest(includeFilters = @Filter(Repository.class))
public class WatchListDAOImplIT {
    
    @Autowired private StudentDAO studentDAO;
    
    @Test
    public void save() {
        StudentInfo studentInfo = getStudentInfo();
        StudentInfo dbUserWatchlist = watchListDAO.save(studentInfo);
        Assert.assertEquals(studentInfo.getId(), dbUserWatchlist.getId());
    }
    
    private StudentInfo getStudentInfo() {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setId(9999999l);
        return studentInfo;
    }
}

这给了我以下错误:-

java.lang.IllegalStateException: Failed to load ApplicationContext
    at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at
org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:
: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'appMongoConfig': Unsatisfied dependency
expressed through field 'mongoDbFactory'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'primaryFactory' defined in class path
resource [.../config/mongo/MultipleMongoConfig.class]: Unsatisfied
dependency expressed through method 'primaryFactory' parameter 0;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.boot.autoconfigure.mongo.MongoProperties'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations: {}

Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.boot.autoconfigure.mongo.MongoProperties'
available: expected at least 1 bean which
4

0 回答 0