小编Wim*_*uwe的帖子

枚举中的内部类在Java中是否始终是静态的?

我有一个枚举类,其中包含Java中的内部类.

例如(在实际代码中,在内部使用内部类的枚举上声明了一些方法):

public enum MyEnum{
  VALUE_1,
  VALUE_2;

  private static class MyInnerClass // is static here needed or can it be removed?
  { 
  }
}
Run Code Online (Sandbox Code Playgroud)

PMD告诉我不需要'static'修饰符(违反UnusedModifier规则).这是正确的还是PMD错误?

注意:这个问题不重复,这与我在这里要求的相反.

java enums pmd

10
推荐指数
1
解决办法
3948
查看次数

如何使用 Spring Boot 一次性针对真实数据库而不是内存中运行 @DataJpaTest

我正在使用 Spring Boot 1.4.3 并且有一大堆用@DataJpaTest. 默认情况下,它们针对内存数据库运行。我希望能够暂时针对本地 MySQL 运行所有这些。我怎样才能以简单的方式做到这一点?

我发现,我可以把它加入了一个工作,@ActiveProfiles("local")在那里我有一个application-local.properties是指向我的本地MySQL,但它仅仅是太多的工作,以补充说无处不在,运行测试,然后将其删除(因为我只想针对 MySQL 手动运行它,CI 环境将针对内存数据库运行)。

如果这很重要,我正在使用 Maven。

更新:

所以我有一个application-local.properties包含 db 属性来连接到我的本地 MySQL 数据库(我已经使用它来针对本地 MySQL 运行我的应用程序)

然后我在 IntelliJ 中右键单击一个包并选择“运行包中的所有测试”。在该运行配置的设置中,我添加-Dspring.profiles.active=local到“VM 选项”字段。

我原以为这会local在测试期间激活配置文件,但事实并非如此。如果我停止本地 MySQL,测试仍然运行良好。

java spring-test spring-boot

9
推荐指数
1
解决办法
3695
查看次数

为什么Maven选择版本1.0.b2超过1.3.03

我有一个依赖于HTTP BUilder的项目,这给了我后续的依赖树:

[INFO] +- org.codehaus.groovy.modules.http-builder:http-builder:jar:0.5.1:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.3.2:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.6:compile
[INFO] |  +- net.sf.json-lib:json-lib:jar:jdk15:2.3:compile
[INFO] |  |  +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |  |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  |  +- commons-lang:commons-lang:jar:2.4:compile
[INFO] |  |  \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] |  +- net.sourceforge.nekohtml:nekohtml:jar:1.9.9:compile
[INFO] |  |  \- xerces:xercesImpl:jar:2.8.1:compile
[INFO] |  |     \- xml-apis:xml-apis:jar:1.3.03:compile
[INFO] |  \- xml-resolver:xml-resolver:jar:1.2:compile
Run Code Online (Sandbox Code Playgroud)

在我添加hibernate-entitymanager后,xml-apis:aml-apis更改版本.突然,Maven更喜欢使用版本1.0b2,这是dom4j的传递依赖:

[INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.1.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:4.3.1.Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] …
Run Code Online (Sandbox Code Playgroud)

java xerces maven

8
推荐指数
1
解决办法
1317
查看次数

使用Java 9模块(JPMS)时java.lang.NoSuchMethodError

我正在尝试使用JPMS模块将JavaFX,Spring Boot和VLCJ结合在一起。如果没有Spring Boot,则在我的module-info.java文件中一切正常:

module myapplication.module {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.web;
    requires vlcj;

    requires org.kordamp.iconli.core;
    requires org.kordamp.ikonli.javafx;
    requires org.kordamp.ikonli.fontawesome5;

    exports com.company.app;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果现在结合使用Spring Boot,则我将其更新module-info.java为包括Spring相关模块:

    requires spring.beans;
    requires spring.context;
    requires spring.core;
    requires spring.boot.autoconfigure;
    requires spring.boot;
Run Code Online (Sandbox Code Playgroud)

但是,我在运行时遇到此异常:

java.lang.NoSuchMethodError: com.sun.jna.Native.load(Ljava/lang/String;Ljava/lang/Class;)Lcom/sun/jna/Library;
    at vlcj.natives@4.1.0/uk.co.caprica.vlcj.binding.LibC.<clinit>(LibC.java:38)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.discovery.strategy.OsxNativeDiscoveryStrategy.setPluginPath(OsxNativeDiscoveryStrategy.java:72)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.discovery.strategy.BaseNativeDiscoveryStrategy.onSetPluginPath(BaseNativeDiscoveryStrategy.java:126)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.discovery.NativeDiscovery.tryPluginPath(NativeDiscovery.java:176)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.discovery.NativeDiscovery.discover(NativeDiscovery.java:117)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.MediaPlayerFactory.discoverNativeLibrary(MediaPlayerFactory.java:187)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.MediaPlayerFactory.<init>(MediaPlayerFactory.java:119)
    at vlcj@4.2.0/uk.co.caprica.vlcj.factory.MediaPlayerFactory.<init>(MediaPlayerFactory.java:174)
    at myapplication.module/com.company.app.MyApplication.mediaPlayerFactory(MyApplicationConfiguration.java:18)
    at myapplication.module/com.company.app.MyApplication$$EnhancerBySpringCGLIB$$702eae01.CGLIB$mediaPlayerFactory$0(<generated>)
    at myapplication.module/com.company.app.MyApplication$$EnhancerBySpringCGLIB$$702eae01$$FastClassBySpringCGLIB$$af782040.invoke(<generated>)
    at spring.core@5.1.9.RELEASE/org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at spring.context@5.1.9.RELEASE/org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
    at myapplication.module/com.company.app.MyApplication$$EnhancerBySpringCGLIB$$702eae01.mediaPlayerFactory(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at …
Run Code Online (Sandbox Code Playgroud)

java vlcj spring-boot java-module

8
推荐指数
1
解决办法
66
查看次数

如何从自定义实现中引用"普通"spring数据仓库?

我想扩展一个JpaRepository自定义实现,所以我添加了一个MyRepositoryCustom接口和一个MyRepositoryImpl扩展此接口的类.

有没有办法从JpaRepository我的自定义类中调用方法?

注意:这也是作为对/sf/answers/831684241/的评论而提出的,但我认为值得一个单独的问题是很常见的.

java spring-data spring-data-jpa

7
推荐指数
1
解决办法
1802
查看次数

Spring Data 存储库未从 save 方法返回更新的 @version

在 Spring Boot 1.3.0 应用程序中,我将 Spring Data JPA 与 Hibernate 结合使用。

我有这样的服务方法:

@Override
@Transactional(readOnly = false)
public BookDto updateBook(BookDto bookDto) {
        BookId id = new BookId(bookDto.getId());
        if (!exists(id)) {
            throw new EntityNotFoundException("Unable to find a book with id " + bookDto.getId());
        }
        return convertToDto(bookRepository.save(bookDto.convertToBook()));
    }
Run Code Online (Sandbox Code Playgroud)

在我的实体中,我有一个 @Version 字段:

@Version
private long version;
Run Code Online (Sandbox Code Playgroud)

(带有 getter 和 setter),我的 DTO 中也有这个(DTO 上没有 @Version 注释)。

从调用中返回的“Book”实体bookRepository.save()仍然具有version = 0,而不是1如预期的那样。当我之后在存储库上执行操作时findAll,版本更新得很好。这可能是什么原因?

spring hibernate jpa spring-data

7
推荐指数
1
解决办法
3176
查看次数

Amazon Java SDK 的 putObject() 之后需要关闭 InputStream 吗?

putObjectAmazon Java SDK for S3 的方法中是否需要关闭传入的InputStream ?

他们的示例仅显示了文件的用法,但没有显示输入流的用法。Javadoc也没有指出是否需要。

java amazon-s3 amazon-web-services

7
推荐指数
1
解决办法
2175
查看次数

如何将 Testcontainers 与 @DataJpaTest 结合起来避免代码重复?

我想使用 JUnit 5 来​​使用 Testcontainers 和@DataJpaTest(and )。我有使用and注释@SpringBootTest的基本设置,如下所示:@Testcontainers@Container

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
public class AtleteRepositoryTest {
    @Container
    private static final PostgreSQLContainer<?> CONTAINER = new PostgreSQLContainer<>("postgres:11");

    @DynamicPropertySource
    static void registerProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", CONTAINER::getJdbcUrl);
        registry.add("spring.datasource.username", CONTAINER::getUsername);
        registry.add("spring.datasource.password", CONTAINER::getPassword);
    }

    @Autowired
    private AtleteRepository repository;

    @Test
    void testSave() {
        repository.save(new Atlete("Wout Van Aert", 0, 1, 0)); …
Run Code Online (Sandbox Code Playgroud)

java spring-test junit5 testcontainers

7
推荐指数
1
解决办法
2789
查看次数

在Reentrantlock上使用lockInterruptibly时如何避免IllegalMonitorStateException

我想用一个syncronized块替换一个块ReentrantLock来支持等待锁的中断.为此,我使用lockInterruptibly()方法和惯用的try/finally块:

private ReentrantLock lock = new ReentrantLock();

try
{
  lock.lockInterruptably();
}
catch( InterruptedException e )
{
  Thread.currentThread.interrupt();
}
finally
{
  lock.unlock();
}
Run Code Online (Sandbox Code Playgroud)

问题是当InterruptedException发生时,也会发生最终的问题.这导致a IllegalMonitorStateException,因为当前线程不保持锁定.

这个简单的程序证明了这一点:

public class LockTest
{
public static void main( String[] args )
{
    System.out.println("START");

    Thread interruptThread = new Thread( new MyRunnable( Thread.currentThread() ) );
    interruptThread.start();
    ReentrantLock lock = new ReentrantLock();

    Thread takeLockThread = new Thread( new TakeLockRunnable( lock ) );
    takeLockThread.start();

    try
    {
        Thread.sleep( 500 );
        System.out.println("Trying to take …
Run Code Online (Sandbox Code Playgroud)

java concurrency

6
推荐指数
1
解决办法
1万
查看次数

如何将具有2个实体名称的同一类迁移到Spring Data JPA?

我正在从Hibernate将应用程序迁移到Spring Data JPA.我已经迁移了一些存储库并使其正常工作.我现在有一个需要转换的特殊情况.

我在.hbm.xml中有这个:

<class name="SoundNotification" table="SoundNotification" entity-name="SoundNotificationWithData">
        <id name="m_id" type="int" column="id" unsaved-value="-1">
            <generator class="native"/>
        </id>
        <property name="m_name" column="name" unique="true" not-null="true"/>
        <property name="m_data" column="data"
                  type="com.traficon.tmsng.server.common.service.persistence.impl.hibernate.usertype.BlobUserType"
                  not-null="true"/>
        <property name="m_size" formula="OCTET_LENGTH(data)"/>
        <property name="m_inUse"
                  formula="(select count(1) from EventTypeConfiguration etc where etc.soundNotification=id)"/>
    </class>

    <class name="SoundNotification" table="SoundNotification" entity-name="SoundNotificationWithoutData">
        <id name="m_id" type="int" column="id" unsaved-value="-1">
            <generator class="native"/>
        </id>
        <property name="m_name" column="name" unique="true" not-null="true"/>
        <property name="m_size" formula="OCTET_LENGTH(data)"/>
        <property name="m_inUse"
                  formula="(select count(1) from EventTypeConfiguration etc where etc.soundNotification=id)"/>
    </class>
Run Code Online (Sandbox Code Playgroud)

请注意我只有1个类SoundNotification,但它与2个不同的entity-names一起使用(SoundNotificationWithData和SoundNotificationWithoutData)

是否可以将其转换为Spring Data JPA?我需要创建2个java类作为"解决方法"吗?

我们的另一个例子就是这个:

<class name="FlowDataMessageImpl" …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa spring-data

6
推荐指数
1
解决办法
664
查看次数