Spring Boot没有在Jersey的ResourceConfig类中设置@Value属性

时间:2016-03-22 09:05:37

标签: java spring dependency-injection spring-boot jersey-2.0

为什么我的端口成员0在运行时的Jersey ResourceConfig类中?

Application.property文件:

#web server
server.port=9000

JerseyConfig文件:

@Component
@ApplicationPath(Pathes.APPLICATION_PATH)
public class JerseyConfig extends ResourceConfig {

    @Value("${server.port}")
    private int port;

    public JerseyConfig() {
        final BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setHost("localhost:" + port);
        beanConfig.setBasePath(Pathes.APPLICATION_PATH);
        beanConfig.setResourcePackage(myPackages);
        beanConfig.setScan(true);

        register(ApiListingResource.class);
        register(SwaggerSerializers.class);
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

在Spring容器初始化之前,您正在构造函数中使用port。你不能用Spring做到这一点。

改为构造函数注入:

@Component
@ApplicationPath(Pathes.APPLICATION_PATH)
public class JerseyConfig extends ResourceConfig {

    @Autowired
    public JerseyConfig(@Value("${server.port}") int port) {
        final BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setHost("localhost:" + port);
        beanConfig.setBasePath(Pathes.APPLICATION_PATH);
        beanConfig.setResourcePackage(myPackages);
        beanConfig.setScan(true);

        register(ApiListingResource.class);
        register(SwaggerSerializers.class);
    }
}