本文主要講的是如何使用Java Bean來配置Spring,而不是用xml來配置Spring。java
本文主要是代碼,須要注意的都在註釋裏面。spring
1 package com.wisely.highlight_spring4.ch1.javaconfig; 2 3 /** 4 * 最終被調用的類 5 */ 6 public class FunctionService { 7 public String sayHello(String word){ 8 return "Hello " + word +" !"; 9 } 10 }
1 package com.wisely.highlight_spring4.ch1.javaconfig; 2 3 /** 4 * 調用類 5 */ 6 public class UseFunctionService { 7 //這裏也能夠使用@Autowired將FunctionService的實體bean注入到UseFunctionService中, 8 //讓UseFunctionService具有將FunctionService的實體bean注入到UseFunctionService中的功能 9 //@Autowired 10 FunctionService functionService; 11 12 public void setFunctionService(FunctionService functionService) { 13 this.functionService = functionService; 14 } 15 16 public String SayHello(String word){ 17 return functionService.sayHello(word); 18 } 19 }
1 package com.wisely.highlight_spring4.ch1.javaconfig; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 6 /** 7 * 配置類,代替xml配置 8 */ 9 //聲明當前類是一個配置類 10 @Configuration 11 //自動掃描包名下全部使用@Service/@Component/@Repository和@Controller的類,並註冊爲Bean 12 //@ComponentScan("com.wisely.highlight_spring4.ch1") 13 public class JavaConfig { 14 /** 15 * 聲明當前方法的返回值是一個bean,bean的名稱是方法名 16 * @return bean 17 */ 18 @Bean 19 public FunctionService functionService(){ 20 return new FunctionService(); 21 } 22 23 @Bean 24 public UseFunctionService useFunctionService(){ 25 UseFunctionService useFunctionService = new UseFunctionService(); 26 //注入FunctionService的時候直接調用functionService 27 useFunctionService.setFunctionService(functionService()); //3 28 return useFunctionService; 29 30 } 31 /** 32 * 也能夠將bean做爲方法參數傳入,spring會自動注入 33 */ 34 // @Bean 35 // public UseFunctionService useFunctionService(FunctionService functionService){//4 36 // UseFunctionService useFunctionService = new UseFunctionService(); 37 // useFunctionService.setFunctionService(functionService); 38 // return useFunctionService; 39 // } 40 }
1 package com.wisely.highlight_spring4.ch1.javaconfig; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 5 /** 6 * 程序入口 7 */ 8 public class Main { 9 public static void main(String[] args) { 10 //使用AnnotationConfigApplicationContext做爲容器, 11 //接受輸入一個配置類做爲參數 12 AnnotationConfigApplicationContext context = 13 new AnnotationConfigApplicationContext(JavaConfig.class); 14 //得到聲明配置的UseFunctionService的Bean 15 UseFunctionService useFunctionService = context.getBean(UseFunctionService.class); 16 //調用輸出方法 17 System.out.println(useFunctionService.SayHello("java config")); 18 context.close(); 19 } 20 }
最終效果以下所示spa
1 package com.wisely.highlight_spring4.ch1.aop; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Retention; 6 import java.lang.annotation.RetentionPolicy; 7 import java.lang.annotation.Target; 8 9 /** 10 * 攔截規則的註解 11 */ 12 @Target(ElementType.METHOD) 13 @Retention(RetentionPolicy.RUNTIME) 14 @Documented 15 public @interface Action { 16 //註解參數 17 String name(); 18 }
1 package com.wisely.highlight_spring4.ch1.aop; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 使用註解的被攔截類 7 */ 8 @Service 9 public class DemoAnnotationService { 10 @Action(name="註解式攔截的add操做") 11 public void add(){} 12 }
1 package com.wisely.highlight_spring4.ch1.aop; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 使用方法規則被攔截類 7 */ 8 @Service 9 public class DemoMethodService { 10 public void add(){} 11 }
1 package com.wisely.highlight_spring4.ch1.aop; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.Aspect; 6 import org.aspectj.lang.annotation.Before; 7 import org.aspectj.lang.annotation.Pointcut; 8 import org.aspectj.lang.reflect.MethodSignature; 9 import org.springframework.stereotype.Component; 10 11 import java.lang.reflect.Method; 12 13 /** 14 * 配置切面 15 */ 16 @Aspect //聲明切面 17 @Component //讓切面成爲spring容器管理的bean 18 public class LogAspect { 19 //聲明切點,攔截Action 20 @Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") 21 public void annotationPointCut() { 22 } 23 24 //聲明建言,並使用@Pointcut定義的切點annotationPointCut() 25 @After("annotationPointCut()") 26 public void after(JoinPoint joinPoint) { 27 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); 28 Method method = signature.getMethod(); 29 Action action = method.getAnnotation(Action.class); 30 //經過反射獲可得註解上的name屬性,而後作日誌記錄相關的操做 31 System.out.println("註解式攔截 " + action.name()); 32 } 33 34 //聲明建言,直接使用攔截規則做爲參數 35 @Before("execution(* com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") 36 public void before(JoinPoint joinPoint) { 37 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); 38 Method method = signature.getMethod(); 39 System.out.println("方法規則式攔截," + method.getName()); 40 } 41 }
1 package com.wisely.highlight_spring4.ch1.aop; 2 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 7 /** 8 * Spring配置類,由於程序中使用了諸如@Service之類的配置,因此這裏不用寫其餘配置 9 * 只須要使用@ComponentScan掃描一下就能夠了 10 */ 11 @Configuration 12 //掃描註解,註冊bean 13 @ComponentScan("com.wisely.highlight_spring4.ch1.aop") 14 //開啓spring對AspectJ的支持 15 @EnableAspectJAutoProxy //1 16 public class AopConfig { 17 18 }
1 package com.wisely.highlight_spring4.ch1.aop; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 5 /** 6 * 程序入口 7 */ 8 public class Main { 9 public static void main(String[] args) { 10 AnnotationConfigApplicationContext context = 11 new AnnotationConfigApplicationContext(AopConfig.class); 12 DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); 13 DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); 14 demoAnnotationService.add(); 15 demoMethodService.add(); 16 context.close(); 17 } 18 }
最終效果以下所示日誌