Python 官方文档:入门教程 => 点击学习
目录在注解上使用SPEL表达式注入对象场景描述具体案例补充spring属性注入方式之SPEL表达式在注解上使用SPEL表达式注入对象 场景描述 在平时开发中,我们经常通过定义一些注解
在平时开发中,我们经常通过定义一些注解,进行轻量级开发。
今天主要研究的内容是关于如何在注解上通过spel表达式注入对象,以此调用注入对象的具体业务处理逻辑,然后在通过对表达式的解析,进而获取该业务逻辑处理的结果,类似于Spring Security中的@PreAuthorize, @PreAuthorize, @PostAuthorize等注解,本次场景案例以模仿@PreAuthorize注解进行分析。
定义@SpelPreAuthorize注解,对标@PreAuthorize
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SpelPreAuthorize {
String value() default "";
}
定义具体业务逻辑处理类
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component("ps")
public class PermissionService {
public boolean hasPermission(String permission) {
List<String> allPermissions = Arrays.asList("user:save", "user:delete", "user:edit");
return allPermissions.contains(permission);
}
}
定义切面
import com.czf.ebao.data.spel.annoation.SpelPreAuthorize;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class SpelPreAuthorizeAspect {
@Autowired
private DefaultListableBeanFactory defaultListableBeanFactory;
@Before("@annotation(spelPreAuthorize)")
public void perAuthorize(JoinPoint point, SpelPreAuthorize spelPreAuthorize) {
String permission = spelPreAuthorize.value();
// 实例化spel表达式解析器
SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
// 解析表达式内容
Expression expression = spelExpressionParser.parseExpression(permission);
// 声明StandardEvaluationContext对象,用于设置上下文对象。
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(defaultListableBeanFactory));
Boolean result = expression.getValue(context, Boolean.class);
if (!result) {
throw new RuntimeException("该用户无访问权限");
}
}
}
定义测试类
import com.czf.ebao.data.spel.annoation.SpelPreAuthorize;
import org.springframework.WEB.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/spel")
public class SpelController {
@GetMapping("/hello")
@SpelPreAuthorize("@pms.hasPermission('user:hello')")
public String sayHello() {
return "hello";
}
}
通配符匹配
// import org.springframework.util.PatternMatchUtils
List<String> allPermissions = Arrays.asList("user:save", "user:delete", "user:edit");
return allPermissions.stream().anyMatch(item -> PatternMatchUtils.simpleMatch(permission, item));
<bean id="categroy" class="com.test.inject.CateGory" p:cateName="服装"></bean>
<bean id="calculate" class="com.test.inject.PriceCalculate"></bean>
<bean id="product" class="com.test.inject.Product" p:name="A21的T恤" p:categroy-ref="categroy">
<property name="price" value="#{calculate.calPrice()}"></property>
</bean>
spel表达式的好处就是,我们可以使用定义好的某个类中的方法来产生值,然后注入给所需要的对象使用
比较适用于比较复杂的bean注入
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: SpringAOP如何在注解上使用SPEL表达式注入对象
本文链接: https://www.lsjlt.com/news/139539.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0