我在Spring Boot项目中有几个类,有些使用@Autowired,有些则没有.我的代码如下:
Application.java(@Autowired works):
package com.example.myproject;
@ComponentScan(basePackages = {"com.example.myproject"})
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.example.myproject.repository")
@PropertySource({"classpath:db.properties", "classpath:soap.properties"})
public class Application {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public SOAPConfiguration soapConfiguration() {
SOAPConfiguration SOAPConfiguration = new SOAPConfiguration();
SOAPConfiguration.setUsername(environment.getProperty("SOAP.username"));
SOAPConfiguration.setPassword(environment.getProperty("SOAP.password"));
SOAPConfiguration.setUrl(environment.getProperty("SOAP.root"));
return SOAPConfiguration;
}
Run Code Online (Sandbox Code Playgroud)
HomeController(@Autowired works):
package com.example.myproject.controller;
@Controller
class HomeController {
@Resource
MyRepository myRepository;
Run Code Online (Sandbox Code Playgroud)
MyService(@Autowired不起作用):
package com.example.myproject.service;
@Service
public class MyServiceImpl implements MyService {
@Autowired
public SOAPConfiguration soapConfiguration; // is null
private void init() { …Run Code Online (Sandbox Code Playgroud) 我的问题非常非常简单,但我在网上发现的一切都告诉我,我正在以正确的方式做到这一点 - 但我显然误解了一些事情.
我有一个简单,简单的Java ListIterator,它在while-hasNext() - 循环中为next()返回一个null.以下是我对调试状态的评论代码:
[...]
ListIterator<Role> rolesIterator = currentUser.getRoles().listIterator();
// rolesIterator is now: java.util.ArrayList$ListItr
while( rolesIterator.hasNext() ) {
Role roleObject = rolesIterator.next(); // extra step for debugging reasons
String role = roleObject.getName(); // NullPointerException - roleObject is null
[...]
Run Code Online (Sandbox Code Playgroud)
在我的想法中,如果没有next()对象,则不应输入循环 - 这就是我使用hasNext()检查的原因.我理解错了什么,正确的方法是什么?
我尝试使用 Spring(-WS) 将 HTTP Basic Auth 凭据添加到我的 SOAP-Request 中。请求本身有效,但未提交任何凭据。HTTP 标头应如下所示:
[...]
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Authorization: Basic mybase64encodedtopsecretcredentials=
Run Code Online (Sandbox Code Playgroud)
但最后一行并没有丢失。在 MyConfig.java 中,我配置 Bean(无 XML):
@Bean
public WebServiceTemplate webServiceTemplate() {
WebServiceTemplate template = new WebServiceTemplate();
try {
template.setMarshaller(marshaller()); //Jaxb2Marshaller
template.setUnmarshaller(marshaller());
// proxy for tcpmon inspection
template.setDefaultUri("http://127.0.0.1:29080/target/webservice.php");
String username = environment.getProperty("config.username");
String password = environment.getProperty("config.password");
Credentials credentials = new UsernamePasswordCredentials(username, password);
HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
sender.setCredentials(credentials);
sender.afterPropertiesSet();
template.setMessageSender(sender);
} catch (Exception e) {
// @todo: handle me
}
return template; …Run Code Online (Sandbox Code Playgroud) java ×3
spring ×2
autowired ×1
iterator ×1
listiterator ×1
soap ×1
spring-boot ×1
spring-ws ×1
web-services ×1