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总结文 ...
随机推荐
- java.io.Serializable中serialVersionUID的作用
把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中: 2) 在网 ...
- Lua常用封装方法
Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...
- vue核心之响应式原理(双向绑定/数据驱动)
实例化一个vue对象时, Observer类将每个目标对象(即data)的键值转换成getter/setter形式,用于进行依赖收集以及调度更新. Observer src/core/observer ...
- python面向对象之 类
内容梗概: 1. 类的成员 2. 类的成员-变量 3. 类的成员-方法 4. 类的成员-属性 5. 私有 1.类的成员class 类名: 方法 def __init__(self, 参数1, 参数2. ...
- IntelliJ IDEA的调试方法
快捷键F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 S ...
- strom:实时的WordCount
集采单词 package wordcount; import java.io.File; import java.io.IOException; import java.util.Collection ...
- 剑指offer-整数中1出现的次数
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- InnoDB存储引擎介绍-(6) 一. Innodb Antelope 和Barracuda区别
分类 Antelope是innodb-base的文件格式,Barracude是innodb-plugin后引入的文件格式,同时Barracude也支持Antelope文件格式.两者区别在于: 文件格式 ...
- 查看某一职责下对应的菜单&功能&请求(转)
原文地址:查看某一职责下对应的菜单&功能&请求 查看菜单&功能 SELECT res.RESPONSIBILITY_NAME 职责名称, menu.MENU_NAME 菜单编码 ...
- Spring注解之@validated的使用
spring-boot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理.比如,我们判断一个输入参数是否合法,可以用如下方式 一 基础使用 因为spring-b ...