`
hbxflihua
  • 浏览: 660355 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

通过spring-context创建可执行jar

阅读更多

1、新建一个maven工程;

 

 2、pom.xml中引入spring-context dependency

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>		
	</dependencies>

 

 3、添加maven的compiler 和 jar plugin

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<encoding>${project.build.sourceEncoding}</encoding>
					<source>${java.version}</source>
					<target>${java.version}</target>
					 <compilerArgs>
                      </compilerArgs>
				</configuration>
			</plugin>
			
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<!-- 指定入口函数 -->
							<mainClass>com.huatech.core.Bootstrap</mainClass>
							<!-- 是否添加依赖的jar路径配置 -->
							<addClasspath>true</addClasspath>
							<!-- 依赖的jar包存放位置,和生成的jar放在同一级目录下 -->
							<classpathPrefix>lib</classpathPrefix>
						</manifest>
					</archive>
				</configuration>
			</plugin>
			
		</plugins>
	</build>

 

4、启动类添加main方法

package com.huatech.core;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;


@ComponentScan(basePackages="com.huatech.core")
@EnableScheduling
public class Bootstrap {
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		System.out.println("starting jar-demo ...");
		AnnotationConfigApplicationContext context = 
				new AnnotationConfigApplicationContext(Bootstrap.class);
//		//1. 初始化bean读取器和扫描器
//		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//		//2.注册bean配置类
//		context.register(Bootstrap.class);
//		context.register(ScheduleConfig.class);
//		//3.刷新上下文
//		context.refresh();
		// 优雅停机
		context.registerShutdownHook();
	}

}

 

5、添加一个测试类

package com.huatech.core.service;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TestSchedule {
	
	
	@Scheduled(fixedDelay = 2000)
	public void log(){
		System.out.println("["+new Date() +"]--");
	}
	

}

 

分享到:
评论

相关推荐

    spring4.1核心包

    4.spring-context-4.1.1.RELEASE.jar 在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。这个jar 文件为Spring ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...

    Java打包可执行后的执行脚本cmd,sh,spring

    import org.springframework.context.support.GenericXmlApplicationContext; import com.pactera.service.schedule.ThreadReadPath; import com.pactera.util.CommTool; /*打包用File-&gt;Export-&gt;runnable JAR ...

    java微信公众号MVC开发框架

    通过maven生成一个webapp项目,例如项目名为weixin,在maven配置文件pom.xml中添加jwx依赖,jwx的1.1.1jar包已经提交到maven中心仓库,通过中心仓库搜索jwx关键字可以获取jar包依赖配置。 &lt;groupId&gt;...

    《MyEclipse 6 Java 开发中文教程》前10章

    3.1.5 上下文菜单(Context Menu) 55 3.1.6 状态栏(Status Bar) 55 3.1.7 编辑器(Editor) 55 3.2 常见概念和操作 56 3.2.1 项目(Project) 56 3.2.2工作区(Workspace) 56 3.2.3 导入、导出Java项目 56 3.2.3.1 ...

    Maven权威指南 很精典的学习教程,比ANT更好用

    通过Maven Profiles实现可移植性 11.2.1. 覆盖一个项目对象模型 11.3. 激活Profile 11.3.1. 激活配置 11.3.2. 通过属性缺失激活 11.4. 外部Profile 11.5. Settings Profile 11.5.1. 全局Settings ...

    JAVA上百实例源码以及开源项目源代码

    使用时候只需在控制台窗口执行jar就可以了。 Java 3DMenu 界面源码 5个目标文件 内容索引:Java源码,窗体界面,3DMenu  Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,...

    JAVA上百实例源码以及开源项目

    使用时候只需在控制台窗口执行jar就可以了。 Java 3DMenu 界面源码 5个目标文件 内容索引:Java源码,窗体界面,3DMenu  Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,...

Global site tag (gtag.js) - Google Analytics