由于某种原因,我尝试使用 @Autowired 实例化的对象指针从未实例化。我试过看几个例子,但似乎没有任何效果!这是我的代码:
测试.java
package com.example.core.service.integration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations={"/app-context.xml"})
public class Testing {
@Autowired
private IntegrationRestService integrationRestService;
public static void main(String args[]) {
Testing t = new Testing();
t.checkNull();
}
private void checkNull() {
if(integrationRestService == null) System.err.println("FAIL...");
else System.out.println("SUCCESS!");
}
}
集成测试服务.java
public interface IntegrationRestService {
public FindSomething getFindSomethingResponse(String a, int b, int c);
public FindSomethingElse getFindSomethingElseResponse(String urlToRead);
}
IntegrationRestServiceImpl.java
@Service
@Path("/test")
public class IntegrationRestServiceImpl implements IntegrationRestService {
public IntegrationRestServiceImpl() {
super();
}
...
}
应用程序上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
default-autowire="constructor"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- JSR-303 support will be detected on classpath and enabled automatically -->
<context:annotation-config/>
<context:component-scan base-package="com.example.core"/>
<bean id="IntegrationRestService" class="com.example.core.service.integration.IntegrationRestServiceImpl" />
<bean id="Testing" class="com.example.core.service.integration.Testing" />
</beans>
任何想法我做错了什么?
回答:
测试.java
@Service
public class Testing {
@Autowired
private IntegrationRestService integrationRestService;
public static void main(String args[]) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
Testing testing = (Testing) context.getBean(Testing.class);
testing.checkNull();
}
private void checkNull() {
if(integrationRestService == null) System.err.println("FAIL...");
else System.out.println("SUCCESS!");
}
}
应用程序上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
default-autowire="constructor"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- JSR-303 support will be detected on classpath and enabled automatically -->
<context:annotation-config/>
<context:component-scan base-package="com.example.core"/>
<bean id="testing" class="com.example.core.service.integration.Testing"/>
<bean id="integrationRestService" class="com.example.core.service.integration.IntegrationRestServiceImpl" />
</beans>