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