无法使用Maven部署的依赖项中的类

时间:2018-12-06 15:04:10

标签: java maven spring-boot

我将依赖项部署到了一个私有Maven存储库(JFrog Artifactory) 当我要进行测试时,会抛出此错误"Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.util.PropertiesUtil"

这就是我所说的:

AccountManager.getInstance().getAccounts();

在这个客户经理中,我有一个客户服务,该服务使用Spring Boot中的restTemplate向后端API发出请求。

Stacktrace:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.util.PropertiesUtil
    at org.apache.logging.log4j.status.StatusLogger.<clinit>(StatusLogger.java:78)
    at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:60)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:135)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:102)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:79)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.springframework.http.HttpLogging.<clinit>(HttpLogging.java:45)
    at org.springframework.http.client.support.HttpAccessor.<init>(HttpAccessor.java:48)
    at org.springframework.http.client.support.InterceptingHttpAccessor.<init>(InterceptingHttpAccessor.java:44)
    at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:138)
    at br.com.toxicnetwork.tooling.account.AccountServiceImpl.<init>(AccountServiceImpl.java:17)
    at br.com.toxicnetwork.tooling.account.AccountManager.<init>(AccountManager.java:16)
    at br.com.toxicnetwork.tooling.account.AccountManager.getInstance(AccountManager.java:71)
    at br.com.toxicnetwork.engine.Main.main(Main.java:8)

客户经理:

public class AccountManager {

private static AccountManager instance;
private AccountService accountService;
private Map<String, Account> cachedAccounts;

private AccountManager() {
    accountService = new AccountServiceImpl();
    cachedAccounts = new WeakHashMap<>();
}

/**
 * Retrieves all cached accounts
 *
 * @return all accounts created on crm
 */
public Collection<Account> getAccounts() {
    return cachedAccounts.values();
}

/**
 * Create a new account
 *
 * @param account account
 * @return if the account already exists it returns as null, if not returns a newly account
 */
public Account create(Account account) {
    Account createdAccount = accountService.create(account);
    cachedAccounts.putIfAbsent(createdAccount.getId(), createdAccount);

    return createdAccount;
}

/**
 * Gets the account by it's id and deletes
 *
 * @param id account id
 * @return account deleted
 */
public Account delete(String id) {
    cachedAccounts.computeIfPresent(id, (presentId, account) -> cachedAccounts.remove(presentId));
    return accountService.delete(id);
}

/**
 * Get the account by it's id
 *
 * @param id account id
 * @return account found by id
 */
public Account getAccountById(String id) {
    return cachedAccounts.containsKey(id) ? cachedAccounts.get(id) : accountService.getAccountById(id);
}

public void setAccountService(AccountService accountService) {
    this.accountService = accountService;
}

public static AccountManager getInstance() {
    if (instance == null) {
        synchronized (AccountManager.class) {
            if (instance == null) {
                instance = new AccountManager();
            }
        }
    }
    return instance;
}
}

帐户服务:

public class AccountServiceImpl implements AccountService {

private RestTemplate restTemplate;

public AccountServiceImpl() {
    restTemplate = new RestTemplate();
}

@Override
public List<Account> getAccounts(){
    ResponseEntity<List<Account>> response = restTemplate.exchange(
            Endpoints.USERS.getUrl(),
            HttpMethod.GET,
            null,
            new ParameterizedTypeReference<List<Account>>() {
            });

    List<Account> accounts = response.getBody();

    if(accounts == null){
        return null;
    }

    return accounts;
}

@Override
public Account create(Account account) {
    return restTemplate.postForObject(Endpoints.CREATE_USER.getUrl(), account, Account.class);
}

@Override
public Account delete(String id) {
    Account toDelete = getAccountById(id);

    if(toDelete == null){
        return null;
    }

    restTemplate.delete(Endpoints.DELETE_USER_BY_ID.getUrl(), toDelete.getId(), id);

    return toDelete;
}

@Override
public Account getAccountById(String id) {
    return restTemplate.getForObject(Endpoints.USERS_BY_ID.getUrl(), Account.class, id);
}
}

1 个答案:

答案 0 :(得分:2)

Log4j那里有一个问题,报告为https://issues.apache.org/jira/browse/LOG4J2-2129。 请使用2.9,或使用2.10使用该故障单中的解决方法,或使用2.11.1