【软通鲲鹏云最佳实践58】spring-4.3.14鲲鹏云服务器安装实践
一、 软件介绍
Spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
二、 支持的操作系统
经过华为云严格实测,以下操作系统在鲲鹏生态中可以完整运行Varnish的全部功能:
CentOS7.6
三、 支持版本和获取方式
建议使用的版本:spring-4.3.14
下载地址:
通过mvn获取:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
</dependencies>
四、 编译与测试环境
本文选用华为鲲鹏云服务器ECS KC1实例做测试,KC1实例的处理器为兼容ARMv8指令集的鲲鹏920。详细规格如下:
类别 | 子项 | 版本 |
云主机配置 | ECS实例类型 | kc1.2xlarge.4 |
ECS配置 | 8U32GB | |
EVS | 高IO(80GB) | |
云OS | Kernel | 4.18.0 |
五、 编译安装与测试
1安装依赖
# yum -y install wget tree
卸载自带的jdk,安装jdk-1.8_151:
# rpm -qa | grep jdk
# rpm -e --nodeps java-1.8.0-openjdkxxxxxxx
# rpm -e --nodeps java-1.8.0-openjdk-headlessxxxxxxxx
# cd /opt/ && wget https://repo.huaweicloud.com/java/jdk/8u151-b12/jdk-8u151-linux-arm64-vfp-hflt.tar.gz
# tar -zxvf jdk-8u151-linux-arm64-vfp-hflt.tar.gz
# vim /etc/profile
export JAVA_HOME=/opt/jdk1.8.0_151
export PATH=$PATH:$JAVA_HOME/bin
# source /etc/profile
2 编译安装
下载maven,并安装:
# wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
# tar -zxvf apache-maven-3.6.0-bin.tar.gz
配置环境变量:
# vim /etc/profile
export MAVEN_HOME=/opt/apache-maven-3.6.0
export PATH=$PATH:$MAVEN_HOME/bin
:wq保存.
# mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /opt/apache-maven-3.6.0
Java version: 1.8.0_212, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.aarch64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "4.14.0-115.5.1.el7a.aarch64", arch: "aarch64", family: "unix"
创建spring java 项目:
# mvn archetype:generate -DgroupId=com.isstech -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart
提示:
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /opt
[INFO] Parameter: package, Value: com.isstech
[INFO] Parameter: groupId, Value: com.isstech
[INFO] Parameter: artifactId, Value: demo
[INFO] Parameter: packageName, Value: com.isstech
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /opt/demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:07 min
[INFO] Finished at: 2019-10-16T07:48:43+08:00
[INFO] ------------------------------------------------------------------------
项目创建成功,项目目录结构如下:
# tree demo
demo
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── isstech
│ └── App.java
└── test
└── java
└── com
└── isstech
└── AppTest.java
修改demo/pom.xml,加载spring:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.isstech</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.isstech.App</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
# vim deom/pom.xml
我不使用junit组件来测试用例,所以删掉AppTest.java
# rm -f src/test/java/com/isstech/AppTest.java
创建demo/src/main/java/com/isstech/Hello.java文件
# vim demo/src/main/java/com/isstech/Hello.java
package com.isstech;
public class Hello {
public void sayHello() {
System.out.println("Hello World!");
}
}
# mkdir -p demo/src/main/resources
# vim demo/src/main/resources/application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id = "hello" class="com.isstech.Hello"/>
</beans>
修改App.java,加载spring容器,调用Hello类中的方法sayHello();
# vim demo/src/main/java/com/isstech/App.java
package com.isstech;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
//1.创建Spring的IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//2.从IOC容器中获取到Bean的示例
Hello hello = (Hello) context.getBean("hello");
//3.进行方法的调用
hello.sayHello();
}
}
3验证测试
构建项目
# cd demo && mvn clean package
成功显示如下:
# java -jar /opt/demo/target/demo-1.0-SNAPSHOT.jar
十月 16, 2019 9:48:38 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27fa135a: startup date [Wed Oct 16 09:48:38 CST 2019]; root of context hierarchy
十月 16, 2019 9:48:38 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application.xml]
Hello World!
4问题总结
无
六、 声明
- 点赞
- 收藏
- 关注作者
评论(0)