springboot学习章节代码-spring基础
1、DI
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
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
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")); }
}
springboot学习章节代码-spring基础的更多相关文章
- springboot学习章节代码-Spring MVC基础
1.项目搭建. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- springboot学习章节代码-spring高级话题
1.Spring Aware(获取Spring容器的服务) hi, i am guodaxia! test.txt package com.zhen.highlights_spring4.ch3.aw ...
- SpringBoot学习(一)基础篇
目录 关于Springboot Springboot优势 快速入门 关于SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭 ...
- springboot学习章节-spring常用配置
1.Scope package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation ...
- springboot学习之授权Spring Security
SpringSecurity核心功能:认证.授权.攻击防护(防止伪造身份) 涉及的依赖如下: <dependency> <groupId>org.springframework ...
- 1.1(Spring学习笔记)Spring基础(BeanFactory、ApplicationContext 、依赖注入)
1.准备工作 下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/ 选择需要下载的版本 ...
- SpringBoot学习笔记:Spring Data Jpa的使用
更多请关注公众号 Spring Data Jpa 简介 JPA JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范(JSR ...
- SpringBoot学习笔记(2) Spring Boot的一些配置
外部配置 Spring Boot允许使用properties文件.yaml文件或者命令行参数作为外部配置 使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Enviro ...
- Springboot学习:Thymeleaf 语法基础
详细内容见:Thymeleaf Tutorial 中文翻译,中文文档 参考: thymeleaf官方指南 新一代Java模板引擎Thymeleaf Thymeleaf基本知识 thymeleaf总结文 ...
随机推荐
- eclipse---->error and exception 和常用配置
1.eclipse打开后报错:An internal error occurred during: "Initializing Java Tooling". java.lang.N ...
- fedora21 中lamp的搭建(测试没有问题)
LAMP Stands for Linux,Apache,MySQL and PHP. Most of the websites works with the above combination. T ...
- Confluence 6 设置一个空间主页
当你创建一个空间的时候,Confluence 将会自动为你创建的空间新建一个主页.如果你的空间是从蓝图中创建,并且蓝图中已经有一个供使用的主页了,那么这个主页将会自动从蓝图中载入有用的宏和在蓝图中指定 ...
- apiCloud 播放视频
api.openVideo({ url:'https://fabu.chenchaoweb.cn/./Uploads/5a3b72be2b102.mp4' (路径必须是完整路径) });
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- 使用scp上传和下载文件
利用scp传输文件 1.从服务器下载文件 scp username@servername:/path/filename /tmp/local_destination 例如scp codinglog@1 ...
- function_exists
在已经定义的函数列表(包括系统自带的函数和用户自定义的函数)中查找 function_name. 如果 function_name 存在且的确是一个函数就返回 TRUE ,反之则返回 FALSE .
- 第二阶段——个人工作总结DAY02
1.昨天做了什么:昨天学习了Intent跳转的知识. 2.今天打算做什么:来实现这个功能. 3.遇到的困难:不会用隐式跳转,只会用显式跳转.
- 26. Remove Duplicates from Sorted Array C++ 删除排序数组中的重复项
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 双指针,注意初始时左右指针指向首元素! class Solutio ...
- 数据结构与算法之PHP实现队列、栈
一.队列 1)队列(Queue)是一种先进先出(FIFO)的线性表,它只允许在表的前端进行删除操作,在表的后端进行插入操作,进行插入操作的端称为队尾,进行删除操作的端称为队头.即入队只能从队尾入,出队 ...