我有一个配置类,其中设置了JavaMailSenderImpl
属性,但是我不想设置@Bean
中的值,而只是将其设置为通过方法(例如:{ {1}})。下面是代码,我尝试通过getter传递值,但得到npe。
mailSender.setPort(ClassImpl.getPort());
//properties.file
mail.host=the_host
mail.port=123
mail.username=the_username
mail.password=the_password
// ClassInterface has only one sendMail method
@Component
public class ClassImpl implements ClassInterface{
@Value("${mail.host}")
public String host;
@Value("${mail.port}")
private int port;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
@Autowired
JavaMailSenderImpl mailSender;
@Override
public void sendMail(String sender, String receiver, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(sender);
message.setTo(receiver);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
编辑
试图使此getHost @Configuration
@ComponentScan
public class SpringConfiguration {
@Bean
public JavaMailSenderImpl mailSenderService() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host_here);
mailSender.setPort(port_here);
mailSender.setUsername(user_here);
mailSender.setPassword(pwd_here);
return mailSender;
}
}
并通过如下方法:
static
答案 0 :(得分:0)
正如zapl所说,我在@Bean参数中添加了ClassImpl,它工作正常。
create table Table1 (
TestOne int identity(1,1) primary key not null,
FirstName nvarchar(50) not null,
);
create table Table2 (
TestTwo int identity(1,1) primary key foreign key references Table1(TestOne) not null,
FirstName nvarchar(50) not null,
);