java.lang.String和String(java)有什么区别



我对java还很陌生,但已经学习了很长时间。我目前正在用MyBatis学习Spring Boot,并遇到了这个令人沮丧的编译错误。我正在尝试实现这个哈希服务,在这个过程中,我试图捕捉并记录任何错误。问题是,当我尝试使用从mybatis导入的记录器来记录错误时,我会得到编译错误required type java.lang.StringProvided: String我之前的印象是这两个类是相同的,但我猜String类与java.lang.String类不一样?还是有其他事情发生了?这是你要看的实际代码。有人能帮我吗?

import org.apache.tomcat.util.codec.binary.Base64;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator;
import org.springframework.stereotype.Component;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.lang.String;
@Component
public class HashedService {
private Logger logger = LoggerFactory.getLogger(HashedService.class);
public String getHashedValue(String data, String salt) {
byte[] hashedValue = null;
KeySpec spec = new PBEKeySpec(data.toCharArray(), salt.getBytes(), 5000, 128);
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
hashedValue = factory.generateSecret(spec).getEncoded();
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
java.lang.String error = e.getMessage();
logger.error(error);
}
return Base64.encodeBase64String(hashedValue);
}
}

编辑我正在使用intellij

我使用

implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
// https://mvnrepository.com/artifact/org.mybatis/mybatis-spring
compile group: 'org.mybatis', name: 'mybatis-spring', version: '2.0.5'

我发现org.mybatis.logging.Logger中的方法error看起来像这样:

public void error(Supplier<String> s) {
log.error(s.get());
}

我认为你不应该使用MyBatis的记录器。

您可以尝试替换:

import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;

带有:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

最新更新