0

以下のようなスプリングレストアプリケーションがありRest Controllerます

@RestController
public class IngestorController
{
    @Autowired
    private IngestorService ingestorService;

    @RequestMapping( value = "/ingestor/createAndDeploy/{ingestorName}", method = RequestMethod.POST )
    public void createAndDeploy( @PathVariable String ingestorName )
    {
        ingestorService.createAndDeploy( ingestorName );
    }

}

同様に、私は以下のように持ってService Beanいます

@Service
public class IngestorService
{
    @Autowired
    private IngestorCommandBuilder ingestorCommandBuilder;

    private String uri;
    private DeployTemplate deployTemplate;

    public void init() throws URISyntaxException
    {
        deployTemplate = new DeployTemplate( new URI( uri ) );
    }

    @Transactional
    public void createAndDeploy( Ingestor ingestor )
    {
         //.....
     }

}

私はSpring config以下のショーを持っています

<bean id="ingestorCommandBuilder" class="org.amaze.server.ingestor.IngestorCommandBuilder" />

<bean id="ingestorService" class="org.amaze.server.service.IngestorService" init-method="init">
    <property name="uri" value="http://localhost:15217" />
</bean>

<bean id="ingestorController" class="org.amaze.server.controller.IngestorController"/>

アプリケーション コンテキストを開始しようとすると、アプリケーション コンテキストが開始され、IngestorService の init メソッドにヒットし、サービス Bean の deployTemplate オブジェクトも初期化されます。

ただし、この Bean は IngestorController に対して自動配線されません。postman から rest エンドポイントをヒットすると、サービス Bean の deployTemplate プロパティが null になっています。Controller の ingestorService 変数に割り当てられているオブジェクトは、init メソッドで呼び出されたオブジェクトとは別のオブジェクトです...

サービス Bean シングルトンを作成しようとしましたが (デフォルトのスコープがシングルトンであっても)、うまくいきません...

私がやっている間違いを見つけることができません..どんな提案もありがとう...

4

3 に答える 3

0

まず、次のものがあることを確認してください。

<context:annotation-config />

あなたの春の設定で。これで、いくつかの選択肢があります。

<context:component-scan base-package="your.package.com" />

コンポーネントをスキャンする、または

<!-- autowiring byName, bean name should be same as the property name annotate your ingestorservice with @Service("ingestorServiceByName") -->
<bean name="ingestorServiceByName" class="your.package.com.IngestorService" autowire="byName" />

<!-- autowiring byType, tif there are more bean of the same "general" type, this will not work and you will have to use qualifier or mark one bean as @Primary  -->
<bean name="ingestorServiceByType" class="your.package.com.IngestorService" autowire="byType" />

<!-- autowiring by constructor is similar to type, but the DI will be done by constructor -->
<bean name="ingestorServiceConstructor" class="your.package.com.IngestorService" autowire="constructor" />

問題の分析を容易にするために、完全な Spring 構成を含めてください。

于 2015-06-23T09:59:39.020 に答える
0

注釈ベースの構成を使用する場合、ほとんどの場合、すべての Bean をアプリケーション コンテキスト xml ファイルに記述する必要はありません。サービスを自動配線するために必要なのは注釈だけです。

init メソッドを適切に定義するには、@PostConstructアノテーションを使用します。プロパティは externat .properties ファイルに簡単に移動でき、@Value注釈付きでコードに挿入できます。

または、 とともに使用@Qualifier@Autowiredます。

于 2015-06-23T04:57:52.380 に答える
-1

注釈ベースの DI を使用している場合、Bean を XML で定義する必要はありません。

@PostConstruct を使用して、xml 構成の init-method を置き換えることができます。

使うだけ

    <context:component-scan base-package="..."/> <mvc:annotation-driven/>
于 2015-06-23T05:25:16.997 に答える