`

用命令行启动spring

 
阅读更多

A first example
Let us assume that you are an application developer who has been tasked with diagnosing the cause of
some performance problems in a system. Rather than break out a profiling tool, what we are going to do
Spring Framework
3.1 Reference Documentation 238
is switch on a simple profiling aspect that will enable us to very quickly get some performance metrics, so
that we can then apply a finer-grained profiling tool to that specific area immediately afterwards.
Here is the profiling aspect. Nothing too fancy, just a quick-and-dirty time-based profiler, using the
@AspectJ-style of aspect declaration.
package foo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
import org.springframework.core.annotation.Order;
@Aspect
public class ProfilingAspect {
@Around("methodsToBeProfiled()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
StopWatch sw = new StopWatch(getClass().getSimpleName());
try {
sw.start(pjp.getSignature().getName());
return pjp.proceed();
} finally {
sw.stop();
System.out.println(sw.prettyPrint());
}
}
@Pointcut("execution(public * foo..*.*(..))")
public void methodsToBeProfiled(){}
}
We will also need to create an 'META-INF/aop.xml' file, to inform the AspectJ weaver that we want to
weave our ProfilingAspect into our classes. This file convention, namely the presence of a file (or
files) on the Java classpath called ' META-INF/aop.xml' is standard AspectJ.


<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="foo.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="foo.ProfilingAspect"/>
</aspects>
</aspectj>


Now to the Spring-specific portion of the configuration. We need to configure a LoadTimeWeaver (all
explained later, just take it on trust for now). This load-time weaver is the essential component
Spring Framework
3.1 Reference Documentation 239
responsible for weaving the aspect configuration in one or more 'META-INF/aop.xml' files into the
classes in your application. The good thing is that it does not require a lot of configuration, as can be seen
below (there are some more options that you can specify, but these are detailed later).
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- a service object; we will be profiling its methods -->
<bean id="entitlementCalculationService"
class="foo.StubEntitlementCalculationService"/>
<!-- this switches on the load-time weaving -->
<context:load-time-weaver/>
</beans>
Now that all the required artifacts are in place - the aspect, the 'META-INF/aop.xml' file, and the
Spring configuration -, let us create a simple driver class with a main(..) method to demonstrate the
LTW in action.
package foo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml", Main.class);
EntitlementCalculationService entitlementCalculationService
= (EntitlementCalculationService) ctx.getBean("entitlementCalculationService");
// the profiling aspect is 'woven' around this method execution
entitlementCalculationService.calculateEntitlement();
}
}
There is one last thing to do. The introduction to this section did say that one could switch on LTW
selectively on a per-ClassLoader basis with Spring, and this is true. However, just for this example,
we are going to use a Java agent (supplied with Spring) to switch on the LTW. This is the command line
we will use to run the above Main class:


java -javaagent:C:/projects/foo/lib/global/spring-instrument.jar foo.Main


The '-javaagent' is a Java 5+ flag for specifying and enabling agents to instrument programs running
on the JVM. The Spring Framework ships with such an agent, the InstrumentationSavingAgent,
which is packaged in the spring-instrument.jar that was supplied as the value of the
-javaagent argument in the above example.

分享到:
评论

相关推荐

    spring boot 命令行启动的方式

    Spring Boot 命令行启动方式详解 在 Spring Boot 应用程序中,命令行启动是非常常见的场景。在实际开发中,我们经常需要在应用程序启动时,执行一些特定的操作或初始化某些配置。为此,Spring Boot 提供了两种方式...

    一个基于命令行使用Spring Initializr生成Spring Boot项目的步骤,并附带一个简单的Maven构建和运行脚

    之后通过`java -jar`命令运行这个JAR文件即可启动Spring Boot应用。 #### 四、脚本示例 为了进一步简化项目的构建和运行流程,还可以编写一个简单的bash脚本来自动化这些步骤。下面是一个示例脚本: ```bash #!/...

    springboot命令行启动的方法详解

    在开发中,通过命令行启动Spring Boot应用程序是一种常见的做法,它可以在不依赖IDE工具的情况下快速启动项目。本文将详细介绍两种常见的Spring Boot命令行启动方法,以及与之相关的Maven命令和环境变量配置等内容。...

    SpringBoot java-jar命令行启动原理解析

    这使得 Spring Boot 应用程序可以直接从命令行启动,而不需要另外配置一个 Web Server。 Spring Boot java-jar 命令行启动原理解析可以总结为以下几点: 1. Spring Boot 应用程序可以直接从命令行启动,而不需要...

    springboot

    - **命令行启动器**:描述如何使用命令行启动Spring Boot应用。 - **Application退出**:介绍如何优雅地关闭Spring Boot应用。 - **外化配置**: - **配置随机值**:介绍如何在配置中使用随机值。 - **访问...

    spring boot如何指定启动端口

    可以在命令行中使用以下命令来指定启动端口: `java -jar test.jar --server.port=8081` 这样,在启动 Spring Boot 应用程序时,端口号将被设置为 8081。 方式三-2:虚拟机系统属性 也可以使用虚拟机系统属性来...

    springloaded-1.2.6实现spring热部署,最全的教程

    - **命令行启动**:对于通过命令行启动的应用,需要在启动参数中加入`-javaagent:path/to/springloaded-x.x.x.jar`。 3. **支持的环境** SpringLoaded不仅支持Spring框架,还适用于其他基于Java的应用,如Tomcat...

    spring-boot中文参考指南

    - **命令行启动器**:通过命令行启动Spring Boot应用。 - **Application退出**:处理应用退出时的逻辑。 - **外化配置**: - **配置随机值**:为安全配置生成随机值。 - **访问命令行属性**:读取命令行参数作为...

    Spring和SpringCloud视频

    2. **SpringBoot项目结构**:理解主配置类、启动器、 starter 包的使用。 3. **Maven或Gradle集成**:如何配置构建工具来管理依赖。 4. **Spring Boot Actuator**:监控和度量应用性能的工具,包括端点的使用和安全...

    spring-boot-tutorials-master.zip_dollarkle_quartermya_spring_spr

    4. **运行器(Spring Boot Runner)**:提供`spring-boot:run`命令,使得我们可以直接通过命令行启动Spring Boot应用。 5. **Actuator**:提供健康检查、指标收集、审计跟踪等功能,帮助监控和管理应用。 6. **无...

    LEARNING SPRING BOOT 3.0 - THIRD EDITION

    读者将学习如何使用Spring Boot的命令行接口(CLI)快速启动项目,以及如何利用IDE如IntelliJ IDEA或Eclipse进行开发。 书中还会深入讨论Spring Boot的核心特性,比如自动配置、健康检查、内嵌的Tomcat或Jetty...

    82. Spring Boot – 启动彩蛋【从零开始学Spring Boot】

    这样,每次启动Spring Boot应用时,控制台就会显示设定的文本。 另外,开发者可以创建一个自定义的`Banner`类,扩展`org.springframework.boot.Banner`接口并实现`printBanner()`方法。这个方法允许你在启动时打印...

    spring boot cli 1.5.10

    1. **快速启动Spring应用**:CLI允许开发者直接从命令行启动一个完整的Spring应用,无需构建整个项目结构。只需几行代码,就可以启动一个包含所有依赖的Spring Boot应用。 2. **Groovy支持**:Spring Boot CLI特别...

    spring深入26个例子

    例子可能包括Spring Boot的起步依赖、自动配置和命令行启动器的使用。 7. **Spring Cloud**:对于微服务架构,Spring Cloud提供了服务发现、配置中心、负载均衡、熔断机制等功能。例子可能会展示Eureka服务注册与...

    十分钟上手spring boot

    - 编写main方法来启动Spring Boot应用。 - **11.4. 运行示例** - 通过IDE或者命令行来运行Spring Boot应用。 - **11.5. 创建一个可执行jar** - 将Spring Boot应用打包成一个可执行的jar文件。 **12. 接下来阅读...

    springcloud部署redis集群

    在命令行中,使用`redis-trib.rb`工具(在Redis源码包的`src`目录下)初始化集群。执行以下命令,其中`ip:port`是你准备的每个节点的IP和端口号: ```bash redis-trib.rb create --replicas 1 node1_ip:port node2_...

    Linux部署springboot(springcloud)启动多个jar

    我们可以使用`java -jar`命令来启动这些服务: ```bash java -jar /opt/apps/myapp/service-a.jar --server.port=8080 java -jar /opt/apps/myapp/service-b.jar --server.port=8081 ``` 然而,这种方式手动操作...

    spring 常用的26个包

    7. `org.springframework.context.support`:提供了各种上下文支持类,如命令行启动器、XML配置的ApplicationContext等。 8. `org.springframework.jdbc`:用于处理数据库操作的包,包含JdbcTemplate和...

    spring boot+spring cloud+spring mvc+mybatis

    Spring Boot的核心特性包括自动配置、起步依赖和命令行接口。自动配置使得我们只需要引入相应的起步依赖,如Spring Data JPA或MyBatis,Spring Boot就能自动配置相关的数据访问层。同时,Spring Boot还提供了一个...

Global site tag (gtag.js) - Google Analytics