小编Kar*_*cki的帖子

Spring-Boot多模块项目加载属性文件

我有一个Spring-Boot-Application作为maven中的multimodule-Project.结构如下:

Parent-Project
|--MainApplication
|--Module1
|--ModuleN
Run Code Online (Sandbox Code Playgroud)

MainApplication项目中有main()方法类注释用@SpringBootApplication等等.此项目一如既往地自动加载application.properties文件.所以我可以使用@Value注释访问值

@Value("${myapp.api-key}")
private String apiKey;
Run Code Online (Sandbox Code Playgroud)

在我的Module1中,我也想使用属性文件(称为module1.properties),其中存储了模块配置.只能在模块中访问和使用此文件.但是我无法加载它.我尝试过@Configuration,@PropertySource但没有运气.

@Configuration
@PropertySource(value = "classpath:module1.properties")
public class ConfigClass {
Run Code Online (Sandbox Code Playgroud)

如何使用Spring-Boot加载属性文件并轻松访问这些值?找不到有效的解决方案.

我的配置

@Configuration
@PropertySource(value = "classpath:tmdb.properties")
public class TMDbConfig {

    @Value("${moviedb.tmdb.api-key}")
    private String apiKey;

    public String getApiKey() {
        return apiKey;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
Run Code Online (Sandbox Code Playgroud)

调用配置

@Component
public class TMDbWarper {

@Autowired
private TMDbConfig tmdbConfig;

private TmdbApi tmdbApi;

public TMDbWarper(){
    tmdbApi = …
Run Code Online (Sandbox Code Playgroud)

java spring properties-file spring-boot

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

出版和阅读非易变性领域

public class Factory {
    private Singleton instance;
    public Singleton getInstance() {
        Singleton res = instance;
        if (res == null) {
            synchronized (this) {
                res = instance;
                if (res == null) {
                    res = new Singleton();
                    instance = res;
                }
            }
        }
        return res;
    }
}
Run Code Online (Sandbox Code Playgroud)

它几乎是正确的线程安全实现Singleton.我看到的唯一问题是:

thread #1被初始化的instance字段可以发布之前就被完全初始化.现在,第二个线程可以读取instance不一致的状态.

但是,就我而言,这只是问题所在.这只是问题吗?(而且我们可以变得instance不稳定).

java volatile

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

Jackson 无法将 String 反序列化为 Joda-Time

我正在使用 Spring Boot 1.5.6 和 Jackson 2.8.8。反序列化 REST 调用的答案时,Jackson 失败并出现以下异常:

JSON 解析错误:无法构造 org.joda.time.DateTime 的实例:没有从字符串值反序列化的字符串参数构造函数/工厂方法 ('2018-03-19T12:05:21.885+01:00')

确实没有 String 构造函数,对象中只有一个 Object 构造DateTime函数。

jackson-datatype-joda在 build.gradle 文件中包含了依赖项。这些是 build.gradle 中的相应行:

compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: jacksonVersion
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion
compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: jacksonVersion
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: jacksonVersion
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda', version: jacksonVersion
Run Code Online (Sandbox Code Playgroud)

我需要做任何额外的配置吗?

PS:如果我将日期字符串放入 anew DateTime("2018-03-19T12:05:21.885+01:00")它工作正常。

有任何想法吗?干杯!

java json jodatime jackson spring-boot

12
推荐指数
1
解决办法
6367
查看次数

Mysql jconnector在com.myql.jdbc.utils.ReadAheadInputStream.fill()中花费50%的时间

我正在分析我的应用程序,它使用Spring,Hibernate和mysql-java-connector.VisualVM显示,com.myql.jdbc.utils.ReadAheadInputStream.fill()当有1000个并行连接正在读取时,超过50%的CPU时间花在方法上.

是否有任何优化使其更快?

java mysql hibernate jdbc

11
推荐指数
1
解决办法
2298
查看次数

Hibernate失去连接

我正在使用hibernate连接我的mysql数据库并执行事务.

我在整个应用程序中使用单个SessionFactory,并且我没有与数据库的其他连接,但是,我收到以下异常:

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3008)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3466)
    ... 21 common frames omitted
Wrapped by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 526 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3556)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3897) …
Run Code Online (Sandbox Code Playgroud)

java dao hibernate

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

如何使用设置的上次修改日期和时间触摸当前时间的文件夹?

我正在尝试更新特定文件夹的上次修改日期,这是我得到的:

public void touchFolder(){
    File folderToTest = new File("C:\\Temp");
    SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
    String newTime = dateFormatUtc.format(new Date());
    folderToTest.setLastModified(Long.parseLong(newTime));
}
Run Code Online (Sandbox Code Playgroud)

我只是将此代码放在测试用例中,所以不要担心调用此方法等.

我在解析日期格式时遇到错误,设置上次修改日期和时间时使用的格式是什么?

java

10
推荐指数
3
解决办法
571
查看次数

Lombok toBuilder() 方法是否创建字段的深层副本

toBuilder()在一个对象实例上使用来创建一个构建器实例,然后使用 build 方法来创建新实例。原始对象有一个列表,新对象是否引用了相同的列表或它的副本?

@Getter
@Setter
@AllArgsConstructor
public class Library {

    private List<Book> books;

    @Builder(toBuilder=true)
    public Library(final List<Book> books){
         this.books = books;
    }

}

Run Code Online (Sandbox Code Playgroud)
Library lib2  = lib1.toBuilder().build();
Run Code Online (Sandbox Code Playgroud)

lib2 书籍会引用与 lib1 书籍相同的列表吗?

java lombok

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

Java字节数组压缩

我正在尝试使用 javaDeflaterOutputStreamInflaterOutputStream类来压缩字节数组,但两者似乎都无法正常工作。我认为我错误地实施了它们。

public static byte[] compress(byte[] in) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream defl = new DeflaterOutputStream(out);
        defl.write(in);
        defl.flush();
        defl.close();

        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(150);
        return null;
    }
}

public static byte[] decompress(byte[] in) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InflaterOutputStream infl = new InflaterOutputStream(out);
        infl.write(in);
        infl.flush();
        infl.close();

        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(150);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我用来压缩和解压缩字节数组的两种方法。我在网上看到的大多数实现都使用固定大小的缓冲区数组来进行解压缩部分,但如果可能的话,我希望避免这种情况,因为如果我想要任何缓冲区数组,我需要使该缓冲区数组的大小为 1显着压缩。

如果有人可以向我解释我做错了什么,我将不胜感激。另外,为了解释为什么我知道这些方法无法正常工作:它输出的“压缩”字节数组始终大于未压缩的字节数组,无论我尝试提供什么大小的字节数组。

java compression arrays

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

Spring Boot请求标头返回null值

我有一个spring boot项目,里面有一些Rest API.我有两个自定义标题命名request_datetenant分别.

我试图在拦截器中读取这些头的值,但它只读取值tenant并返回null request_date.

重要

  • 我使用过滤器来包装请求对象,因为我想稍后读取请求主体.
  • 有一个过滤器可以添加CORS标头.

当我在localhost上运行我的项目并调试代码时,我成功地读取了两个标头的值.

但是,当我在生产中部署我的应用程序并使用postman或其他客户端发出请求时,request_date标头的值始终读为null.

我不确定这似乎是什么问题.我正在使用Spring boot v1.5.10.RELEASEJDK 1.8

注意:

  • 我试图将标题重命名为input_date.但是,它仍然读为null.

以下是相关代码

TestInterceptor

public class TestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestDate = request.getHeader("request_date");
        String tenant = request.getHeader("Tenant");

        /*Perform some checks*/

        return super.preHandle(request, response, handler);
    }
}
Run Code Online (Sandbox Code Playgroud)

CorsFilter

public class ApiCorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse …
Run Code Online (Sandbox Code Playgroud)

java rest spring http spring-boot

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

从方法引用的java.lang.IllegalArgumentException在类加载器中不可见

当通过/ target / class / .....中的wsimport为WS服务生成存根时,并使用devtools运行spring boot应用程序时,我获得了异常。

由以下原因引起:java.lang.IllegalArgumentException:com ....在类加载器中看不到方法引用的服务

我发现spring devtools类加载器RestartClassLoader出现问题,因为对类的两个不同引用(RestartClassLoader和AppClassLoader

private static void ensureVisible(ClassLoader ld, Class<?> c) {
    Class<?> type = null;
    try {
        type = Class.forName(c.getName(), false, ld);
    } catch (ClassNotFoundException e) {
        if (type != c) {
            throw new IllegalArgumentException(c.getName() +
                    " referenced from a method is not visible from class loader");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在spring-devtools.properties中添加对jar文件的引用以重新启动.include = / ..... jar

Spring Boot 2.0.0.RELEASE Java 9

java spring-boot spring-boot-devtools

8
推荐指数
1
解决办法
1682
查看次数