springboot学习章节代码-spring基础
2023-09-14 08:58:29 时间
1、DI
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zhen.highlights_spring4.ch1.di; import org.springframework.stereotype.Service; /** * @author zhen * @Date 2018/6/12 10:05 */ @Service public class FunctionService { public String sayHello(String word){ return "Hello " + word + " !"; } } package com.zhen.highlights_spring4.ch1.di; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author zhen * @Date 2018/6/12 10:07 */ @Service public class UseFunctionService { @Autowired private FunctionService functionService; public String sayHello(String word){ return functionService.sayHello(word); } } package com.zhen.highlights_spring4.ch1.di; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * @author zhen * @Date 2018/6/12 10:09 */ @Configuration @ComponentScan("com.zhen.highlights_spring4.ch1.di") public class DiConfig { } package com.zhen.highlights_spring4.ch1.di; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author zhen * @Date 2018/6/12 10:10 */ public class Main { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class); UseFunctionService useFunctionService = context.getBean(UseFunctionService.class); System.out.println(useFunctionService.sayHello("di")); context.close(); } }
2、AOP
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zhen.highlights_spring4.ch1.aop; import java.lang.annotation.*; /** * @author zhen * @Date 2018/6/12 10:45 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); } package com.zhen.highlights_spring4.ch1.aop; import org.springframework.stereotype.Service; /** * @author zhen * @Date 2018/6/12 10:48 */ @Service public class DemoAnnotationService { @Action(name = "注解式拦截的aop操作") public void add(){} } package com.zhen.highlights_spring4.ch1.aop; import org.springframework.stereotype.Service; /** * @author zhen * @Date 2018/6/12 10:49 */ @Service public class DemoMethodService { public void add(){} } package com.zhen.highlights_spring4.ch1.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * @author zhen * @Date 2018/6/12 10:54 */ @Aspect @Component public class LogAspect { @Pointcut("@annotation(com.zhen.highlights_spring4.ch1.aop.Action)") public void annotationPointCut(){} @After("annotationPointCut()") public void after(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.out.println("注解式拦截 " + action.name()); } @Before("execution(* com.zhen.highlights_spring4.ch1.aop.DemoMethodService.*(..))") public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature)joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截 " + method.getName()); } } package com.zhen.highlights_spring4.ch1.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * @author zhen * @Date 2018/6/12 11:31 */ @Configuration @ComponentScan("com.zhen.highlights_spring4.ch1.aop") @EnableAspectJAutoProxy public class AopConfig { } package com.zhen.highlights_spring4.ch1.aop; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author zhen * @Date 2018/6/12 11:33 */ public class Main { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); context.close(); } }
3.javaConfig
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zhen.highlights_spring4.ch1.javaconfig; /** * @author zhen * @Date 2018/6/12 10:14 */ public class FunctionService { public String sayHello(String word){ return "Hello " + word + " !"; } } package com.zhen.highlights_spring4.ch1.javaconfig; /** * @author zhen * @Date 2018/6/12 10:15 */ public class UseFunctionService { private FunctionService functionService; public String sayHello(String word){ return functionService.sayHello(word); } public FunctionService getFunctionService() { return functionService; } public void setFunctionService(FunctionService functionService) { this.functionService = functionService; } } package com.zhen.highlights_spring4.ch1.javaconfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author zhen * @Date 2018/6/12 10:17 */ @Configuration public class JavaConfig { @Bean public FunctionService functionService(){ return new FunctionService(); } @Bean public UseFunctionService useFunctionService(){ UseFunctionService useFunctionService = new UseFunctionService(); useFunctionService.setFunctionService(functionService()); return useFunctionService; } } package com.zhen.highlights_spring4.ch1.javaconfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author zhen * @Date 2018/6/12 10:22 */ public class Main { public static void main(String[] args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class); UseFunctionService useFunctionService = context.getBean(UseFunctionService.class); System.out.println(useFunctionService.sayHello("config")); } }
相关文章
- spring mvc 坑之PUT,DELETE方法接收不到请求参数
- springboot学习章节代码-Spring MVC基础
- springboot学习章节代码-spring高级话题
- Spring整合ActiveMQ完成消息队列MQ编程
- Spring Boot 应用使用 application.yml 和 application.properties 的区别
- Atitit 定时器timer 总结 目录 1. 定时器 循环定时器 和timeout超时定时器1 2. Spring定时器1 2.1. 大概流程1 2.2. 核心源码springboot1
- 为什么Spring Boot推荐使用logback-spring.xml来替代logback.xml来配置logback日志的问题分析
- 毕设/私活/bigold必备项目,一个挣钱的免费的全开源标准前后端分离后台管理权限系统【springboot+vue+redis+Spring Security】脚手架搭建:若依Ruo框架具体使用教程
- 解释spring,struts,hibernate优缺点
- Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found 用IDEA创建springboot项目的时候遇到的一些坑
- 【springboot系列】这样优化Spring Boot,启动速度快到飞起!
- 【【SpringBoot系列】最详细demo--自定义日志脱敏组件,简单3 步完成 Spring Boot 的日志脱敏
- 【spring】Spring Data --Spring Data JPA
- Springboot热部署,使用spring-boot-devtools、springloaded两种方式进行热部署
- 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
- spring集成mybatis配置多个数据源,通过aop自动切换
- Spring Security---ONE
- Spring Cloud Alibaba微服务第18章之消息服务
- Spring Cloud Gateway过滤器工厂的使用
- 【Spring Boot】SpringBoot项目jar、war包启动解析
- 【Spring Boot】SpringBoot参数验证以及实现原理