子包功能介绍

一.boot

平台的启动停止及从新启动 com.jspx.boot.ApplicationBoot 负责平台全局的环境变量及配置调度
  JspxNetApplication.restart();

二.EnvFactory

环境工厂接口 com.jspx.boot.EnvFactory
  //Bean 工厂
  getBeanFactory();

 //环境模板接口
  getPlaceholder();

  //得到环境配置
  getEnvironmentTemplate();

三.Cache

缓存接口 com.jspx.cache.Cache 平台缓存接口,相当于CPU的一级缓存,用户能够通过配置定义缓存的大小及时间。 整个平台为了考虑数据的及时性只内置了一级缓存。 默认提供了两个接口支持: 标准的本地内存缓存 com.jspx.cache.store.MemoryStore 标准的LRU本地内存缓存,默认限制长度250 com.jspx.cache.store.LRUStore 用户能够通过接口扩展分布式缓存等机制,但不内置。 Cache 配置在Sioc中配置方法如下,每个换成可控可以是一个类逻辑隔离的.这样管理内存清晰明了. Redis已经内置支持,也可以可以自己整合配置,第三方缓存。
<?xml version="1.0" encoding="UTF-8"?>
<sioc name="cache" extends="global" namespace="cache">
    <!-- memoryStore lruStore 都是内存缓存 -->
    <bean id="memoryStore" class="com.jspx.cache.store.MemoryStore" singleton="false"/>
    <bean id="lruStore" class="com.jspx.cache.store.LRUStore" singleton="false"/>

    <bean class="com.github.jspxnet.cache.redis.RedissonClientConfig" create="getRedissonClient" singleton="true" caption="Redis配置">
        <string name="config">${redissonConfig}</string>
    </bean>
    <bean id="redisStore" class="com.github.jspxnet.cache.store.SingleRedissonStore" singleton="false" caption="这里不能是用单例">
    </bean>

     <bean id="com.github.jspxnet.cache.DefaultCache" class="com.github.jspxnet.cache.core.JSCache" singleton="true" caption="默认缓存">
        <string name="name">com.github.jspxnet.cache.DefaultCache</string>
        <int name="second">1800</int>
        <int name="maxElements">2000</int>
        <ref name="store">redisStore</ref>
    </bean>
    <!--system 配置 end-->

    <!--JSCache 配置 begin-->
    <bean id="com.github.jspxnet.txweb.table.UserSession" caption="在线用户缓存" class="com.github.jspxnet.cache.core.JSCache" singleton="true" >
        <ref name="store">redisStore</ref>
        <string name="name">com.github.jspxnet.txweb.table.UserSession</string>
        <int name="second" caption="超时">86400</int>
    </bean>
    <bean id="com.github.jspxnet.txweb.table.ActionLog" caption="日志缓存" class="com.github.jspxnet.cache.core.JSCache" singleton="true" >
        <ref name="store">redisStore</ref>
        <string name="name">com.github.jspxnet.txweb.table.ActionLog</string>
        <int name="second" caption="超时">10</int>
    </bean>
</sioc>
中bean 对象中如下定义后Sober对数据库查询时就会自动调用缓存

四.spring整合是用

可以整合是用本架构的sober orm ,替代mybatis这些组件 SoberFactory持久层管理对象
@Slf4j
@Configuration
@Import({SpringDataSource.class,DataSourceProperties.class})
public class SpringSoberMappingBean implements Serializable {
    final static private SoberMappingBean SOBER_MAPPING_BEAN = new SoberMappingBean();
    @Autowired
    private DataSource dataSource;

    @Autowired
    private DataSourceProperties dataSourceProperties;


    @Bean(name = "soberFactory")
    @Primary
    public SoberFactory soberFactory() throws Exception {
        JspxNetApplication.onlySql("application.properties",new Class[]{DefaultCache.class});
        SOBER_MAPPING_BEAN.setDataSource(dataSource);
        SOBER_MAPPING_BEAN.setShowsql(dataSourceProperties.isShowSql());
        String sqlmap = dataSourceProperties.getSqlmap();
        if (StringUtil.isNull(sqlmap))
        {
            sqlmap = "*.sqlmap.xml";
        }
        String[] fileMap = StringUtil.split(sqlmap,StringUtil.SEMICOLON);
        SOBER_MAPPING_BEAN.setMappingResources(fileMap);
        return SOBER_MAPPING_BEAN;
    }
}
事务整合
import com.github.jspxnet.sober.SoberFactory;
import com.github.jspxnet.sober.transaction.SpringPlatformTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration(
        proxyBeanMethods = false
)
//如果这为true会去是用 com/atomikos/jdbc/AtomikosDataSourceBean
@ConditionalOnClass({SpringSoberMappingBean.class, SpringDataSource.class,PlatformTransactionManager.class})
@EnableConfigurationProperties({DataSourceProperties.class})
public class SrpingDataSourceTransactionManager {
    @Autowired
    private SoberFactory soberFactory;


    @Bean
    @ConditionalOnMissingBean({PlatformTransactionManager.class})
    public SpringPlatformTransactionManager transactionManager() {
        return new SpringPlatformTransactionManager(soberFactory);
    }
}
json 解析器整合
@Slf4j
@Import({ServiceInterceptor.class})
@Configuration
public class WebConfiguration implements WebMvcConfigurer
{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        JsonHttpMessageConverter jsonConverter = new JsonHttpMessageConverter();
        converters.add(jsonConverter);
    }
}

五.加密算法例子

   //国密
    public static void testSM4A() throws Exception {
        String text = "目前,SiSoftware数据库中已经出现了Coffee Lake的身影,赫然就是6核心!\n" +
                "\n" +
                "    不过线程数也是6个,按照Intel这几年的产品线划分惯例,那应该就是隶属于Core i5系列,更高的Core i7就得是6核心12线程了,可以更好地对抗AMD 6/8核心的Ryzen 5/7系列。\n" +
                "\n" +
                "    当然,也不排除6核心6线程、6核心12线程都是Core i7系列的一部分,Core i5则变为4核心8线程。\n" +
                "\n" +
                "    检测信息还显示,该处理器主频为3.5GHz,二级缓存1.5MB(每核心256Kaby Lake)、三级缓存9MB。\n" +
                "\n" +
                "    这就比较奇怪,Kaby Lake架构是每个核心2MB三级缓存,6核心应该是12MB才对,这里要么是阉割了一部分(每核心1.5MB),要么就是检测不准确。";

        SM4Encrypt encrypt = new SM4Encrypt();
        encrypt.setSecretKey("1234567890123456");
        encrypt.setCipherAlgorithm("CBC");
        encrypt.setCipherIv("1234567890123456");

        byte[] sm = encrypt.getEncode(text.getBytes(Environment.defaultEncode));
        byte[] out = encrypt.getDecode(sm);
        Assert.assertEquals(text, new String(out, Environment.defaultEncode));
    }

    //非标准到文本加密
   @Test
    public static void testTextKeyEncryptTwo() throws Exception {

        Encrypt en = new TextKeyEncrypt();
        en.setSecretKey("家密我的文件");
        String txt = en.getEncode("家密我的文件");
        String out = en.getDecode(txt);
        Assert.assertEquals("家密我的文件", out);
    }


     //DES加密,3des 类名称为 DESedeEncrypt,就可以了,AES 类为 AESEncrypt,改个类名就可以切换到不同到加密方式
     @Test
    public void testDESEncrypt() throws Exception {
        String text = "1236745^$%^&$^%$(*&()&*_()KHJN<M:LIUPOULKGT中文看看,五一小长假期间,国内的各大景点都出现了游客激增的情况,不少景区因为游客实在太多,为了游客安全,不得不采取限流的措施。 ";
        DESEncrypt encrypt = new DESEncrypt();
        encrypt.setSecretKey(StringUtil.cut(EncryptUtil.getMd5(text), 8, ""));
        encrypt.setCipherIv("01234567");
        String mm = encrypt.getEncode(text);
        String out = encrypt.getDecode(mm);
        Assert.assertEquals(out, text);
    }


    //密钥生存
     @Test
    public static void testHexSave() throws Exception {

        RSAEncrypt encrypt = new RSAEncrypt();
        KeyPairGen keyPair = encrypt.getKeyPair();

        String hexPri = EncryptUtil.byteToHex(keyPair.getPrivateKey());
        String hexPub = EncryptUtil.byteToHex(keyPair.getPublicKey());

        System.out.println("私钥: " + hexPri);
        System.out.println("公钥: " + hexPub);
        System.out.println("---------------------------------------");

        byte[] bytePri = EncryptUtil.hexToByte(hexPri);
        byte[] bytePub = EncryptUtil.hexToByte(hexPub);

        Assert.assertEquals(keyPair.getPrivateKey(),bytePri);
        Assert.assertEquals(keyPair.getPublicKey(),bytePub);

        String basePri = EncryptUtil.getBase64Encode(bytePri,EncryptUtil.DEFAULT);
        String basePut = EncryptUtil.getBase64Encode(bytePub,EncryptUtil.DEFAULT);

        System.out.println("私钥: " + basePri);
        System.out.println("公钥: " + basePut);

        byte[] bytePri1 = EncryptUtil.getBase64Decode(basePri);
        byte[] bytePput1 = EncryptUtil.getBase64Decode(basePut);

        Assert.assertEquals(bytePri1,bytePri);
        Assert.assertEquals(bytePput1,bytePub);


        File outPri = new File("d:/tmp/privateKey.key");
        File outPub = new File("d:/tmp/publicKey.key");
        FileUtil.writeFile(outPri,keyPair.getPrivateKey());
        FileUtil.writeFile(outPub,keyPair.getPublicKey());

}