Java:将List <string>转换为方法的输入(String,String,String)

时间:2015-07-19 12:21:02

标签: java string list methods input

希望标题是解释性的。

 1 public void commander(){
 2  Scanner input = new Scanner(System.in);
 3  System.out.println("\nWelcome to my application."
 4          + "\nType the command you want to use,"
 5          + "or type 'exit' to close the program.");
 6  while (input.next() != "exit") {
 7      List<String> userInput = Arrays.asList(input.next().split(" "));
 8      Class<? extends RunnableCommand> command = Commands.commandsMap().get(userInput.get(0));
 9      userInput.remove(0);
10      // command.run(userInput); ?!
11  }

第8行: RunnableCommand是一个提供run()方法的接口,由每个代表命令的类实现(受策略模式启发)。

第10行:这是问题所在。每个“命令类”的run()方法可以有1,2或3个字符串作为输入。

有没有办法将 userInput 的每个元素作为 run()方法的输入?

1 个答案:

答案 0 :(得分:2)

实现这样的run方法:

command.run(input1);
command.run(input1, input2);
command.run(input1, input2, input3);

您可以通过传递数组或逗号单独的参数来调用它。

command.run(userInput.toArray(new String[0]);

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
@Qualifier("loginService")
UserDetailsService loginService; //THIS IS THE POINT OF FAILURE

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .csrf().disable()
        .authorizeRequests()
            .antMatchers("/").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/")
            .usernameParameter("username")
            .passwordParameter("password")
            .defaultSuccessUrl("/userlist")
            .failureUrl("/")
            .permitAll()
            .and()
        .logout()
            .permitAll();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    /*auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");*/
    auth.userDetailsService(loginService).passwordEncoder(passwordEncoder());
}

@Override
public void configure(WebSecurity web) throws Exception {

            web
              .ignoring()
                 .antMatchers("/resources/**");

          }


@Bean
public Md5PasswordEncoder passwordEncoder(){
    Md5PasswordEncoder encoder = new Md5PasswordEncoder();
    return encoder;
}
}