@Service("userService")
spring产生一个service层bean示例,就是相当于把
| UserServiceImpl userService = new UserServiceImpl(); |
这一步给自动化管理了。你要用的话直接引入就可以了
| @Service("userService") |
| public class UserServiceImpl implements UserService { |
| @Resource |
| private UserMapper userMapper; |
| @Override |
| public User getUserById(String id) { |
| return userMapper.getUserById(id); |
| } |
| } |
@Value注解使用自定义参数
springboot application.yml 配置
| server: |
| port: 80 |
| servlet: |
| context-path: |
| |
| custom: |
| value1: 测试参数1 |
| map: "{ds: '发送失败',ds3: '未发送,ds4: 发送成功'}" |
| list: "test,test32" |
自定义参数配置类
| |
| @Configuration |
| |
| @Data |
| public class CustomVariableConfig { |
| |
| @Value("${custom.value1:defaultValue}") |
| private String customValue1; |
| |
| @Value("#{${custom.map}}") |
| private Map<String,String> map; |
| @Value("#{'${custom.list}'.split(',')}") |
| private List<String> list; |
| } |
@ConfigurationProperties配置参数空间注解
java类
| @Component |
| |
| @Data |
| |
| @ConfigurationProperties("custom") |
| public class CustomVariableConfig { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Value("${custom.customValue1}") |
| private String customValue1; |
| |
| |
| |
| |
| private List<Map<String,String>> simpleViewControllers; |
| |
| } |
yml文件配置
| |
| custom: |
| customValue1: 测试参数1 |
| |
| map: "{ds: '发送失败',ds3: '未发送,ds4: 发送成功'}" |
| |
| list1: "test,test32" |
| |
| |
| simple-view-controllers: |
| - urlParame: index |
| templatePath: index |
| - urlParame: forgetPsd |
| templatePath: user/ordinary |
| - urlParame: iregister |
| templatePath: user/admin |
| - urlParame: boss |
| templatePath: user/boss |
@Autowired与@Resource的区别
@Autowired
1、本身无name属性,如果想通过名称匹配注入则需要配合@Qualifier注解一起使用。
| @Autowired() |
| @Qualifier("userService") |
| UserService userService; |
2、默认required为true,即叫这个名字的bean实例必须要有,不然报错。可手动设置为false,此时代码会有警告。妈蛋上传不了图片。就是下面的Qualifier注解属性会变成大红色。
| @Autowired(required = false) |
| @Qualifier("userService") |
| UserService userService; |
3、由spring提供
@Resource
1、name属性值设置则通过name匹配,
| @Resource(name = "userService") |
| UserService userService; |
| |
| @Service("userService") |
| public class UserServiceImpl implements UserService {} |
2、name属性值未设置则先按照名字后通过类型匹配
| @Resource() |
| UserService userService; |
| |
| @Service("userService") |
| public class UserServiceImpl implements UserService {} |
3、由jdk1.6开始提供
4、默认可为空
2021.8.8补充
从jdk11开始,移除了@Resource注解,该注解Spring家族通过引入 jakarta.annotation:jakarta.annotation-api:1.3.5 来支持该注解。

@ComponentScan
设置 spring 扫描包路径
| |
| @ComponentScan(basePackages = {"com.jdw.springboot.entity"}) |
| |
| @ComponentScan(basePackages = {"com.jdw.springboot.*","com.jdw.springboot2.*"}) |
用于告诉spring去哪里获取bean。spring不会再扫描所有注解的bean,而只会扫描指定的包下面的。而且这个太深了也不行。
| |
| @ComponentScan(basePackages = {"com.jdw.springboot.*"}) |
| |
| @ComponentScan(basePackages = {"com.*"}) |
注意
‘.*’ 不能扫到下面所有,最好还是写清楚。
| |
| @ComponentScan(basePackages = {"com.*"}) |
| @MapperScan("com.jdw.springboot.mapper") |
| |
| @ComponentScan(basePackages = {"com.jdw.*"}) |
| @MapperScan("com.jdw.springboot.mapper") |
@Component
直接写在类上面,标明这个类要被 spring 管理(相当于举手,并不一定被管理。)
| @Component |
| public class TimedTask { |
| |
| @Scheduled(fixedRate = 2000) |
| private void task1() throws Exception{ |
| System.out.println("2秒执行一遍定时任务"); |
| } |
| } |
@Aspect 面向切面编程相关
直接写在类上面,标明这个类是个切面类,即:
AOP (Aspect Oriented Programming)中的 Aspect
| |
| @Aspect |
| |
| @Component |
| public class ControllerAspect { |
| @Pointcut(value = " execution(public * com.jdw.sys.controller..*.*(..))") |
| public void webLog() { |
| } |
| |
| |
| |
| @After("webLog()") |
| public void doAfter() { |
| log.info("After"); |
| } |
| } |
@Pointcut
| |
| |
| |
| |
| |
| |
| @Pointcut(value = " execution(public * com.jdw.sys.controller..*.*(..))") |
| public void webLog() { |
| } |
2020.08.01补充:
spring 切面的思想是面向类切面,所以内部方法之间的调用时不会触发切面的。
失败案例:
| @RestController |
| @RequestMapping("/demo") |
| public class DemoController { |
| @GetMapping("/test") |
| public String test(){ |
| return test1(); |
| } |
| public String test1(){ |
| return "fdsdfs"; |
| } |
| } |
| |
| @Aspect |
| |
| @Component |
| |
| @Slf4j |
| public class ControllerAspect { |
| |
| |
| |
| |
| |
| |
| @Pointcut(value = " execution(public * com.jdw.sys.controller..*.test1(..))") |
| public void webLog() { |
| } |
@Before
| |
| @Before("webLog()") |
| public void doBefor(JoinPoint joinPoint) { |
| |
| ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| HttpServletRequest request = attributes.getRequest(); |
| |
| log.info("URL:" + request.getRequestURI()); |
| log.info("HTTP_METHOD:" + request.getMethod()); |
| |
| |
| |
| Enumeration<String> parameterNames = request.getParameterNames(); |
| log.info("请求参数列表为:"); |
| while (parameterNames.hasMoreElements()) { |
| String s = parameterNames.nextElement(); |
| log.info(s + ":" + request.getParameter(s)); |
| } |
| } |
@After
| |
| |
| |
| @After("webLog()") |
| public void doAfter() { |
| log.info("After"); |
| } |
@AfterReturning
| |
| |
| |
| @AfterReturning(returning = "object", pointcut = "webLog()") |
| public void doAfterReturning(Object object) { |
| log.info("RESPONSE:" + object); |
| } |
@AfterThrowing
| |
| |
| |
| @AfterThrowing(value = "webLog()", throwing = "exception") |
| public void doAfterThrowing(JoinPoint joinPoint, Exception exception) { |
| log.info(joinPoint.getSignature().getName() + "抛出异常:" + exception.getMessage()); |
| } |
@Around
2020.08。01补充:
ProceedingJoinPoint(程序连接点)类的几个实用方法,
| Object[] getArgs:返回目标方法的参数 |
| |
| Signature getSignature:返回目标方法的签名 |
| |
| Object getTarget:返回被织入增强处理的目标对象 |
| |
| Object getThis:返回AOP框架为目标对象生成的代理对象 |
| |
| |
| |
| |
| |
| |
| @Around("webLog()") |
| public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) { |
| log.info("- - - - - 环绕通知 - - - -"); |
| log.info("环绕通知的目标方法名:" + proceedingJoinPoint.getSignature().getName()); |
| try { |
| |
| Object obj = proceedingJoinPoint.proceed(); |
| return obj; |
| } catch (Throwable throwable) { |
| throwable.printStackTrace(); |
| } finally { |
| log.info("- - - - - 环绕通知 end - - - -"); |
| } |
| return null; |
| } |
@EnableScheduling+@Scheduled 集成定时任务
springboot的定时任务超简单,两个注解搞定。
@EnableScheduling
启动类添加 @EnableScheduling 注解,开启定时任务。
| @SpringBootApplication |
| |
| @ComponentScan(basePackages = {"com.jdw.*.*"}) |
| |
| @MapperScan("com.jdw.*.mapper") |
| |
| @EnableScheduling |
| public class SpringbootApplication { |
| public static void main(String[] args) { |
| SpringApplication.run(SpringbootApplication.class, args); |
| } |
| } |
bean类 方法上添加 @Scheduled 注解,标明这个方法是一个定时任务。
| |
| @Component |
| public class TimedTask { |
| private static int t = 0; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Scheduled(cron = "0/4 * * * * ?") |
| private void task1() throws Exception{ |
| t++; |
| System.out.println("task"+t+":定时任务开始"+(new Date()).getTime()); |
| Thread.sleep(2000); |
| |
| } |
| } |
| |
@Lazy懒加载注解
| @Controller |
| @RequestMapping("/gcjs/bszn") |
| public class GcjsBsznController { |
| @Autowired |
| |
| @Lazy |
| private ICCfgTypeService cCfgTypeService; |
| } |
@RequestBody 请求体注解
| |
| |
| |
| @PostMapping("test") |
| @RequestBody |
| public object test(@RequestBody Object object){ |
| return object; |
| } |
| |
| @JsonFormat(timezone = "GMT+8", pattern= "yyyy-MM-dd HH:mm:ss") |
| private LocalDateTime updateTime; |
| |
| @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
| private LocalDateTime updateTime; |
@SpringBootTest
@ActiveProfiles("native")
| |
| |
| |
| |
| |
| |
| @SpringBootTest |
| |
| @ActiveProfiles("native") |
| public class DemoWebTest { |
| @Test |
| void contextLoads() { |
| String t = "[ tt ts]"; |
| String substring = t.substring(1, t.length() - 1); |
| System.out.println(substring); |
| } |
| } |
@ConditionalOnProperty
| |
| @Configuration |
| @ConditionalOnProperty(prefix = "spring.profile", name = "active", havingValue = "dev") |
| public class RestTemplateConfig { |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律