Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接本身具有构造函数的Bean_Java_Spring - Fatal编程技术网

Java 连接本身具有构造函数的Bean

Java 连接本身具有构造函数的Bean,java,spring,Java,Spring,我有一个web服务客户端,它有一个验证器类。验证器需要用户名/密码。正在寻找有关如何使用Spring注入凭据的帮助 我应该将user/pass注入验证器还是注入正在实例化验证器的客户端 任何具体的例子都将不胜感激,因为我是春天的新手 这是两个组件的外观: @Controller public class WSClient { @Autowired MyAuthenticator myAuthenticator; } } 具有以下凭据的身份验证程序

我有一个web服务客户端,它有一个验证器类。验证器需要用户名/密码。正在寻找有关如何使用Spring注入凭据的帮助

我应该将user/pass注入验证器还是注入正在实例化验证器的客户端

任何具体的例子都将不胜感激,因为我是春天的新手

这是两个组件的外观:

@Controller
    public class WSClient {
        @Autowired
        MyAuthenticator myAuthenticator;
    }
}
具有以下凭据的身份验证程序:

public class MyAuthenticator extends Authenticator {
    private final String userName;
    private final String passWord;

    public MyAuthenticator(String userName, String passWord) {
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(this.userName, this.passWord.toCharArray());
    }
}

使用
@Value
Authentication
bean中设置用户名/密码

@Component
public class MyAuthenticator extends Authenticator {
    @Value("${credentials.username}")
    private final String userName;
    @Value("${credentials.password}")
    private final String passWord;

    public MyAuthenticator(String userName, String passWord) {
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(this.userName, this.passWord.toCharArray());
    }
}
和在XML文件中



并将
凭证.properties
放在类路径中

使用
@Value
身份验证
bean中设置用户名/密码

@Component
public class MyAuthenticator extends Authenticator {
    @Value("${credentials.username}")
    private final String userName;
    @Value("${credentials.password}")
    private final String passWord;

    public MyAuthenticator(String userName, String passWord) {
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(this.userName, this.passWord.toCharArray());
    }
}
和在XML文件中


并将
credentials.properties
放入类路径中