parent
3cf54ef4c2
commit
04540bb7b4
@ -0,0 +1,69 @@
|
||||
package test.thread;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-11 22:40
|
||||
*/
|
||||
public class TestThreadLocal {
|
||||
static ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2,
|
||||
0L, TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<Runnable>());
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ThreadLocal<Map<Object, Object>> threadLocal = new InheritableThreadLocalMap<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Map<Object, Object> map = new HashMap<>();
|
||||
map.put("key", i);
|
||||
threadLocal.set(map);
|
||||
executor.execute(()->{
|
||||
System.out.println(threadLocal.get().get("key"));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void testInThreadLocal(){
|
||||
ThreadLocal<Map<Object, Object>> threadLocal = new InheritableThreadLocalMap<>();
|
||||
Map<Object, Object> map = new HashMap<>();
|
||||
map.put("key", "value");
|
||||
threadLocal.set(map);
|
||||
new Thread(() -> {
|
||||
System.out.println(threadLocal.get().get("key"));
|
||||
}, "input thread name").start();
|
||||
}
|
||||
public static void testThreadLocal(){
|
||||
ThreadLocal<String> threadLocal = new ThreadLocal<>();
|
||||
threadLocal.set("测试threadLocal");
|
||||
new Thread(() -> {
|
||||
System.out.println(threadLocal.get());
|
||||
}, "input thread name").start();
|
||||
|
||||
}
|
||||
|
||||
private static final class InheritableThreadLocalMap<T extends Map<Object, Object>> extends InheritableThreadLocal<Map<Object, Object>> {
|
||||
|
||||
/**
|
||||
* This implementation was added to address a
|
||||
* <a href="http://jsecurity.markmail.org/search/?q=#query:+page:1+mid:xqi2yxurwmrpqrvj+state:results">
|
||||
* user-reported issue</a>.
|
||||
* @param parentValue the parent value, a HashMap as defined in the {@link #initialValue()} method.
|
||||
* @return the HashMap to be used by any parent-spawned child threads (a clone of the parent HashMap).
|
||||
*/
|
||||
@SuppressWarnings({"unchecked"})
|
||||
protected Map<Object, Object> childValue(Map<Object, Object> parentValue) {
|
||||
if (parentValue != null) {
|
||||
return (Map<Object, Object>) ((HashMap<Object, Object>) parentValue).clone();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.bjmashibing.shiro.moduler.controller;
|
||||
|
||||
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户信息表 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author 孙志强
|
||||
* @since 2020-04-13
|
||||
*/
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.bjmashibing.shiro.moduler.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.bjmashibing.shiro.moduler.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户信息表 Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author 孙志强
|
||||
* @since 2020-04-13
|
||||
*/
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.bjmashibing.shiro.moduler.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bjmashibing.shiro.moduler.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户信息表 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author 孙志强
|
||||
* @since 2020-04-13
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
void aspectj1();
|
||||
void aspectj2();
|
||||
|
||||
|
||||
|
||||
void parent();
|
||||
|
||||
void child();
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.bjmashibing.shiro.moduler.service.impl;
|
||||
|
||||
import com.bjmashibing.shiro.moduler.entity.User;
|
||||
import com.bjmashibing.shiro.moduler.mapper.UserMapper;
|
||||
import com.bjmashibing.shiro.moduler.service.UserService;
|
||||
import com.bjmashibing.shiro.proxy.SysLog;
|
||||
import org.springframework.aop.config.AopConfigUtils;
|
||||
import org.springframework.aop.framework.AopContext;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户信息表 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author 孙志强
|
||||
* @since 2020-04-13
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
@SysLog
|
||||
@Override
|
||||
public void aspectj1() {
|
||||
System.out.println("aspectj1 执行。。。");
|
||||
aspectj2();
|
||||
//如何解决单独调用,注解不生效的情况
|
||||
// ((UserService) AopContext.currentProxy()).aspectj2();
|
||||
}
|
||||
@SysLog
|
||||
@Override
|
||||
public void aspectj2() {
|
||||
System.out.println("aspectj2 执行。。。");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void parent() {
|
||||
User parent = new User();
|
||||
parent.setUsername("父亲");
|
||||
userMapper.insert(parent);
|
||||
// 工作中有这样的需求
|
||||
//事务注解和异步注解都不生效
|
||||
((UserService) AopContext.currentProxy()).child();
|
||||
// testPrivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Transactional() //根本就没生效
|
||||
public void child() {
|
||||
User child = new User();
|
||||
child.setUsername("儿子");
|
||||
userMapper.insert(child);
|
||||
System.out.println(1 / 0);
|
||||
}
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
private void testPrivate(){
|
||||
User child = new User();
|
||||
child.setUsername("private");
|
||||
userMapper.insert(child);
|
||||
System.out.println(1 / 0);
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package com.bjmashibing.shiro.service;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-06 17:40
|
||||
*/
|
||||
public interface UserService {
|
||||
int add();
|
||||
int select();
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.bjmashibing.shiro.service;
|
||||
|
||||
import com.bjmashibing.shiro.proxy.SysLog;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-06 17:41
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService{
|
||||
@SysLog
|
||||
@Override
|
||||
public int add() {
|
||||
System.out.println("添加用户");
|
||||
select();
|
||||
return 0;
|
||||
}
|
||||
@SysLog
|
||||
@Override
|
||||
public int select() {
|
||||
System.out.println("查询用户");
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bjmashibing.shiro.moduler.mapper.UserMapper">
|
||||
|
||||
</mapper>
|
@ -1,27 +0,0 @@
|
||||
package com.bjmashibing.shiro;
|
||||
|
||||
import com.bjmashibing.shiro.service.UserService;
|
||||
import com.bjmashibing.shiro.service.UserServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-06 17:43
|
||||
*/
|
||||
@Slf4j
|
||||
public class UserServiceTest extends ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Test
|
||||
public void add(){
|
||||
//userservice 真正运行的时候,是不是代理对象?
|
||||
userService.add();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.bjmashibing.shiro.mybatis;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.generator.AutoGenerator;
|
||||
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.PackageConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
|
||||
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
||||
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 〈一句话功能简述〉<br>
|
||||
* 代码生成器测试
|
||||
*
|
||||
* @author:孙志强
|
||||
* @create:2018-06-11 11:07
|
||||
* @Modified BY:
|
||||
**/
|
||||
|
||||
public class CodeGenerator {
|
||||
@Test
|
||||
public void generateCode() {
|
||||
String packageName = "com.bjmashibing.shiro.moduler.system";
|
||||
generateByTables( packageName, "sys_module","sys_role","sys_role_module_ref","sys_user","sys_user_role_ref");
|
||||
}
|
||||
|
||||
private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
|
||||
GlobalConfig config = new GlobalConfig();
|
||||
String dbUrl = "jdbc:mysql://127.0.0.1:3306/db_shiro";
|
||||
DataSourceConfig dataSourceConfig = new DataSourceConfig();
|
||||
dataSourceConfig.setDbType(DbType.MYSQL)
|
||||
.setUrl(dbUrl)
|
||||
.setUsername("root")
|
||||
.setPassword("admin")
|
||||
.setDriverName("com.mysql.jdbc.Driver")
|
||||
.setTypeConvert(new MySqlTypeConvert() {
|
||||
@Override
|
||||
public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
|
||||
System.out.println("转换类型:" + fieldType);
|
||||
//将数据库中datetime转换成date
|
||||
if ( fieldType.toLowerCase().contains( "datetime" ) ) {
|
||||
return DbColumnType.DATE;
|
||||
}
|
||||
if ( fieldType.toLowerCase().contains( "timestamp" ) ) {
|
||||
return DbColumnType.DATE;
|
||||
}
|
||||
return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
|
||||
}
|
||||
});
|
||||
StrategyConfig strategyConfig = new StrategyConfig();
|
||||
strategyConfig
|
||||
.setCapitalMode(true)
|
||||
.setEntityLombokModel(true)
|
||||
.setNaming(NamingStrategy.underline_to_camel)
|
||||
.setInclude(tableNames).setTablePrefix("base_","test_","sys_","flow_","tbl_","jd_");//修改替换成你需要的表名,多个表名传数组
|
||||
config.setActiveRecord(false)
|
||||
.setAuthor("孙志强")
|
||||
.setEnableCache(false)
|
||||
.setOutputDir("D:\\export")
|
||||
.setFileOverride(true);
|
||||
//user -> UserService, 设置成true: user -> IUserService
|
||||
if (!serviceNameStartWithI) {
|
||||
config.setServiceName("%sService");
|
||||
}
|
||||
new AutoGenerator()
|
||||
.setTemplateEngine(new FreemarkerTemplateEngine())
|
||||
.setGlobalConfig(config)
|
||||
.setDataSource(dataSourceConfig)
|
||||
.setStrategy(strategyConfig)
|
||||
.setPackageInfo(
|
||||
new PackageConfig()
|
||||
.setParent(packageName)
|
||||
.setController("controller")
|
||||
.setEntity("entity")
|
||||
).execute();
|
||||
}
|
||||
|
||||
private void generateByTables(String packageName, String... tableNames) {
|
||||
generateByTables(false, packageName, tableNames);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.bjmashibing.shiro.patterns.builder;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-13 17:28
|
||||
*/
|
||||
public class SubjectBuilder {
|
||||
private int id = 1;
|
||||
private String firstname = "first";
|
||||
private String lastname = "last";
|
||||
private Date birthdate = new Date();
|
||||
private String street = "street";
|
||||
|
||||
public SubjectBuilder(int id, String firstname, String lastname, Date birthdate, String street) {
|
||||
this.id = id;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.birthdate = birthdate;
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public SubjectBuilder Build() {
|
||||
return new SubjectBuilder(id, firstname, lastname, birthdate, street);
|
||||
}
|
||||
|
||||
public SubjectBuilder WithFirstName(String firstname) {
|
||||
this.firstname = firstname;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SubjectBuilder WithLastName(String lastname) {
|
||||
this.lastname = lastname;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SubjectBuilder WithBirthDate(Date birthdate) {
|
||||
this.birthdate = birthdate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SubjectBuilder WithStreet(String street) {
|
||||
this.street = street;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.bjmashibing.shiro.patterns.builder;
|
||||
|
||||
import org.apache.shiro.mgt.DefaultSecurityManager;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-13 17:36
|
||||
*/
|
||||
public class TestSubjectBuilder {
|
||||
public static void main(String[] args) {
|
||||
SecurityManager securityManager = new DefaultSecurityManager();
|
||||
Subject subject1 = new Subject.Builder(securityManager).authenticated(false).buildSubject();
|
||||
Subject subject2 = new Subject.Builder(securityManager).authenticated(true).buildSubject();
|
||||
System.out.println(subject1.isAuthenticated());
|
||||
System.out.println(subject2.isAuthenticated());
|
||||
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.bjmashibing.shiro.proxy;
|
||||
package com.bjmashibing.shiro.patterns.proxy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
@ -1,4 +1,4 @@
|
||||
package com.bjmashibing.shiro.proxy;
|
||||
package com.bjmashibing.shiro.patterns.proxy;
|
||||
|
||||
import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.bjmashibing.shiro.proxy;
|
||||
package com.bjmashibing.shiro.patterns.proxy;
|
||||
|
||||
/**
|
||||
* <p></p>
|
@ -1,4 +1,4 @@
|
||||
package com.bjmashibing.shiro.proxy;
|
||||
package com.bjmashibing.shiro.patterns.proxy;
|
||||
|
||||
/**
|
||||
* <p></p>
|
@ -1,4 +1,4 @@
|
||||
package com.bjmashibing.shiro.proxy;
|
||||
package com.bjmashibing.shiro.patterns.proxy;
|
||||
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
import org.springframework.cglib.proxy.MethodInterceptor;
|
@ -0,0 +1,29 @@
|
||||
package com.bjmashibing.shiro.patterns.proxy.springaop;
|
||||
|
||||
import com.bjmashibing.shiro.ApplicationTests;
|
||||
import com.bjmashibing.shiro.moduler.service.UserService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-09 20:41
|
||||
*/
|
||||
public class AnnotationTest extends ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Test
|
||||
public void testAspectOnly(){
|
||||
userService.aspectj2();
|
||||
userService.aspectj2();
|
||||
}
|
||||
@Test
|
||||
public void testAspectMany(){
|
||||
userService.aspectj1();
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.bjmashibing.shiro.patterns.proxy.springaop;
|
||||
|
||||
import org.springframework.aop.MethodBeforeAdvice;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-13 14:48
|
||||
*/
|
||||
public class BeforeAdvice implements MethodBeforeAdvice{
|
||||
@Override
|
||||
public void before(Method method, Object[] objects, Object o) throws Throwable {
|
||||
System.out.println("代理方法执行:"+method.getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.bjmashibing.shiro.patterns.proxy.springaop;
|
||||
|
||||
import com.bjmashibing.shiro.moduler.service.UserService;
|
||||
import com.bjmashibing.shiro.moduler.service.impl.UserServiceImpl;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-13 14:47
|
||||
*/
|
||||
public class SpringAdviceTest {
|
||||
@Test
|
||||
public void testAdvice() {
|
||||
UserService target = new UserServiceImpl();
|
||||
BeforeAdvice beforeAdvice = new BeforeAdvice();
|
||||
ProxyFactory proxyFactory = new ProxyFactory();
|
||||
proxyFactory.setTarget(target);
|
||||
proxyFactory.addAdvice(beforeAdvice);
|
||||
|
||||
UserService proxy = (UserService) proxyFactory.getProxy();
|
||||
proxy.aspectj1();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.bjmashibing.shiro.patterns.proxy.springaop;
|
||||
|
||||
import com.bjmashibing.shiro.ApplicationTests;
|
||||
import com.bjmashibing.shiro.moduler.service.UserService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* <p></p>
|
||||
*
|
||||
* @author sunzhiqiang23
|
||||
* @date 2020-06-09 21:45
|
||||
*/
|
||||
public class UserServiceTest extends ApplicationTests {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Test
|
||||
public void test(){
|
||||
userService.parent();
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue