java-怎样在main方法中调用action
以下内容转自http://blog.csdn.net/zzq900503/article/details/40590281
Java中spring 框架 中的action操作(extends ActionSupport) 一般配置在struts中 通过 浏览器 通过链接形式访问
例如:我的action和配置文件如下:
RapToDpsProjectRaw.java
- package com.action.rap;
- public class RapToDpsProjectRaw extends ActionSupport {
- /**
- *
- */
- private static final long serialVersionUID = 4759438181190004544L;
- private DpsProjectRapReposity dpsProjectRapReposity;
- public void setDpsProjectRapReposity(
- DpsProjectRapReposity dpsProjectRapReposity) {
- this.dpsProjectRapReposity = dpsProjectRapReposity;
- }
- public String execute() {
- System.out.println("已执行");
- return SUCCESS;
- }
rap.xml spring文件
- <?xml version="1.0" encoding="GBK"?>
- <!-- 指定Spring配置文件的Schema信息 -->
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 查询LINKURL -->
- <bean id="rapToDpsProjectRaw" class="com.action.rap.RapToDpsProjectRaw">
- <property name="dpsProjectRapReposity" ref="dpsProjectRapReposity"></property>
- </bean>
- </beans>
rap.xml struts文件
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <struts>
- <package name="rapToDpsProjectRaw" extends="imoen-default" namespace="/rap">
- <action name="rapToDpsProjectRaw" class="rapToDpsProjectRaw">
- <result type="json"></result>
- </action>
- </package>
- </struts>
我要执行这个action 就会在浏览器中输入 http://localhost:8080/项目名/rap/rapToDpsProjectRaw
如果要在 main方式中 运行 主要是获取到 rapToDpsProjectRaw这个Bean 代码如下:
- public static void main(final String[] args) {
- LOG.info("starting to copy record from rap system to mongo db...");
- final String[] paths = new String[] {
- "resource/spring/action/rap/rap.xml",
- "resource/struts/rap.xml", "resource/spring/common-beans.xml",
- "resource/spring/reposity/common-reposity.xml" };
- @SuppressWarnings("resource")
- final ApplicationContext context = new FileSystemXmlApplicationContext(
- paths); // ClassPathXmlApplicationContext
- final RapToDpsProjectRaw processor = (RapToDpsProjectRaw) context
- .getBean("rapToDpsProjectRaw");
- final String result = processor.execute();
- if (ActionSupport.SUCCESS.equals(result)) {
- LOG.info("Good job, copy operation succedded!");
- } else {
- LOG.warn("Oops! There's something wrong under the hook!");
- }
- LOG.info("finished copying record from rap system to mongo db.");
- }
=============================华丽的分割线====================================
以下内容为自己的感悟:
我认为还是从web的原理入手,将context给加载起来了,可以使用该种方法进行web工程中的测试。
浙公网安备 33010602011771号