2016-04-06 11 views
5

私はjarファイルにクラスMyFrontServiceを定義しているが、ここで私はそれを使用する方法である:ClassPathXmlApplicationContext.getBeanを置き換えるSpringアノテーションはありますか?

import blabla.MyFrontService; 

public class Main { 
    public static void main(String[] args) { 
     MyFrontService.doThis(); 
    } 
} 

FrontServiceは、他のサービスにアクセスするためのエントリポイントとして機能します。 現在定義されている(動作しています)。

MyFrontService.java:

public class MyFrontService { 

    public static void doThis(){ 

     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/springBeans.xml"); 
     MyService1 myService1 = (MyService1) context.getBean("myService1"); 
     myService1.doSomething(); 
     context.close(); 

    } 
} 

MyService1.java:

package blabla.service; 

@Service("myService1") 
public class MyService1 { 

    public void doSomething(){ 
     // some code 
    } 
} 

のsrc /メイン/リソース/ META-INF/springBeans.xml:

(...) 
<context:annotation-config /> 
<context:component-scan base-package="blabla.service" /> 
(...) 

私は希望MyFrontService.javaのコードを次のように置き換えます。

@MagicAnnotation("what_path?/springBeans.xml") 
public class MyFrontService { 

    @Autowired 
    private static MyService1 myService1; 

    public static void doThis(){ 
     myService1.doSomething();    
    } 
} 

私はこのウェブサイトなどで他の質問から多くを読みました。 @Configuration、@Importなどの注釈が使用されることもありますが、それらを動作させることはできません。たとえば、

@ImportResource("classpath:META-INF/springBeans.xml") 

を使用すると、myService1.doSomething()を呼び出すときにNullPointerExceptionがトリガーされます。

可能であれば、どのアノテーションとどのパスを使用すればよいですか?

+0

のようになめらかに書くことができるようになります。何かアノテーションを処理する必要があります。それは春ですSpringに 'ApplicationContext'を初期化することによってそれを行うように指示しなければ、それをどうやって知っているのでしょうか? – Savior

+0

Springベースのアプリケーションのエントリポイントは 'ApplicationContext'です。 – Savior

+1

また、Springは '@ Autowired'を' static'で何もしません。 – Savior

答えて

-1

クラスMyFrontServiceに@Componentアノテーションを付けておくと、実行中のすべての作業をやり遂げるという頭痛を救うことができます。注釈を追加したくない場合、春の設定の下にすべてを持ってほしくない場合は、以下のようにクラスを定義することができます。

import org.springframework.beans.BeansException; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 

public class SpringContextUtil implements ApplicationContextAware { 

    private static ApplicationContext applicationContext; 


    public static ApplicationContext getApplicationContext() { 
      return applicationContext; 
     } 


    @Override 
    public void setApplicationContext(
      org.springframework.context.ApplicationContext ctx) 
      throws BeansException { 
     applicationContext = ctx; 
    } 


} 

そして、あなたが行うことができます任意のクラスでは、

ApplicationContext ctx = SpringContextUtil.getApplicationContext(); 
     BeanClass av = ctx.getBean(BeanClass.class); 
+0

ここでは 'setApplicationContext'を呼び出していますか? – Savior

+0

あなたのディスパッチャーサーブレットにそのBeanを登録するだけで、それを設定する作業が必要です。 – chetank

+0

あなたはこれがサーブレットアプリケーションだと思いますか? – Savior

1

使用Spring Bootフレームワークと[いいえ、魔法が存在しないこの

@Controller 
@EnableAutoConfiguration 
public class SampleController { 

    @RequestMapping("/") 
    @ResponseBody 
    String home() { 
     return "Hello World!"; 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(SampleController.class, args); 
    } 
} 
関連する問題