自动装配,值注释不起作用
Autowired , Value annotation is not working
我在下面使用 class 并且我的@Autowired、@Value 注释不适用于 spring 启动应用程序。此外,我将 init 方法与 @PostConstruct 一起使用,并在应用程序启动时初始化值。然后在使用变量之前,该值被初始化为 0 或 null。
这是我的代码:
@Component
public class OsmXmlClient {
private static final Logger log =
LoggerFactory.getLogger(OsmXmlClient.class);
/*@Value("${osm.xml.service.url}") */
private String osmXmlServiceUrl = "http://myurl/XMLAPI";
/*#@Value("${osm.xml.service.userId}") */
private String osmXmlServiceUserId = "abc";
/*@Value("${osm.xml.service.password}")*/
private String osmXmlServicePassword = "abc123";
private Map<Integer, String> urlMap = new HashMap<>();
private Random random = new Random();
private int seviceUrlsCount = 0;
@PostConstruct
public void init() {
List<String> servieUrlList = Arrays.asList(osmXmlServiceUrl.split(";"));
log.info("servieUrlList = " + servieUrlList);
seviceUrlsCount = servieUrlList.size();
log.info("seviceUrlsCount in init = " + seviceUrlsCount);
for(int i=0;i<seviceUrlsCount;i++) {
log.info("servieUrlList.get(i) = " + servieUrlList.get(i));
urlMap.put(i, servieUrlList.get(i));
}
}
private String getServiceUrl() {
log.info("serviceUrlsCount at getServiceUrl = " + seviceUrlsCount);
return urlMap.get(random.nextInt(seviceUrlsCount));
}
}
这是日志:
[2020-07-31 17:07:30.373] [INFO] [Context:TomcatWebServer] [] [Tomcat initialized with port(s): 9014 (http)]
[2020-07-31 17:07:30.903] [DEBUG] [Context:OsmModuleApplication] [] [osmProperties = OsmProperties()]
[2020-07-31 17:07:30.920] [INFO] [Context:OsmXmlClient] [] [servieUrlList = [http://myurl/XMLAPI]]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [seviceUrlsCount in init = 1]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [servieUrlList.get(i) = http://myurl/XMLAPI]
[2020-07-31 17:07:31.041] [INFO] [Context:OrderController] [] [OrderControllerInit called]
然后我得到了这个日志:
[2020-07-31 17:07:45.403] [INFO] [Context:OrderDetailsService] [] [Entering
retryOrderService with billerorderId = 22428040]
[2020-07-31 17:07:45.403] [INFO] [Context:OsmXmlClient] [] [serviceUrlsCount at getServiceUrl = 0]
[2020-
我之前遇到过@Autowired 和@Value 注解问题。但我有一个解决方法。我想知道为什么会发生这种情况以及解决方法是什么。
这是我的服务 class 调用 OsmXmlClient class:
@Service
public class OrderDetailsService {
private static final Logger log =
LoggerFactory.getLogger(OrderDetailsService.class);
OSMClient osmClient = new OSMClient();
OsmXmlClient osmXmlClient = new OsmXmlClient();
OSMProvAdService provAdService = new OSMProvAdService();
CommonService commonService = new CommonService();
public RetryOrderResponseType retryOrderService(String orderId) {
log.info("Entering retryOrderService with billerorderId = " + orderId);
String billerOrderId = "";
String orderProcessHistory = osmXmlClient.queryOrder(orderId);
log.info("orderProcessHistory = " + orderProcessHistory);
RetryOrderResponseType retryOrderResponse = null;
List<GetOrderResponseType> orderResponseTypes = new ArrayList<>();
FindOrderResponseType findOrderResponseType = null;
return retryOrderResponse;
}
}
如您所见,我必须使用 new 关键字而不是自动装配来实例化。
@Value 属性不会被 Spring 初始化,除非组件被 Spring 初始化。如果你想让依赖注入、属性 注入等工作,不要自己用 new
实例化 类。相反,使用 @Autowired 提供 Component
.
的实例
我在下面使用 class 并且我的@Autowired、@Value 注释不适用于 spring 启动应用程序。此外,我将 init 方法与 @PostConstruct 一起使用,并在应用程序启动时初始化值。然后在使用变量之前,该值被初始化为 0 或 null。
这是我的代码:
@Component
public class OsmXmlClient {
private static final Logger log =
LoggerFactory.getLogger(OsmXmlClient.class);
/*@Value("${osm.xml.service.url}") */
private String osmXmlServiceUrl = "http://myurl/XMLAPI";
/*#@Value("${osm.xml.service.userId}") */
private String osmXmlServiceUserId = "abc";
/*@Value("${osm.xml.service.password}")*/
private String osmXmlServicePassword = "abc123";
private Map<Integer, String> urlMap = new HashMap<>();
private Random random = new Random();
private int seviceUrlsCount = 0;
@PostConstruct
public void init() {
List<String> servieUrlList = Arrays.asList(osmXmlServiceUrl.split(";"));
log.info("servieUrlList = " + servieUrlList);
seviceUrlsCount = servieUrlList.size();
log.info("seviceUrlsCount in init = " + seviceUrlsCount);
for(int i=0;i<seviceUrlsCount;i++) {
log.info("servieUrlList.get(i) = " + servieUrlList.get(i));
urlMap.put(i, servieUrlList.get(i));
}
}
private String getServiceUrl() {
log.info("serviceUrlsCount at getServiceUrl = " + seviceUrlsCount);
return urlMap.get(random.nextInt(seviceUrlsCount));
}
}
这是日志:
[2020-07-31 17:07:30.373] [INFO] [Context:TomcatWebServer] [] [Tomcat initialized with port(s): 9014 (http)]
[2020-07-31 17:07:30.903] [DEBUG] [Context:OsmModuleApplication] [] [osmProperties = OsmProperties()]
[2020-07-31 17:07:30.920] [INFO] [Context:OsmXmlClient] [] [servieUrlList = [http://myurl/XMLAPI]]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [seviceUrlsCount in init = 1]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [servieUrlList.get(i) = http://myurl/XMLAPI]
[2020-07-31 17:07:31.041] [INFO] [Context:OrderController] [] [OrderControllerInit called]
然后我得到了这个日志:
[2020-07-31 17:07:45.403] [INFO] [Context:OrderDetailsService] [] [Entering
retryOrderService with billerorderId = 22428040]
[2020-07-31 17:07:45.403] [INFO] [Context:OsmXmlClient] [] [serviceUrlsCount at getServiceUrl = 0]
[2020-
我之前遇到过@Autowired 和@Value 注解问题。但我有一个解决方法。我想知道为什么会发生这种情况以及解决方法是什么。
这是我的服务 class 调用 OsmXmlClient class:
@Service
public class OrderDetailsService {
private static final Logger log =
LoggerFactory.getLogger(OrderDetailsService.class);
OSMClient osmClient = new OSMClient();
OsmXmlClient osmXmlClient = new OsmXmlClient();
OSMProvAdService provAdService = new OSMProvAdService();
CommonService commonService = new CommonService();
public RetryOrderResponseType retryOrderService(String orderId) {
log.info("Entering retryOrderService with billerorderId = " + orderId);
String billerOrderId = "";
String orderProcessHistory = osmXmlClient.queryOrder(orderId);
log.info("orderProcessHistory = " + orderProcessHistory);
RetryOrderResponseType retryOrderResponse = null;
List<GetOrderResponseType> orderResponseTypes = new ArrayList<>();
FindOrderResponseType findOrderResponseType = null;
return retryOrderResponse;
}
}
如您所见,我必须使用 new 关键字而不是自动装配来实例化。
@Value 属性不会被 Spring 初始化,除非组件被 Spring 初始化。如果你想让依赖注入、属性 注入等工作,不要自己用 new
实例化 类。相反,使用 @Autowired 提供 Component
.