小编Far*_*hir的帖子

如何避免在 Java 中具有相同方法主体但不同返回类型的重复代码?

理念:

当我使用 hibernate 时,我每次必须编写某种代码时都会看到这一点。所以我将它们移到另一种方法作为包装器。哪里会有函数式接口作为参数,以便我可以在这些上下文方法中附加一些代码。

问题:

这是我的两种方法。一个返回对象,而另一个返回列表。我怎样才能准确地泛化并将这两种方法合二为一,这样我就可以避免代码重复。

public Object objectReturnContext(Function<Session, Object> function) {
    Object object = null;
    Transaction transaction = null;

    try {
        Session session = HibernateUtil.sessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
        object = function.apply(session);
        transaction.commit();
    } catch (NoResultException exception) {
        if (transaction != null) transaction.rollback();
        return object;
    } catch (HibernateException exception) {
        if (transaction != null) transaction.rollback();
        exception.getStackTrace();
    }

    return object;
}


public List<T> listReturnContext(Function<Session, List<T>> function) {
    List<T> object = null;
    Transaction transaction = null;

    try {
        Session session = HibernateUtil.sessionFactory().getCurrentSession(); …
Run Code Online (Sandbox Code Playgroud)

java oop generics hibernate code-duplication

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

导入 io.jsonwebtoken.security.Keys 时无法解析符号“安全”;

我正在尝试使用 JWT 构建一个关于 API 身份验证的项目。我已经安装了版本 0.9.1 的 io.jsonwebtoken 包,以便做到这一点。

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xyz</groupId>
    <artifactId>jwt_api_authentication</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jwt_api_authentication</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId> …
Run Code Online (Sandbox Code Playgroud)

java pom.xml maven jwt spring-boot

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

标签 统计

java ×2

code-duplication ×1

generics ×1

hibernate ×1

jwt ×1

maven ×1

oop ×1

pom.xml ×1

spring-boot ×1