@Autowired和@Value注解不起作用

huangapple go评论83阅读模式
英文:

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(&quot;${osm.xml.service.url}&quot;) */
	private String osmXmlServiceUrl = &quot;http://myurl/XMLAPI&quot;;
	/*#@Value(&quot;${osm.xml.service.userId}&quot;) */
	private String osmXmlServiceUserId = &quot;abc&quot;;
	/*@Value(&quot;${osm.xml.service.password}&quot;)*/
	private String osmXmlServicePassword = &quot;abc123&quot;;
	
	private Map&lt;Integer, String&gt; urlMap = new HashMap&lt;&gt;();
	private Random random = new Random();
	private int seviceUrlsCount = 0;
	
	
	@PostConstruct
	public void init() {
		List&lt;String&gt; servieUrlList = Arrays.asList(osmXmlServiceUrl.split(&quot;;&quot;));
		log.info(&quot;servieUrlList = &quot; + servieUrlList);
		seviceUrlsCount = servieUrlList.size();
		log.info(&quot;seviceUrlsCount in init = &quot; + seviceUrlsCount);
		
		for(int i=0;i&lt;seviceUrlsCount;i++) {
			log.info(&quot;servieUrlList.get(i) = &quot; + servieUrlList.get(i));
			urlMap.put(i, servieUrlList.get(i));
		}
	} 
	
	private String getServiceUrl() {
		log.info(&quot;serviceUrlsCount  at getServiceUrl = &quot; + 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(&quot;Entering retryOrderService with billerorderId = &quot; + orderId);
	
	    String billerOrderId = &quot;&quot;;
	
	    String orderProcessHistory = osmXmlClient.queryOrder(orderId);
	    log.info(&quot;orderProcessHistory = &quot; + orderProcessHistory);
	
	    RetryOrderResponseType retryOrderResponse = null;
	    List&lt;GetOrderResponseType&gt; orderResponseTypes = new ArrayList&lt;&gt;();
	    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.

huangapple
  • 本文由 发表于 2020年8月1日 07:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63200205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定