小编pok*_*ken的帖子

Spring boot从属性文件加载包列表

我使用 Spring boot 设置了项目,在启动时加载各个组件。每个单独的包都包含自己的数据源、进程等。我可以简单地使用它,它工作得很好

@SpringBootApplication(scanBasePackages = {
    "com.package1",
    "com.package2",
    "com.package3"
})
public class Application extends SpringBootServletInitializer{
    public static void main(String[] args){
        SpringApplication.run(Application.class,args)        
    }
}
Run Code Online (Sandbox Code Playgroud)

但目前,单个项目的数量正在变得越来越大。是否可以将要扫描的组件/包列表放入外部属性文件或 spring Vault 中?我不确定如何检索它,是否可以在启动前检索属性?


编辑:

目前我尝试过这个:

@Import(AppConfig.class)
public class Application extends SpringBootServletInitializer{
    public static void main(String[] args){
        SpringApplication.run(Application.class,args)        
    }
}


@Configuration
@ComponentScan(basePackages = {$app.packages})
@EnableAutoConfiguration
public class AppConfig {
}

//in my properties file
app.packages = ["com.package1","com.package2","com.package3"]
Run Code Online (Sandbox Code Playgroud)

但它不起作用

java package java-8 spring-boot spring-vault

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

Autowire all components in a package without implementing an interface

I got a project with tons of services and repositories. Currently each repository is autowired to a service using annotations.

@Service
public class Service1 {
    @Autowired
    private Repository1 repository1;

    @Autowired
    private Repository2 repository2;

    ...100+ more
}
Run Code Online (Sandbox Code Playgroud)

All of these repository are under the same package. Is it possible to skip declaration for each repository?

A simple solution I found would be to implement an interface like this:

@Autowired
private Map<String,RepositoryInterface> repositoryInterface

public void method1(){
    repositoryInterface.get("repository1").doMethod();
}
Run Code Online (Sandbox Code Playgroud)

It should have been …

java spring autowired

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

JSON Stringify用于具有相同名称params的表单

我需要从具有相同名称参数的多个字段的表单中创建一个JSON,但我想将其分组为数组?可能吗?

编辑:我尝试了建议的解决方案,但我不能将name的值用作新数组中的键...你可以帮我解决一下如何将其更改为{name:value []}而不是{name: name,value:value []}?

var json = JSON.stringify($("#frm").serializeArray());
$("#json").html(json);


var myArray = $("#frm").serializeArray();
var groups = {};
for (var i = 0; i < myArray.length; i++) {
  var groupName = myArray[i].name;
  if (!groups[groupName]) {
    groups[groupName] = [];
  }
  groups[groupName].push(myArray[i].value);
}
myArray = [];
for (var groupName in groups) {
  myArray.push({name: groupName, value: groups[groupName]});
}

var json = JSON.stringify(myArray);
$("#json").html(json);

var myArray2 = [];
for (var groupName in groups) {
  myArray2.push({groupName: groups[groupName]});
}


var json2 = JSON.stringify(myArray2);
$("#json2").html(json2);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json

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

Spring data jpa findOne 抛出 In CorrectResultSizeDataAccessException 异常

我正在使用 spring data jpa 并创建了一个扩展 JpaSpecificationExecutor 的存储库。然后我使用 findOne(specs) 方法。但我得到了这个例外:

IncorrectResultSizeDataAccessException: query did not return a unique result: 2
Run Code Online (Sandbox Code Playgroud)

Specs 查询确实返回多个结果,但我认为使用 findOne 会返回第一个结果?但我仍然收到错误。我想我使用了错误的方法。我应该使用哪种方法来获得第一个结果?

java database spring-data-jpa

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