私は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がトリガーされます。
可能であれば、どのアノテーションとどのパスを使用すればよいですか?
のようになめらかに書くことができるようになります。何かアノテーションを処理する必要があります。それは春ですSpringに 'ApplicationContext'を初期化することによってそれを行うように指示しなければ、それをどうやって知っているのでしょうか? – Savior
Springベースのアプリケーションのエントリポイントは 'ApplicationContext'です。 – Savior
また、Springは '@ Autowired'を' static'で何もしません。 – Savior