fat*_*man 34 java spring command-line
我想创建spring控制台应用程序(从命令行运行maven,例如:mvn exec:java -Dexec.mainClass ="package.MainClass").
这个应用程序我想要一些服务和dao层.我知道如何为Web应用程序执行此操作,但我还没有找到有关如何在控制台应用程序中执行操作的任何信息(可能使用Swing).
我正在尝试创建类似的东西:
public interface SampleService {
public String getHelloWorld();
}
@Service
public class SampleServiceImpl implements SampleService {
public String getHelloWorld() {
return "HelloWorld from Service!";
}
}
public class Main {
@Autowired
SampleService sampleService;
public static void main(String [] args) {
Main main = new Main();
main.sampleService.getHelloWorld();
}
}
Run Code Online (Sandbox Code Playgroud)
可能吗?我能找到一个如何做的例子吗?
axt*_*avt 34
看看Spring Reference,3.2.2实例化容器.
为了在控制台应用程序中使用Spring,您需要创建一个实例ApplicationContext并从中获取Spring管理的bean.
参考中描述了使用XML配置创建上下文.对于完全基于注释的方法,您可以这样做:
@Component // Main is a Spring-managed bean too, since it have @Autowired property
public class Main {
@Autowired SampleService sampleService;
public static void main(String [] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext("package"); // Use annotated beans from the specified package
Main main = ctx.getBean(Main.class);
main.sampleService.getHelloWorld();
}
}
Run Code Online (Sandbox Code Playgroud)
Chi*_*ang 20
Spring Reference建议在方法中使用ClassPathXmlApplicationContextmain来创建应用程序上下文,然后调用该getBean方法从应用程序上下文获取对bean的初始引用.在编写了相同的代码几次之后,您最终会将样板重构为此实用程序类:
/**
* Bootstraps Spring-managed beans into an application. How to use:
* <ul>
* <li>Create application context XML configuration files and put them where
* they can be loaded as class path resources. The configuration must include
* the {@code <context:annotation-config/>} element to enable annotation-based
* configuration, or the {@code <context:component-scan base-package="..."/>}
* element to also detect bean definitions from annotated classes.
* <li>Create a "main" class that will receive references to Spring-managed
* beans. Add the {@code @Autowired} annotation to any properties you want to be
* injected with beans from the application context.
* <li>In your application {@code main} method, create an
* {@link ApplicationContextLoader} instance, and call the {@link #load} method
* with the "main" object and the configuration file locations as parameters.
* </ul>
*/
public class ApplicationContextLoader {
protected ConfigurableApplicationContext applicationContext;
public ConfigurableApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* Loads application context. Override this method to change how the
* application context is loaded.
*
* @param configLocations
* configuration file locations
*/
protected void loadApplicationContext(String... configLocations) {
applicationContext = new ClassPathXmlApplicationContext(
configLocations);
applicationContext.registerShutdownHook();
}
/**
* Injects dependencies into the object. Override this method if you need
* full control over how dependencies are injected.
*
* @param main
* object to inject dependencies into
*/
protected void injectDependencies(Object main) {
getApplicationContext().getBeanFactory().autowireBeanProperties(
main, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
}
/**
* Loads application context, then injects dependencies into the object.
*
* @param main
* object to inject dependencies into
* @param configLocations
* configuration file locations
*/
public void load(Object main, String... configLocations) {
loadApplicationContext(configLocations);
injectDependencies(main);
}
}
Run Code Online (Sandbox Code Playgroud)
load在应用程序main方法中调用该方法.请注意,Main该类不是Spring创建的bean,但您可以使用应用程序上下文中的bean注入其中一个属性.
public class Main {
@Autowired
private SampleService sampleService;
public static void main(String[] args) {
Main main = new Main();
new ApplicationContextLoader().load(main, "applicationContext.xml");
main.sampleService.getHelloWorld();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40481 次 |
| 最近记录: |