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!");
}

}

IntegrationTestService.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();
     }
   ...
}

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>

私が間違っていることについて何か考えはありますか?

答え:

テスト.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

<?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 は、Spring Bean でのみ機能します。

于 2012-10-22T11:20:27.200 に答える
2

オブジェクトは自分で作成しているため、Spring Bean ではありません。

applicationContext を初期化するコードはありません。 @ContextConfiguration、知る限り、単体テストにのみ使用されます。

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 に答える