4

由于某种原因,我尝试使用 @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>
4

4 回答 4

3

试试这个:

@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 仅适用于弹簧豆。

于 2012-10-22T11:20:27.200 回答
2

您的对象不是 Spring bean,因为您是自己创建的。

您没有任何代码来初始化您的 applicationContext。 @ContextConfiguration,AFAIK,仅用于单元测试。

Spring 并不神奇,您需要在它工作之前调用它。

如果使用 main 方法,则需要自己创建 applicationContext,然后从中获取 bean。

ApplicationContext ctx = new ClasspathXmlApplicationContext("/app-context.xml");
Testing testing = (Testing) ctx.getBean("testing");
于 2012-10-22T11:22:34.023 回答
0

bean 只会被注入到 spring 管理的实例中。由于您正在创建一个Testing使用new运算符的实例,因此它不会被integrationRestService注入。

你能做的是

  • testing从应用程序上下文中获取实例。

    ClassPathXmlApplicationContext context = 
        new ClassPathXmlApplicationContext("Spring.xml");
    Testing testing = (Testing) context.getBean(Testing.class);
    
  • 在类上使用@Configurable注释,Testing以便使用new运算符创建的实例也将被弹簧管理。但这是一个相当繁琐的过程。
于 2012-10-22T11:20:15.297 回答
-1
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Testing testing = (Testing) context.getBean(Testing.class);

干得好!

但是,使用

@Autowired
private Testing testing;

是失败。

于 2013-10-08T16:44:05.887 回答