小编ip6*_*696的帖子

装饰器模式实现 - 扩展vs实现

我创建装饰模式示例:

接口:

public interface Printer {
  void print(String message);
}
Run Code Online (Sandbox Code Playgroud)

执行:

public class StringPrinter implements Printer {

  public void print(String message) {
    System.out.println(message);
  }
}
Run Code Online (Sandbox Code Playgroud)

和2个装饰者:

将字符串更改为大写:

public class UpCasePrinter implements Printer {

  private Printer printer;

  public UpCasePrinter(Printer printer) {
    this.printer = printer;
  }

  public void print(String message) {
    printer.print(message.toUpperCase());
  }
}
Run Code Online (Sandbox Code Playgroud)

打印反向字符串:

public class InversePrinter implements Printer {

  private Printer printer;

  public InversePrinter(Printer printer) {
    this.printer = printer;
  }

  public void print(String message) {
    StringBuilder builder = new StringBuilder(message); …
Run Code Online (Sandbox Code Playgroud)

java design-patterns decorator

4
推荐指数
1
解决办法
108
查看次数

JPA/Hibernate5 通过序列名称获取序列 nextval

我怎样才能获得sequence nextvalJPAHibernate 5通过sequence name

我有序列以下TEST_SEQOracleDB和ANOTHER_NAME_SEQPostgresqlDB。

我需要一个具有以下签名的方法

public Long getSequenceByName(String sequenceName){}
Run Code Online (Sandbox Code Playgroud)

当我调用这个方法时,它必须nextval从现在使用的 DB返回。

我有几个想法,但都不合适。

1)将每个数据库的本机查询存储在属性中,并像这样编写方法:

@Value("${query}")//"SELECT {name}.NEXTVAL FROM DUAL"
private StringQuery;

public Long getSequenceByName(String sequenceName){
    uery q = em.createNativeQuery(StringQuery.replace("{name}", sequenceName));
    return (java.math.BigDecimal) q.getSingleResult();
  }
Run Code Online (Sandbox Code Playgroud)

但是我需要用占位符存储查询字符串并将占位符替换为序列名称,存储每个数据库的查询。

2) 创建只有一个字段的实体@Id。插入实体和 getId(序列值)。

但是如果在不同的数据库中是不同的序列名称 - ???

3)使用这个。但它用于 hibernate 3,我不知道这是否是一个好方法。

编辑:

我尝试这个解决方案:

@Component
public class SequenseRepository {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public Long getID(final …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa sequence

4
推荐指数
1
解决办法
3497
查看次数

如何在 spring-boot 应用程序启动期间创建许多 kafka 主题?

我有这个配置:

@Configuration
public class KafkaTopicConfig {

    private final TopicProperties topics;

    public KafkaTopicConfig(TopicProperties topics) {
        this.topics = topics;
    }

    @Bean
    public NewTopic newTopicImportCharge() {
        TopicProperties.Topic topic = topics.getTopicNameByType(MessageType.IMPORT_CHARGES.name());
        return new NewTopic(topic.getTopicName(), topic.getNumPartitions(), topic.getReplicationFactor());
    }

    @Bean
    public NewTopic newTopicImportPayment() {
        TopicProperties.Topic topic = topics.getTopicNameByType(MessageType.IMPORT_PAYMENTS.name());
        return new NewTopic(topic.getTopicName(), topic.getNumPartitions(), topic.getReplicationFactor());
    }

    @Bean
    public NewTopic newTopicImportCatalog() {
        TopicProperties.Topic topic = topics.getTopicNameByType(MessageType.IMPORT_CATALOGS.name());
        return new NewTopic(topic.getTopicName(), topic.getNumPartitions(), topic.getReplicationFactor());
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以将 10 个不同的主题添加到 TopicProperties. 而且我不想手动创建每个类似的 bean。是否存在某种方法可以在spring-kafka或仅spring 中创建所有主题?

spring apache-kafka spring-boot spring-kafka spring-config

4
推荐指数
1
解决办法
6556
查看次数

为什么Google调用前缀为"m"的变量?

为什么Google会调用前缀为"m"的变量,例如:

    private int mSectionResourceId;
    private int mTextResourceId;
Run Code Online (Sandbox Code Playgroud)

我在所有的例子中看到了它.但我不明白为什么他们这样做?

现在我有一些实例非常好的例子.如果一个没有前缀的被调用的变量,我需要写

public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
                                              RecyclerView.Adapter baseAdapter) {
        this.sectionResourceId = sectionResourceId;
        this.textResourceId = textResourceId;
Run Code Online (Sandbox Code Playgroud)

但如果我使用前缀我可以写

public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
                                              RecyclerView.Adapter baseAdapter) {

        mSectionResourceId = sectionResourceId;
        mTextResourceId = textResourceId;
Run Code Online (Sandbox Code Playgroud)

我觉得它更具可读性.谁能向我解释一下前缀的优缺点?

android

3
推荐指数
1
解决办法
1628
查看次数

Java中垃圾收集器的工作

在通话时有哪些对象可用于垃圾收集System.gc()?为什么?

public class GCTest {
    static class A {
        private String myName;
        public A(String myName) {
            this.myName = myName;
        }
    }

    public static void main(String[] args) {
        A a1 = new A("a1");
        A a2 = new A("a2");
        ArrayList list = new ArrayList();
        list.add(a1);
        A[] mas = new A[2];
        mas[0] = a2;
        a2 = a1;
        clear(mas);
        a1 = null;
        a2 = null;
        System.gc();
        // some code
        ...
    }

    private static void clear(A[] mas) {
        mas = null;
    }
} …
Run Code Online (Sandbox Code Playgroud)

java garbage-collection

3
推荐指数
1
解决办法
101
查看次数

@ConditionalOnProperty 默认 bean 无法正常工作

我有 2 颗豆子:

@Component("CryptoClient")
@ConditionalOnProperty(name = "enabled-client", havingValue = "AfsClient")
public class AfsClient implements CryptoClient {
Run Code Online (Sandbox Code Playgroud)

@Component("CryptoClient")
@ConditionalOnProperty(name = "enabled-client", havingValue = "JinnClient")
public class JinnClient implements CryptoClient {
Run Code Online (Sandbox Code Playgroud)

我更改属性值:

enabled-client: AfsClient或者enabled-client: JinnClient并使用这个bean。但现在我添加默认 bean(如果enabled-client属性不存在):

    @Bean("CryptoClient")
    @ConditionalOnProperty(name = "enabled-client", matchIfMissing = true)
    public CryptoClient defaultClient(EDSService edsService) {
        return new AfsClient(edsService);
    }
Run Code Online (Sandbox Code Playgroud)

如果enabled-client属性不存在 - 它工作正常。但即使存在财产,它也能发挥作用。无论如何调用默认 bean。

java spring spring-boot

3
推荐指数
1
解决办法
5133
查看次数

如何使用 gradle 在 spring-boot 中生成 JPA 元模型?(+ lombok)

我想将 JPA 元模型添加到我的项目中 -Spring boot + gradle

我发现很多例子我该怎么做,但都是用Maven. 我还找到这个网站:https ://plugins.gradle.org/search?term=metamodel

并尝试前三个插件。对于每个插件,我都会遇到错误: error: cannot find symbol在标记为 lombok@Builder注释的类中,而某些类则不是entity。这是一些插件的示例:

    buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.iboyko.gradle.plugins:jpamodelgen-plugin:1.0.1"
    }
 }

    dependencies {
        compile ('org.hibernate:hibernate-jpamodelgen')
    }
Run Code Online (Sandbox Code Playgroud)

JPA metamodel1)哪个插件或方法是在 Spring 中创建的最官方(正确)的boot + spring-data-jpa + gradle

2)如何只指定包含的包 entities 而不扫描其他类?

3)如何与它交朋友lombok

编辑

我将此代码添加到 gradle 文件中:

sourceSets.configureEach { sourceSet ->
    tasks.named(sourceSet.compileJavaTaskName).configure {
        options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/java")
    }
}
Run Code Online (Sandbox Code Playgroud)

它生成classes_ …

jpa gradle metamodel spring-data-jpa spring-boot

3
推荐指数
1
解决办法
9577
查看次数

Spring @Service 中的类字段线程安全吗?

@Slf4j
@Service
public class SendServiceImpl implements SendService {

    private final MessageService messageService;

    private Message message;

    public SendServiceImpl (MessageService messageService) {
        this.messageService = messageService;
    }

    @Transactional
    @Override
    public void send(String messageGuid) {
            message = messageService.getOne(messageGuid);
            //...
    }
Run Code Online (Sandbox Code Playgroud)

这个类线程安全吗?如果我像这样在 5 个线程中运行会出现什么问题:

taskExecutor.execute(() -> sendService .send(someGuid);//5 different guids
Run Code Online (Sandbox Code Playgroud)

在实践中,我研究了日志文件,发现同一实体使用不同的线程工作。message 我是否正确理解,在这种情况下,如果我像类字段一样声明消息,线程可以更改实体的值?

谁能详细解释一下吗?

java spring multithreading

3
推荐指数
1
解决办法
6672
查看次数

在BottomNavigationView上方设置容器布局

我有这个布局。此布局包含BottomNavigationView和嵌套片段的容器

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="client.MainActivity">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

如何app_bar_main在BottomNavigationView上方设置上方?

因为现在:

在此处输入图片说明 BottomNavigationView与交叉

android android-layout

2
推荐指数
1
解决办法
1432
查看次数

如何在 spring boot 中从 message_ru_RU.properties 加载俄语(西里尔文,utf-8)文本?

我有调度程序。每 5 秒我从数据库获取一些项目并将推送通知发送到 android。但是此通知中的文本为俄语。在功能上,我们支持不同的语言,现在我尝试创建一些模板。我有消息模板的属性文件:messages_ru_RU.properties我在这个文件中有字符串: notification.message=%s. ?? ?????: %s. ?????: %s

我需要从属性加载这个字符串并重新修改%s我的值。现在我创建配置服务:

@Configuration
public class LocaleConfig {

  @Bean
  public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    Locale defaultLocale = new Locale("ru");
    slr.setDefaultLocale(defaultLocale);
    return slr;
  }
  @Bean
  public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:messages");
    messageSource.setCacheSeconds(3600); //refresh cache once per hour
    return messageSource;
  }
}
Run Code Online (Sandbox Code Playgroud)

和服务:

@Component
public class MessageByLocaleServiceDefault implements MessageByLocaleService {

  private final MessageSource messageSource;

  @Autowired
  public MessageByLocaleServiceDefault(MessageSource messageSource) {
    this.messageSource = messageSource; …
Run Code Online (Sandbox Code Playgroud)

java localization properties spring-boot

2
推荐指数
1
解决办法
2199
查看次数

如何在不同线程中处理@KafkaListener方法?

我在 Spring Boot 中有卡夫卡处理程序:

    @KafkaListener(topics = "topic-one", groupId = "response")
    public void listen(String response) {
        myService.processResponse(response);
    }
Run Code Online (Sandbox Code Playgroud)

例如,生产者每秒发送一条消息。但myService.processResponse工作10秒。我需要处理每条消息并myService.processResponse在新线程中开始。我可以创建我的执行者并将每个响应委托给它。但我认为 kafka 中还有其他配置可供使用。我找到了2个:

1)添加concurrency = "5"@KafkaListener注释 - 它似乎有效。但我不确定有多正确,因为我有第二种方法:

2)我可以创建ConcurrentKafkaListenerContainerFactory并设置它ConsumerFactory并且concurrency

我不明白这些方法之间的区别?concurrency = "5"只需添加到注释就足够了@KafkaListener还是我需要创建ConcurrentKafkaListenerContainerFactory

或者我根本不明白什么,还有其他方法吗?

java apache-kafka spring-boot kafka-consumer-api spring-kafka

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

如果签名不同,Java编译器禁止在内部类方法中创建与外部类同名的方法

为什么这段代码有效:

class Parent {
    private void methodA(String a){
        System.out.println(a);
    }

    class Inner {

        void test(int a){
            methodA("1");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码不起作用(我只是将方法添加到具有相同名称和另一个签名的内部类):

class Parent {
    private void methodA(String a){
        System.out.println(a);
    }

    class Inner {

        private void methodA(int a){
            System.out.println(a);
        }

        void test(int a){
            methodA("1");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不问如何让它发挥作用.我想说明为什么第二个选项不起作用?我想要一个解释,而不是解决方案.

java inheritance javac inner-classes

0
推荐指数
1
解决办法
55
查看次数