2017-10-27 6 views
1

런타임 중에 bean 속성 값을 변경하려고합니다.런타임 중에 스프링 콩을 다시로드하십시오.

WebConfig

@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; 
    } 
} 

컨트롤러

@Controller 
public class SecurityPolicyController { 

    @Autowired 
    private SecurityPolicy policy; 

    @Autowired 
    private ApplicationContext context; 

    @Autowired 
    private SecurityService service; 

    @RequestMapping(value = "/security") 
    public ModelAndView update() { 

     ModelAndView model = new ModelAndView(); 

     //set data to db, it works fine aswell 
     service.setMaxAttempts(7); 

     //now i am trying to reload my beans 
     ((ConfigurableApplicationContext)context).refresh(); 

     //something reloading but i still get the same value 
     System.out.println(policy.getMaxLoginAttempts()); 
     model.setViewName("security"); 
     return model; 

    } 
} 

값 변경은 서버가 재부팅 될 때만 발생한다. 런타임 중에 빈 재로드를 수행하는 방법을 제안하거나 내가 뭘 잘못하고 있는지 말할 수 있습니까? 모든 도움을 주셨습니다

답변

1

servicepolicy에 삽입하지 않으시겠습니까? policy.getMaxLoginAttempts()으로 전화 할 때마다 전화가 service.getMaxAttempts()으로 위임됩니다. 다시로드 할 필요없이 새로운 값이 반환됩니다.

는 그래서 설정은 다음과 같습니다

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

그리고이 같은 SecurityPolicy.getMaxLoginAttempts() :

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

SecurityPolicy와 콩이 다른 서비스에 주입,이 서비스에이 방법 난 아직 – Coder

+0

곳을 이전 값이 서비스는 그 가치를 얻습니까? 정적 소스? – Lino

+0

SecurityPolicy bean – Coder

관련 문제