在运行时重新加载spring bean

时间:2017-10-27 11:28:57

标签: java spring applicationcontext

我正在尝试在运行时更改bean属性值。

WebConfig

<table id="soa_table" class="table table-striped table-bordered table-condensed soa-table">
<thead>
    <tr>
        <th>SoA</th>
        <th id="6" class="soa-column text-center">V1</th>
        <th id="102" class="soa-column text-center success">V2</th>
        <th id="103" class="soa-column text-center">V3</th>
        <th id="104" class="soa-column text-center">V4</th>
        <th id="105" class="soa-column text-center">V5</th>
        <th id="106" class="soa-column text-center">V6</th>
        <th id="107" class="soa-column text-center">V7</th>
        <th id="108" class="soa-column text-center">V8</th>
        <th id="109" class="soa-column text-center">V9</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="soa-row" id="2">Demographics (Pilot)</td>
        <td class="soa-element text-center" form_id="2" visit_id="6" id="18">
            <span class="glyphicon glyphicon-ok text-success"></span>
        </td>
        <td class="soa-element" form_id="2" visit_id="102" id="0">&nbsp;</td>
        <td class="soa-element text-center" form_id="2" visit_id="103" id="21">
            <span class="glyphicon glyphicon-ok text-success"></span>
        </td>
        <td class="soa-element" form_id="2" visit_id="104" id="0">&nbsp;</td>
        <td class="soa-element" form_id="2" visit_id="105" id="0">&nbsp;</td>
        <td class="soa-element" form_id="2" visit_id="106" id="0">&nbsp;</td>
        <td class="soa-element" form_id="2" visit_id="107" id="0">&nbsp;</td>
        <td class="soa-element" form_id="2" visit_id="108" id="0">&nbsp;</td>
        <td class="soa-element" form_id="2" visit_id="109" id="0">&nbsp;</td>
    </tr>
    <tr>
        <td class="soa-row success" id="6">Education (Pilot)</td>
        <td class="soa-element" form_id="6" visit_id="6" id="0">&nbsp;</td>
        <td class="soa-element text-center" form_id="6" visit_id="102" id="25">
            <span class="glyphicon glyphicon-ok text-success"></span>
        </td>
        <td class="soa-element" form_id="6" visit_id="103" id="0">&nbsp;</td>
        <td class="soa-element" form_id="6" visit_id="104" id="0">&nbsp;</td>
        <td class="soa-element text-center" form_id="6" visit_id="105" id="24">
            <span class="glyphicon glyphicon-ok text-success"></span>
        </td>
        <td class="soa-element" form_id="6" visit_id="106" id="0">&nbsp;</td>
        <td class="soa-element" form_id="6" visit_id="107" id="0">&nbsp;</td>
        <td class="soa-element" form_id="6" visit_id="108" id="0">&nbsp;</td>
        <td class="soa-element" form_id="6" visit_id="109" id="0">&nbsp;</td>
    </tr>
</tbody>

控制器

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Autowired
    private SecurityService service;

    @Bean
    public SecurityPolicy securityPolicy() {
        SecurityPolicy policy = new SecurityPolicy();

        //takes data from db, it works fine
        policy.setMaxAttempt = service.getMaxAttempts();
        return policy;
    }
}

仅在重新启动服务器时才会更改该值。 您能否建议如何在运行时实现bean重新加载或告诉我做错了什么?所有帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

为什么不将service注入policy?每次拨打policy.getMaxLoginAttempts()时,呼叫都会被委托给service.getMaxAttempts()。因此,您无需重新加载即可返回新值。

所以配置看起来像这样:

@Bean
public SecurityPolicy securityPolicy() {
    return new SecurityPolicy(service);
}

SecurityPolicy.getMaxLoginAttempts()就像这样:

public int getMaxLoginAttempts(){
    return service.getMaxAttempts();
}