在spring应用程序中,我们这样编写以通过手动加载spring应用程序上下文来获取bean.
ApplicationContext context = new ClasspathXmlApplicationContext("path/to/applicationContext.xml");
JobLauncher launcher=(JobLauncher)context.getBean("launcher");
如何在春季靴子做类似的事情?
作为一个新手…需要帮助
最佳答案
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ApplicationContext app = SpringApplication.run(Application .class,args);//init the context
SomeClass myBean = app.getBean(SomeClass.class);//get the bean by type
}
@Bean // this method is the equivalent of the ssprop(sc);
return uc;
}
}
您也可以使用xml文件来声明bean而不是java配置,只需使用@ImportResource({“classpath *:applicationContext.xml”})
编辑:回答评论:将util类设为spring bean(使用@Component注释和组件扫描或与上面显示的SomeClass相同)然后你可以@Autowire你喜欢的bean.然后,当您想要使用Util类时,只需从上下文中获取它.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。