For some reason the object pointer I am trying to instantiate with @Autowired is never instantiated. I've tried looking at several examples, but nothing seems to work! Here is my code:
出于某种原因,我试图用@Autowired实例化的对象指针从未实例化过。我试着看了几个例子,但似乎什么都不管用!这是我的代码:
Testing.java
Testing.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!");
}
}
IntegrationTestService.java
IntegrationTestService.java
public interface IntegrationRestService {
public FindSomething getFindSomethingResponse(String a, int b, int c);
public FindSomethingElse getFindSomethingElseResponse(String urlToRead);
}
IntegrationRestServiceImpl.java
IntegrationRestServiceImpl.java
@Service
@Path("/test")
public class IntegrationRestServiceImpl implements IntegrationRestService {
public IntegrationRestServiceImpl() {
super();
}
...
}
app-context.xml
app-context.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>
Any ideas what on I'm doing wrong?
你知道我做错了什么吗?
Answer:
Testing.java
Testing.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!");
}
}
app-context.xml
app-context.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>
4 个解决方案
#1
3
Try this:
试试这个:
@Service
public class Testing {
@Autowired
private IntegrationRestService integrationRestService;
public static void main(String args[]) {
final AbstractApplicationContext context = new ClassPathXmlApplicationContext("/app-context.xml");
Testing t = context.getBean(Testing.class);
t.checkNull();
}
private void checkNull() {
if(integrationRestService == null) System.err.println("FAIL...");
else System.out.println("SUCCESS!");
}
}
@Autowired works just with spring beans.
@Autowired只使用spring bean。
#2
2
Your object is not a Spring bean, since you're creating it yourself.
对象不是Spring bean,因为是您自己创建的。
You don't have any code to initialize your applicationContext. @ContextConfiguration
, AFAIK, is only used for unit testing.
您没有任何代码来初始化应用程序上下文。@ContextConfiguration, AFAIK只用于单元测试。
Spring is not magical, you need to invoke it before it works.
Spring不是神奇的,您需要在它工作之前调用它。
If you use a main method, you need to create your applicationContext yourself, then get your bean from it.
如果您使用一个主方法,您需要自己创建应用程序上下文,然后从它获取bean。
ApplicationContext ctx = new ClasspathXmlApplicationContext("/app-context.xml");
Testing testing = (Testing) ctx.getBean("testing");
#3
0
The beans will get injected only into spring-managed instances. Since you are creating an instance of Testing
using the new
operator, it won't get the integrationRestService
injected.
这些bean只会被注入到spring管理的实例中。由于您正在使用新操作符创建一个测试实例,所以它不会得到注入的integrationRestService。
what you can do is
你能做的就是
-
Get the instance of
testing
from the application context.从应用程序上下文获取测试实例。
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); Testing testing = (Testing) context.getBean(Testing.class);
- Use the
@Configurable
annotation onTesting
class so that instances created usingnew
operator will also be spring managed. But this is quite a cumbersome process. - 在测试类上使用@ configure注释,这样使用新操作符创建的实例也会被spring管理。但这是一个相当麻烦的过程。
#4
-1
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Testing testing = (Testing) context.getBean(Testing.class);
is well done!
做得好!
but,use
但是,使用
@Autowired
private Testing testing;
is fail.
是失败。
#1
3
Try this:
试试这个:
@Service
public class Testing {
@Autowired
private IntegrationRestService integrationRestService;
public static void main(String args[]) {
final AbstractApplicationContext context = new ClassPathXmlApplicationContext("/app-context.xml");
Testing t = context.getBean(Testing.class);
t.checkNull();
}
private void checkNull() {
if(integrationRestService == null) System.err.println("FAIL...");
else System.out.println("SUCCESS!");
}
}
@Autowired works just with spring beans.
@Autowired只使用spring bean。
#2
2
Your object is not a Spring bean, since you're creating it yourself.
对象不是Spring bean,因为是您自己创建的。
You don't have any code to initialize your applicationContext. @ContextConfiguration
, AFAIK, is only used for unit testing.
您没有任何代码来初始化应用程序上下文。@ContextConfiguration, AFAIK只用于单元测试。
Spring is not magical, you need to invoke it before it works.
Spring不是神奇的,您需要在它工作之前调用它。
If you use a main method, you need to create your applicationContext yourself, then get your bean from it.
如果您使用一个主方法,您需要自己创建应用程序上下文,然后从它获取bean。
ApplicationContext ctx = new ClasspathXmlApplicationContext("/app-context.xml");
Testing testing = (Testing) ctx.getBean("testing");
#3
0
The beans will get injected only into spring-managed instances. Since you are creating an instance of Testing
using the new
operator, it won't get the integrationRestService
injected.
这些bean只会被注入到spring管理的实例中。由于您正在使用新操作符创建一个测试实例,所以它不会得到注入的integrationRestService。
what you can do is
你能做的就是
-
Get the instance of
testing
from the application context.从应用程序上下文获取测试实例。
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); Testing testing = (Testing) context.getBean(Testing.class);
- Use the
@Configurable
annotation onTesting
class so that instances created usingnew
operator will also be spring managed. But this is quite a cumbersome process. - 在测试类上使用@ configure注释,这样使用新操作符创建的实例也会被spring管理。但这是一个相当麻烦的过程。
#4
-1
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Testing testing = (Testing) context.getBean(Testing.class);
is well done!
做得好!
but,use
但是,使用
@Autowired
private Testing testing;
is fail.
是失败。