小编jcg*_*cia的帖子

如何将@Autowired构造函数参数单独设置为"required = false"

@Autowired@Configuration类构造函数下使用注释.

@Configuration
public class MyConfiguration {

   private MyServiceA myServiceA;
   private MyServiceB myServiceB

   @Autowired
   public MyConfiguration(MyServiceA myServiceA, MyServiceB myServiceB){
     this.myServiceA = myServiceA;
     this.myServiceB = myServiceB;    
   }
}
Run Code Online (Sandbox Code Playgroud)

作为Spring文档sais,我能够声明是否需要带注释的依赖项.

如果我@Autowired在构造函数下标记注释required=false,我是说要自动装配的两个服务不是必需的(如Spring文档所示):

@Autowired(required = false)
public MyConfiguration(MyServiceA myServiceA, MyServiceB myServiceB){
  this.myServiceA = myServiceA;
  this.myServiceB = myServiceB;   
}
Run Code Online (Sandbox Code Playgroud)

从Spring文档:

在多参数方法的情况下,'required'参数适用于所有参数.

如何required单独为每个构造函数参数设置属性?是否有必要@Autowired在每个领域下使用注释?

问候,

spring constructor autowired spring-boot

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

@ExceptionHandler 不会捕获从 Spring Formatters 抛出的异常

我有一个控制器,其中包含一个 show 方法来显示有关所提供实体的信息。

@Controller
@RequestMapping("/owners")
public class OwnersController {

  @RequestMapping(value = "/{owner}", method = RequestMethod.GET,
          produces = MediaType.TEXT_HTML_VALUE)
  public String show(@PathVariable Owner owner, Model model) {
    // Return view
    return "owners/show";
  }
}
Run Code Online (Sandbox Code Playgroud)

要调用此操作,我使用http://localhost:8080/owners/1 URL。如您所见,我提供了所有者标识符 1。

为了能够将标识符 1 转换为有效的 Owner 元素,必须定义 Spring Formatter 并将其注册到addFormatters方法 from 上WebMvcConfigurerAdapter

我有以下几点OwnerFormatter

public class OwnerFormatter implements Formatter<Owner> {

  private final OwnerService ownerService;
  private final ConversionService conversionService;

  public OwnerFormatter(OwnerService ownerService,
      ConversionService conversionService) {
    this.ownerService = ownerService;
    this.conversionService = conversionService; …
Run Code Online (Sandbox Code Playgroud)

java spring exception-handling spring-mvc spring-boot

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

使用"server.context-parameters.*"定义的属性在Java EE服务器上不起作用

我们已经生成了一个基本的Spring Boot应用程序来测试一些功能.

准备将它部署在嵌入式服务器和Java EE服务器(Tomcat 7和JBoss EAP 6.2)上,而不应用任何更改.

server.context-parameters.*application.properties文件中包含了属性.

如果我使用java -jar或在嵌入式服务器上部署应用程序mvn spring-boot:run,它可以正常工作.但是,如果我在Tomcat 7或JBoss EAP 6.2上部署相同的应用程序,我无法正确加载context-params.

  • 在不使用web.xml文件的情况下,在Java EE容器上部署的Spring Boot应用程序上定义上下文参数的正确方法是什么?

您可以在此处查看与此Spring Boot问题相关的所有调试信息

java jboss spring tomcat spring-boot

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

Spring Boot上的自定义配置位置和配置名称

我正在尝试使用spring.config.namespring.config.location属性自定义Spring Boot配置位置和配置名称,就像我在Spring Boot参考指南中看到的那样

我已经创建了一个Spring Boot基本应用程序来测试它.

我可以使用OS环境变量export SPRING_CONFIG_NAME=custom和/或来自定义它export SPRING_CONFIG_LOCATION=classpath:/custom/location.properties.这很好用!

但我想知道,如果可以spring.config.name=custom在默认情况下定义application.properties,然后创建一个custom.properties文件,我将能够定义所有应用程序配置属性.

我已经检查了它,似乎它不能定义spring.config.name属性application.properties...但我想知道这是否是一个有效的方法来做到这一点之前在gitHub上创建一个问题.

问候,

java spring spring-boot

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