Tag: spring boot

为什么组件扫描不适用于Spring Bootunit testing?

服务类FooServiceImpl使用@Service aka @Component进行注释,这使其有资格进行自动assembly。 为什么在unit testing期间没有拾取和自动assembly此类? @Service public class FooServiceImpl implements FooService { @Override public String reverse(String bar) { return new StringBuilder(bar).reverse().toString(); } } @RunWith(SpringRunner.class) //@SpringBootTest public class FooServiceTest { @Autowired private FooService fooService; @Test public void reverseStringShouldReverseAnyString() { String reverse = fooService.reverse(“hello”); assertThat(reverse).isEqualTo(“olleh”); } } 测试无法加载应用程序上下文, 2018-02-08T10:58:42,385 INFO Neither @ContextConfiguration nor @ContextHierarchy found for test […]

如何暂停卡夫卡消费者?

我在我的框架中使用Kafka生产者 – 消费者模型。 消费者端消耗的记录随后被索引到elasticsearch上。 在这里我有一个用例,如果ES关闭,我将不得不暂停kafka消费者,直到ES启动,一旦启动,我需要恢复消费者并消耗我上次离开的记录。 我不认为这可以用@KafkaListener实现。 有谁能请给我一个解决方案吗? 我想我需要为此编写自己的KafkaListenerContainer,但我无法正确实现它。 任何帮助将非常感激。

SpingREST:无法打开JPA EntityManager进行事务处理; 嵌套exception是org.hiberna

当我启动Postman看到我的SpringREST服务运行时,我收到以下错误: { “timestamp”: 1506965117328, “status”: 500, “error”: “Internal Server Error”, “exception”: “org.springframework.transaction.CannotCreateTransactionException”, “message”: “Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection”, “path”: “/api/matchs” } 我不知道错误在哪里,这里是项目的类: 的pom.xml 4.0.0 com.kaluzny spring-boot-rest-api-postgresql 0.0.1-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-rest org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-test test org.springframework.boot […]

Log4j2 YAML生成多个日志文件问题

我的目标; 生成2个日志文件。 第一个将仅记录消息(除了消息之外没有其他内容) 第二个将记录所有INFO日志 我做了类似下面的事情; log4j2-spring.yml Configuration: status: warn monitorInterval: 30 shutdownHook: disable CustomLevels: CustomLevel: – name: MESSAGE intLevel: 50 Appenders: Console: name: Console target: SYSTEM_OUT PatternLayout: Pattern: “[%d{yyyy-MM-dd HH:mm:ss}][%-5p][%t][%c{2}] – %m%n” RollingFile: name: InfoLog fileName: “logs/all-info-logs.log” filePattern: “logs/${date:yyyy-MM}/all-info-logs-%d{MM-dd-yyyy}-%i.log.zip” PatternLayout: Pattern: “[%d{yyyy-MM-dd HH:mm:ss}][%-5p][%t][%c{2}] – %m%n” Policies: SizeBasedTriggeringPolicy: size: 5 MB DefaultRolloverStrategy: max: 50 RollingFile: name: […]

使用MultipartFile在Spring Boot中无法上传多个文件,从JSP中获取Controller中的空数组

我正在使用MultipartFile上传多个文件。 但是当我从Jsp页面选择多个文件并单击提交时,我在控制器中得到空数组。 这是我的代码片段, 在pom.xml中 commons-fileupload commons-fileupload 1.3.3 CommonsMultipartResolver Bean配置。 @Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver resolver=new CommonsMultipartResolver(); resolver.setDefaultEncoding(“utf-8”); resolver.setMaxUploadSize(999999999); return resolver; } JSP代码 Select User ${user.userName} 控制器代码 @PostMapping(value = “/createRequest”) public ModelAndView createRequest(@RequestParam(“fileUpload”) MultipartFile[] fileUpload, @RequestParam(“userDirectory”) String userDirectory) throws IOException { log.info(“In createRequest method”); if (fileUpload != null && fileUpload.length > 0) { Flow not […]

使用@Qualifier的@Bean声明不起作用

假设我有一个配置类(JmsQueueConfig,见下文)。 在本课程中,我想为整个应用程序配置多个队列。 对于一个队列,没有问题。 但是,当我添加第二个队列并尝试从服务(MemberService)使用其中一个队列时,Spring-boot告诉我 com.example.notification.application.jms.JmsEventPublisher中构造函数的参数1需要一个bean,但是找到了2个: – queueAccountToNotification:由类路径资源中的方法’queueAccountToNotification’定义[com / example / notification / application / jms / JmsQueueConfig.class] – queueNotificationToAccount:由类路径资源[com / example / notification / application / jms / JmsQueueConfig.class]中的方法’queueNotificationToAccount’定义 行动: 考虑将其中一个bean标记为@Primary,更新使用者以接受多个bean,或使用@Qualifier标识应该使用的bean 这是我的Config-Class: @Configuration @EnableJms @ImportAutoConfiguration(classes = { JmsAutoConfiguration.class, ActiveMQAutoConfiguration.class }) public class JmsQueueConfig { @Value(“${APP_QUEUE_ACCOUNT_TO_NOTIFICATION}”) private String queueAccountToNotificationName; @Value(“${APP_QUEUE_NOTIFICATION_TO_ACCOUNT}”) private String queueNotificationNameToAccount; @Bean @Qualifier(“q1”) public […]

无法使用restTemplate发送请求,与postman应用程序中的请求相同

我有来自Postman的http模板: 要使用java代码执行相同的请求,我编写了以下代码: LinkedMultiValueMap map = new LinkedMultiValueMap(); exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY, String.class); map.add(“file”, new File(“filePath”); int lastIndexOfDot = fileName.lastIndexOf(“.”); map.add(“type”, fileName.substring(lastIndexOfDot + 1)); map.add(“org_id”, systemSettingsService.getSystemSettings().getOrganizationId()); map.add(“stone_id”, fileName.substring(0, lastIndexOfDot)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<LinkedMultiValueMap> requestEntity = new HttpEntity( map, headers); restTemplate = new RestTemplate(); try { ResponseEntity result = restTemplate.exchange(buildUrl(), HttpMethod.POST, requestEntity, String.class); logger.info(“result {}”, result); } […]

黄瓜和Spring启动集成有错误

Spring启动和黄瓜集成,当我打包这个项目成为jar使用mvn包命令并生成advvic-1.0.jar。 但是,如果我运行这个jar子 java -jar target/advvic-1.0.jar 我收到此错误: 引起:cucumber.runtime.CucumberException:没有找到后端。 请确保您的CLASSPATH上有后端模块。 但是,如果我提取这个jar,我在lib文件夹中找到了cucumber-java.jar 这是我的pom: 4.0.0 com.advvic.cucumber.spring advvic 1.0 jar cucumber-spring-boot http://maven.apache.org UTF-8 1.8 1.4.1.RELEASE 1.2.5 3.0.1 2.13 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-jdbc com.h2database h2 org.springframework.boot spring-boot-starter-test info.cukes cucumber-java ${cucumber.version} info.cukes cucumber-core ${cucumber.version} info.cukes cucumber-spring ${cucumber.version} info.cukes cucumber-junit ${cucumber.version} junit junit 4.12 com.google.guava guava 20.0 org.seleniumhq.selenium […]

JPA spring boot内部join – with-clause引用了两个不同的from-clause元素错误

我正在尝试在我的一个JPA存储库中选择内部联接 查询: @Query(value = “select wm.WagerIdentification, wm.BoardNumber, wm.MarkSequenceNumber, wm.MarkNumber,” + ” pt.CouponTypeIdentification, pt.WagerBoardQuickPickMarksBoard ” + “from WagerBoard wb ” + “inner join wb.listOfWagerMarks wm on wb.WagerIdentification = wm.WagerIdentification and wb.BoardNumber = wm.BoardNumber and wb.GameIdentification = wm.GameIdentification and wm.meta_IsCurrent = 1 ” + “inner join wb.poolgameTransaction pt on (wb.TransactionIdentification = pt.TransactionIdentification and pt.meta_IsCurrent = 1)” + […]

带有2个数据库配置的Spring Boot – 使用第二个配置延迟加载不起作用

我有Spring Boot项目,有2个数据库配置。 主数据库配置: @Configuration @EnableTransactionManagement @EnableJpaRepositories(transactionManagerRef = “primaryTransactionManager”, entityManagerFactoryRef = “primaryEntityManagerFactory”, basePackages = { “com.example.repository.primary” }) public class PrimaryDbConfig { @Primary @Bean(name = “primaryDataSource”) @ConfigurationProperties(prefix = “spring.primary.datasource”) public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = “primaryEntityManagerFactory”) public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier(“primaryDataSource”) DataSource dataSource) { return builder.dataSource(dataSource).packages(“com.example.domain.primary”).persistenceUnit(“primary-persistence-unit”).build(); } @Primary @Bean(name = “primaryTransactionManager”) public PlatformTransactionManager […]