Java 是否有替代ClassPathXmlApplicationContext.getBean的Spring注释?
我在jar中定义了一个类MyFrontService,下面是我想要如何使用它:Java 是否有替代ClassPathXmlApplicationContext.getBean的Spring注释?,java,spring,Java,Spring,我在jar中定义了一个类MyFrontService,下面是我想要如何使用它: import blabla.MyFrontService; public class Main { public static void main(String[] args) { MyFrontService.doThis(); } } FrontService是访问其他服务的入口点。 下面是它当前的定义(及其工作原理) MyFrontService.java: public c
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/main/resources/META-INF/springBeans.xml:
(...)
<context:annotation-config />
<context:component-scan base-package="blabla.service" />
(...)
我从这个网站和其他网站上的其他问题中读到了很多。有时有人说这是不可能的,有时会使用@Configuration、@Import等注释,但我无法让它们工作。例如,使用
@ImportResource("classpath:META-INF/springBeans.xml")
调用myService1.doSomething()时触发NullPointerException
如果可能的话,我应该使用什么注释和什么路径?如果你用@Component annotation标记你的类MyFrontService,这应该可以省去你正在做的所有事情的麻烦。如果您不想添加注释,也不想将所有内容都放在spring配置下,那么可以定义一个类,如下所示
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);
使用框架,您将能够像这样编写smth
@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);
}
}
不,没有魔法。必须对注释进行处理。那就是春天。如果你没有告诉Spring通过初始化一个
ApplicationContext
,它怎么知道要这么做呢?任何基于Spring的应用程序的入口点都是一个ApplicationContext
,Spring不会对静态执行@Autowired
任何操作。我认为没有理由定义Springbean而不实例化它们。这就是为什么我认为可能有一种方法可以指定xml文件并立即实例化bean。我不理解这个问题。您可以为ApplicationContext
提供上下文配置,它将为您实例化bean。您只需检索它们。这里调用的是什么?只需在dispatcher servlet中注册该bean,即可完成设置工作。是什么让您认为这是一个servlet应用程序?定义springBeans.xml时,您正在定义dispatcher servlet。在投票否决或否决之前,请仔细阅读内容。我可以使用XML定义和初始化应用程序上下文,而无需在web应用程序中。他们的问题中没有任何内容表明他们正在使用web应用程序。即使有,他们也有比你建议的更好的方法去做他们想做的事情。
@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);
}
}