小编dav*_*xxx的帖子

单例枚举与常量

我试图用很少的常量创建枚举.我希望枚举成为单身人士.使用下面的代码,我在eclipse中遇到编译错误:

语法错误,插入")"

在第5行完成方法声明.我无法找出错误.

public enum Days {

      SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;

      INSTANCE; // line 5

      public Days getInstance() {
        return INSTANCE;
     }
}
Run Code Online (Sandbox Code Playgroud)

java singleton enums

3
推荐指数
1
解决办法
270
查看次数

当我指定spring.config.location时,Spring Boot 2.x不会扫描application.properties

所以我只是在玩Spring Boot 2.0.4,我今天注意到了这一点。不知道我错过了什么。请帮我检查一下。

春季申请

@SpringBootApplication
@EnableScheduling
public class Application extends SpringBootServletInitializer {
Run Code Online (Sandbox Code Playgroud)

application.properties(位于src / main / resources中)

server.port=8088
Run Code Online (Sandbox Code Playgroud)

使用Intellij启动项目

2018-08-17 12:11:05 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8088 (http) with context path ''
Run Code Online (Sandbox Code Playgroud)

使用java命令行启动项目:

java -jar sample.jar --spring.config.location=D:\config\ --spring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud)

应用程序未使用配置的端口

2018-08-17 11:25:25 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
Run Code Online (Sandbox Code Playgroud)

看起来Spring Boot 2.0忽略了默认属性文件(请注意:此配置仅在applications.properties中,其他位置无,因此不会与其他配置文件重叠)

java spring spring-boot

3
推荐指数
1
解决办法
2248
查看次数

考虑在配置中定义类型为'com.ensat.services.ProductService'的bean

我有

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@ComponentScan(basePackages = {"hello","com.ensat.controllers"})
@EntityScan("com.ensat.entities")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

ProductController.java

package com.ensat.controllers;

import com.ensat.entities.Product;
import com.ensat.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Product controller.
 */
@Controller
public class …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

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

创建名为'dataSource'+ Spring Boot + Hibernate的bean时出错

关于如何访问mysql数据,我在启动spring指南时遇到了一些问题(请参阅此链接:https://spring.io/guides/gs/accessing-data-mysql/).我稍微调整了类,所以我有这个代码:

  1. 的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      http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.springframework</groupId>
        <artifactId>gs-mysql-data</artifactId>
        <version>0.1.0</version>
    
        <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    </parent>
    
    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <!-- Use MySQL Connector-J -->
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    </dependencies>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    </build>
    
    Run Code Online (Sandbox Code Playgroud)

我正在使用这个mainController: MainController.java

import org.springframework.beans.factory.annotation.Autowired;
import …
Run Code Online (Sandbox Code Playgroud)

java mysql spring maven

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

为什么不能扩展集合通用类型

我有一个实用方法,其目标是将a转换Collection为a Map.但是,当我尝试使用泛型时,它会出现编译错误,并且想知道如何修复它:

public static <K, V> Map<K, V> convertCollectionToMap(Collection<V extends ListToMapConvertable<K, V>> sourceList) {
    Map<K, V> newMap = new HashMap<K, V>();
    for (V item : sourceList) {
        newMap.put(item.getKey(item), item);
    }

    return newMap;
} 

public interface ListToMapConvertable<K, V> {
    public K getKey(V item);
}
Run Code Online (Sandbox Code Playgroud)

相反,我收到Incorrect number of arguments for type Collection<E>; it cannot be parameterized with arguments <V, ListToMapConvertable>编译错误.我不明白这个问题V只是延伸 ListToMapConverable.那么为什么它声称我使用了两个参数 - <V, ListToMapConvertable>?我该如何解决这个问题呢?

java generics collections

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

Spring Boot ConditionalOnBean 注解

有spring boot 2.0.2配置

@Configuration
public class ApiConfig {

    @Bean
    @Profile("!tests")
    @ConditionalOnProperty(name = "enabled", havingValue = "true")
    public MyService service() {
        return new MyServiceImpl();
    }

}
Run Code Online (Sandbox Code Playgroud)

...以及一些控制器,只有在初始化 MyService bean 时才应该创建并添加到应用程序上下文中。

@RestController
@ConditionalOnBean(MyService.class)
public class MyController {
   @Autowired
   private MyService service;
}
Run Code Online (Sandbox Code Playgroud)

它工作正常。但偶尔春季启动会跳过 MyController 创建。根据日志 MyService 被创建,但在任何其他 bean(包括所有控制器)之后,最后。

为什么 boot@Configuration之前不处理bean @RestController?谢谢。

java spring spring-boot

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

Maven在命令行找不到org.codehaus.mojo:exec-maven-plugin

我继承了一个孤立的Java 8 / Maven项目,并试图确定其构建方式。在自述文件中,我看到了运行以下命令的说明:

mvn -o -q -Dexec.executable="echo" -Dexec.args='${project.version}' org.codehaus.mojo:exec-maven-plugin:exec
Run Code Online (Sandbox Code Playgroud)

当我从我的项目根目录(包含pom.xml)运行该表单时,出现以下错误:

[ERROR] Error resolving version for plugin 'org.codehaus.mojo:exec-maven-plugin' from the repositories [local (/Users/myuser/.m2/repository), central (https://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the …
Run Code Online (Sandbox Code Playgroud)

java maven-plugin maven

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

仅使用流收集结果(不使用外部循环)

有没有办法只使用lambdas执行以下代码?

// translate someList1 to someList3
// .. get sublist
List<String> someList2 = someList1.stream()
        .map(i -> i.getField())
        .collect(Collectors.toList());
// .. create new (target) list
List<SomeClass> someList3 = new ArrayList<>();
for (String item : someList2) {
    SomeClass someObj = new SomeClass();
    someObj.setField(item);
    someList3.add(someObj);
}
Run Code Online (Sandbox Code Playgroud)

java lambda java-8

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

java.lang.IllegalArgumentException:无法在 JpaRepository 中创建查询方法

我使用 jpa、hibernate、spring boot - data、api REST 技术。

我有以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“walletRestService”的bean时出错:通过字段“walletRepository”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“walletRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: Failed to create query method public abstract java.lang.Long com.wj.dao.WalletRepository.createWallet(java.lang.Long,java.lang.String)!找不到类型 Wallet 的属性 createWallet!

这是我的代码:

实体用户:

package com.wj.entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
public class User implements Serializable{

@Id
@GeneratedValue
private Long id; 
private String name;

@OneToMany(mappedBy="user", fetch=FetchType.LAZY)
@JsonManagedReference
private List<Wallet> wallets = new ArrayList<>();

public User() {
    super();
}

public User(String name) …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa spring-data-jpa

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

如何避免Java中的方法重载以避免重复?

我有2个方法,它执行80%相同的工作,但结果处理不同.我在做 :

 private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, responseClass);
                                                          ^
            // DIFFERENCE --------------------------------|
        }

        return response;
    }

    private <T> T getResponse(final RestURI query, final TypeReference valueTypeRef) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, valueTypeRef);
                                                          ^
            // …
Run Code Online (Sandbox Code Playgroud)

java design-patterns coding-style

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