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();
    }
}
View Code

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();
    }
}
View Code

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"));

    }
}
View Code

 

posted @ 2018-07-02 17:03  guodaxia  阅读(174)  评论(0编辑  收藏  举报