jar嵌入静态资源 供引用调用

2020-12-16  本文已影响0人  阳光也学会了妩媚

一、jar包的制作
1、maven工程引入下列三个依赖

 <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.20.RELEASE</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <!-- 使用SLF4J和LogBack作为日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

2、写三个Java文件 和 相应资源包(vue+element)


图片.png

3、写index.html ,可直接引用本地资源也可cdn 引入,正常编写网页
===================
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
<el-button @click="visible = true">Button</el-button>
<el-dialog :visible.sync="visible" title="Hello world">
<p>Try Element</p>
</el-dialog>
</div>
</body>

<script src="https://unpkg.com/vue/dist/vue.js"></script>

<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
el: '#app',
data: function() {
return { visible: false }
}
})
</script>
</html>
===================
4、InitListener 初始化文件,执行相应初始化动作,可不加

package com.juncheng.syc.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class InitListener implements ServletContextListener {

    private static final Logger log = LoggerFactory.getLogger(InitListener.class);

    public void contextInitialized(ServletContextEvent servletContextEvent) {

        log.info("=====================添加依赖开始运行!================================");
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

5、MvcConfig配置文件,自动装配bean

package com.juncheng.syc.config;

import com.juncheng.syc.listener.InitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Bean
    public InitListener thisListener(){
        return new InitListener();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/syc/**")
                .addResourceLocations("classpath:/ui/");
    }
}

6、ThisApi 自定义注解类,并引入MvcConfig.class

package com.juncheng.syc.annotation;

import com.juncheng.syc.config.MvcConfig;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target({ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MvcConfig.class)
public @interface ThisApi {
}

7、生成jar包


图片.png

二、jar包本地引入,并执行
1、新建springboot项目,引入web 和 thymeleaf ,正常执行,并做一个主页进行测试
2、本地引入jar包,在resources下新建lib ,并复制jar包

        <dependency>
            <groupId>springVueElm</groupId>
            <artifactId>springVueElm</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/springVueElm-1.0-SNAPSHOT.jar</systemPath>

        </dependency>

3、在启动类上,填写注解


图片.png

4、正常执行,并访问jar中Configuration 中addResourceHandlers 配置的访问路径,就可以访问jar中的静态页面了

三、本地jar引入后打包注意事项

 <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--把本地jar包也引入进去-->
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>

            </plugin>
            <!--在这里修改版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
            </plugin>
        </plugins>

    </build>

四、获取相关设置(简单通信)
1、InitListener
···
package com.juncheng.syc.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

public class InitListener implements ServletContextListener {

private static final Logger log = LoggerFactory.getLogger(InitListener.class);

@Autowired
private Environment env;

public void contextInitialized(ServletContextEvent servletContextEvent) {
    //获取属相相关设置
    String  status = env.getProperty("thisApi.status");
    log.info("=====================添加依赖开始运行!================================");
    log.info("=====================thisApi.status:["+status+"]==============================");
}

public void contextDestroyed(ServletContextEvent servletContextEvent) {

}

}

···
2、设置 application.properties

图片.png
上一篇 下一篇

猜你喜欢

热点阅读