Spring Boot學習,熟悉注解和Java式的配置。源代碼見Source-Sample-GitHub。
Spring配置方式
- 配置檔案
(Spring 1.x) 》》xml
- 注解
(Spring 2.x+Jdk1.5)》》@annotation
- Java配置(Spring 3/4)
SpringBoot
就是基于Java配置(注解的應用)
Spring元件
- 核心元件(Core)
- -Core:核心工具類,Spring其他子產品的主要依賴
- -Bean:對
的支援bean
- -Context:運作時容器
- -Context-Support:容器對第三方包的內建支援
- -Expression:使用表達式語言查詢操作運作時的對象
- AOP
- -AOP:基于代理的AOP支援
- -Aspects:基于AspectJ的AOP支援
- Messaging
- -Messaging:對消息架構和協定的支援
- Web
- -Web:基礎的內建功能,為Web項目提供Spring容器
- -Webmvc:基于Servlet的Spring MVC
- -WebSocket:提供WebSocket功能
- -Webmvc-Portlet:支援Portlet環境
- Data Access/Integration
- -JDBC:以JDBC方式通路資料庫
- -TX:事務支援
- -ORM:對象關系映射支援
- -OXM:對象xml映射支援
- -JMS:對JMS的支援
Spring生态圈(獨立項目)
- -Boot:使用預設配置快速開發
- -XD:簡化大資料應用開發
- -Cloud:分布式系統開發工具集
- -Data:對主流關系型/NoSql資料庫的支援
- -Integration:通過消息機制對企業內建模式的支援
- -Batch:簡化優化大資料批處理
- -Security:認證授權
- -Social:與社交網絡API的繼承
- -Mobile:對手機裝置的識别
- -Web Services:基于協定有限的SOAP/Web服務
- -Session:提供API管理使用者會話
- …
Spring原則
- 使用POJO進行輕量級和最小侵入式開發
- 通過依賴注入和基于接口程式設計降低耦合
- 通過AOP和預設習慣進行聲明式程式設計
- 使用AOP和模闆減少模式化(重複的)代碼
依賴注入 (IOC+DI)
即由Spring容器建立管理Bean(POJO類),通過注解/XML/JAVA告訴容器應該将Bean何時放在何地。
聲明Bean:
* @Component 通用
* @Service 在業務邏輯層使用
* @Repository 資料通路層使用
* @Controller 展現層使用
引入Bean:
* @Autowired Spring提供的注解
* @Inject/@Resource JSR-330/JSR-250提供的注解
JAVA配置
@Configuration 表示這個類是一個配置類,等效于一個xml檔案
@Bean 注解一個方法,将方法傳回值注冊為一個bean
- 配置注解化
@ComponentScan("com.controller")
- 配置Java化
@Configuration
public class JavaConfig{
@Bean
public MySwaggerConfig mySwaggerConfig(){
return new MySwaggerConfig();
}
}
使用Aspectj實作AOP
有兩種方式,一個是監聽方法(通過配置目标路徑),一個是監聽注解(在目标方法上放置特殊注解)
* 定義注解,作為被監聽的辨別
/**
* 注解本身是沒有作用的,隻是作為一個規則,關聯攔截者和被攔截者
*/
@Target(ElementType.METHOD)//用于方法
@Retention(RetentionPolicy.RUNTIME)//可用于運作時
@Documented
public @interface AopAction {
String name();
}
- 書寫被攔截的Service(注解式攔截,需要注解Service的方法)
/**
* 注解式攔截,在對應方法上加上@AopAction,在攔截時通過檢查注解進行攔截
*/
@Service
public class DemoAnnotationAopService {
@AopAction(name = "注解式AOP")
public String say() {
return "Hello,Annotation World.";
}
}
@Service
public class DemoMethodAopService {
public String say() {
return "Hello, Normal World.";
}
}
- 定義攔截器實作,定義攔截的對象和處理方法
/**
* Aop實作的地方
*/
@Aspect//聲明一個切面
@Component//托管給Spring容器
public class AspectAopImpl {
private final Logger log = LoggerFactory.getLogger(this.getClass());
/**
* 注解式,通過檢查注解位置來攔截
*/
@Pointcut("@annotation(demo.springboot.aop.impl.AopAction)")
public void annotationPointCut() {}
@Before("annotationPointCut()")
public void logBefore() { log.info("logBefore:現在時間是:" + new Date()); }
/**
* 通過指定攔截位置和具體方法
*/
@Pointcut("execution(* demo.springboot.aop.service.DemoMethodAopService.*(..))")
private void methodPointCut() {}
@After("methodPointCut()")
public void logAfter() { log.info("logAfter:現在時間是:" + new Date()); }
}
啟用AOP
注入配置檔案,開啟AspectJ
/**
* 用注解代替了xml
* <aop:aspectj-autoproxy proxy-target-class="true"/>
*/
@EnableAspectJAutoProxy
@Configuration
@ComponentScan("demo.springboot.aop")//在Application入口整體掃描,可省略此行
public class AopConfiguration {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
/**
* 測試兩種Aop
*/
DemoAnnotationAopService annotationAopService = context.getBean(DemoAnnotationAopService.class);
DemoMethodAopService methodAopService = context.getBean(DemoMethodAopService.class);
System.out.println(annotationAopService.say());
System.out.println(methodAopService.say());
context.close();
}
}