常见问题汇总(七)——关于Spring

1、 applicationContext.xml路径问题

默认在web-inf下,开发中配置在src下,需要在web.xml中配置路径

注意,是classpath而不是classPath

2、 Failed to read candidate component class: 

Spring 版本过低,不支持当前JDK版本

3、jar部署方式中文件读写

可以采用相对路径的方式实现文件存储:

    public static String getExportFileName(String prefixFileName, FileType fileType) {
        String fileName = prefixFileName + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        return "./test" + "/" + fileName + "." + fileType.getExtension();
    }

    public static String saveImportFile(MultipartFile file, Long statementBatchId) {
        String saveFileName = getExportFileName("批次" + statementBatchId + "导入", FileType.XLS);
        File saveFile = new File(saveFileName);
        try {
            FileUtils.writeByteArrayToFile(saveFile, file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return saveFileName;
    }

4、多线程,或者事务过大,可以采用手动事务

     @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;


 TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
        try {

                save = packageStatementItemService.save(itemEntity);
                StopWatch watch = new StopWatch();
                watch.start();
                statementCheckingService.checkingPackageAfterImport(itemEntity, userId, userName);
                watch.stop();
                platformTransactionManager.commit(transactionStatus);
                log.info(LogTempalte.TIME_LOG_DEBUG,"包裹对账,单条重新匹配用时",watch.getTotalTimeSeconds());
        } catch (Exception e) {
            log.error(e.getMessage());
            error.add(String.format("系统异常【%s】", e.getMessage()));
            save = false;
            platformTransactionManager.rollback(transactionStatus);
        }

5、PKIX path building failed(SSL证书未被信任) 

解决方案:1、信任证书

keytool -importcert -alias "passport" -file /xx.crt  -keystore /srv/java/jdk1.8.0_171/jre/lib/security/cacerts -storepass changeit

                 2、忽略证书

    public void afterPropertiesSet() throws Exception {
        requestFactory = new HttpComponentsClientHttpRequestFactory(){
            private HttpClientContext context;
            {
                log.info("passport账号为:" + passportConfig.getApplicationAccessKey() + " 密码为:" + passportConfig.getApplicationAccessPassword());
                context = HttpClientContext.create();
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(org.apache.http.auth.AuthScope.ANY,
                        new org.apache.http.auth.UsernamePasswordCredentials(passportConfig.getApplicationAccessKey(), passportConfig.getApplicationAccessPassword()));
                context.setCredentialsProvider(credsProvider);
            }

            @Override
            protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
                return context;
            }
        };

        requestFactory.setHttpClient(getHttpsClient());
        restTemplate = new RestTemplate(requestFactory);
    }


    public static CloseableHttpClient getHttpsClient() {

        CloseableHttpClient httpClient;
        SSLContext sslContext = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException e) {
            e.getStackTrace();
        } catch (KeyManagementException e) {
            e.getStackTrace();
        } catch (KeyStoreException e) {
            e.getStackTrace();
        }
        httpClient = HttpClients.custom().setSSLContext(sslContext).
                setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
        return httpClient;
    }

6、com java.lang.NoSuchMethodError:XXX

通常是由于包冲突导致

解决方案有两个:a:去掉多余的版本  b:利用dependencyManager控制版本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值