英文:
Autowired , Value annotation is not working
问题
I am using below class and my @Autowired, @Value annotation is not working for spring boot application. Also, I use init method with @PostConstruct and initialize the value when the application starts. Then right before use of the variables, the value gets initialized to 0 or null.
Here is my code:
@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 serviceUrlsCount = 0;
@PostConstruct
public void init() {
List<String> serviceUrlList = Arrays.asList(osmXmlServiceUrl.split(";"));
log.info("serviceUrlList = " + serviceUrlList);
serviceUrlsCount = serviceUrlList.size();
log.info("serviceUrlsCount in init = " + serviceUrlsCount);
for(int i=0;i<serviceUrlsCount;i++) {
log.info("serviceUrlList.get(i) = " + serviceUrlList.get(i));
urlMap.put(i, serviceUrlList.get(i));
}
}
private String getServiceUrl() {
log.info("serviceUrlsCount at getServiceUrl = " + serviceUrlsCount);
return urlMap.get(random.nextInt(serviceUrlsCount));
}
}
Here is the log:
[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] [] [serviceUrlList = [http://myurl/XMLAPI]]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [serviceUrlsCount in init = 1]
[2020-07-31 17:07:30.921] [INFO] [Context:OsmXmlClient] [] [serviceUrlList.get(i) = http://myurl/XMLAPI]
[2020-07-31 17:07:31.041] [INFO] [Context:OrderController] [] [OrderControllerInit called]
Then I got this log:
[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-
I had experienced the @Autowired and @Value annotation issue before. But I had a workaround. I would like to know why this is happening and what the solution to it is.
Here is my service class that calls the 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;
}
}
As you can see, I have to instantiate using the new
keyword instead of Autowiring.
英文:
I am using below class and my @Autowired, @Value annotation is not working for spring boot application. Also, I use init method with @PostConstruct and initialize the value when the application starts. Then right before use of the variables, the value gets intialized to 0 or null.
Here is my code:
@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));
}
}
Here is the log:
[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]
Then I got this log:
[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-
I had experienced the @Autowired and @Value annotation issue before . But I had a workaround. I would like to know why this is hapenning and what the solution to it is.
Here is my service class that call 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;
}
}
As you can see I have to instatiate using new keyword instead of Autowiring.
答案1
得分: 2
@Value属性在Spring中只有在组件由Spring初始化时才会被初始化。如果要进行依赖注入、属性注入等操作,请不要自己使用new
实例化类。相反,使用@Autowired来提供您的Component
的实例。
英文:
@Value properties are not initialized by Spring unless the component is initialized by Spring. Do not instantiate classes with new
yourself if you want dependency injection, property injection et cetera to work. Instead, use @Autowired to supply instances of your Component
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论