SpringBoot:@Conditional 分手后的思念是犯贱 2023-03-13 03:18 14阅读 0赞 1. package tju.SpringBootSecurityJdbc.controller; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; public class MyCondition implements Condition{ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment env=context.getEnvironment(); if(env.containsProperty("myswitch")) { String str=env.getProperty("myswitch"); if(str.equals("true"))return true; } return false; } } 1. package tju.SpringBootSecurityJdbc.controller; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Conditional(MyCondition.class) public class AppController3 { @RequestMapping("/halu2") public String testme() { return "this is rest2!"; } } 1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=root logging.level.org.springframework.security=trace spring.thymeleaf.cache=false test.fgh.enabled=true my.user.num=2 com.abc=fgh myswitch=true
还没有评论,来说两句吧...