本文基於開源項目 quick4j,深度解析企業級 Java Web 基礎架構:Maven 依賴管理、Web.xml 配置、Spring 核心容器、MyBatis 整合、Shiro 權限、Druid 監控、Ehcache 緩存、MyBatis 分頁插件、Log4j 日誌、國際化驗證,提供可直接生產落地的完整配置體系。
quick4j
├── src/main/java # Java 源碼 (controller/service/dao/model)
├── src/main/resources # 配置文件 (spring, mybatis, shiro, ehcache, log4j)
├── src/main/webapp # Web 資源 (JSP, 靜態資源)
├── src/test/java # 測試代碼 (含 MyBatis Generator 配置)
├── pom.xml # Maven 核心配置
└── quick4j.sql # 數據庫腳本
ContextLoaderListener 加載 applicationContext.xml、applicationContext-shiro.xml。CharacterEncodingFilter 強制 UTF-8。DelegatingFilterProxy 代理 shiroFilter。DispatcherServlet 攔截 /rest/*,REST 風格最佳性能。base-package="com.eliteams.quick4j"。PropertyPlaceholderConfigurer 載入 application.properties。SqlSessionFactoryBean (dataSource + configLocation + mapperLocations) + MapperScannerConfigurer 掃描 DAO 接口。DataSourceTransactionManager + tx:advice (insert/update/delete REQUIRED 回滾、select/count 唯讀) + aop:config 切面 Service 層 + tx:annotation-driven 支援 @Transactional。aop:aspectj-autoproxy proxy-target-class="true"。cache:annotation-driven + EhCacheCacheManager。
jdbc.url=jdbc:mysql://localhost:3306/quick4j?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin123
ds.initialSize=1
ds.minIdle=1
ds.maxActive=20
ds.maxWait=60000
ds.timeBetweenEvictionRunsMillis=60000
ds.minEvictableIdleTimeMillis=300000
PaginationResultSetHandlerInterceptor + PaginationStatementHandlerInterceptor 實現物理分頁。lazyLoadingEnabled=true、aggressiveLazyLoading=false、mapUnderscoreToCamelCase=true、localCacheScope=SESSION、jdbcTypeForNull=OTHER。com.eliteams.quick4j.web.model、enums 包。mvc:annotation-driven + Hibernate Validator + 國際化 MessageSource。InternalResourceViewResolver (前綴 /WEB-INF/views/,後綴 .jsp)。CommonsMultipartResolver (10GB 上限)。AuthorizationAttributeSourceAdvisor。ShiroFilterFactoryBean:anon 靜態資源/登錄頁,authc 其他受保護。DefaultWebSecurityManager + 自定義 Realm (需實現 doGetAuthenticationInfo/doGetAuthorizationInfo)。LifecycleBeanPostProcessor 管理 Shiro Bean 生命週期。標準 RBAC 模型:user、role、permission、user_role、role_permission 五表,支援細粒度權限控制 (如 user:create)。


git clone https://github.com/Eliteams/quick4j.gitquick4j.sql。application.properties 數據庫連接。mvn clean install。https://localhost:8080/quick4j/rest/index。starzou / 密碼 123456 (BCrypt 加密存儲)。| 維度 | 方案 | 收益 |
|---|---|---|
| 連接池 | Druid | 監控、防火牆、PSCache、SQL 統計 |
| 分頁 | MyBatis 攔截器物理分頁 | 無感知、跨數據庫、避免內存溢出 |
| 緩存 | Ehcache + Spring Cache 注解 | 聲明式、支援 JSR-107、分佈式可擴展 |
| 事務 | AOP + 注解雙模式 | Service 層統一、方法級精細控制 |
| 安全 | Shiro Realm + 註解授權 | 輕量、非侵入、支援 Session 共享 |
| 驗證 | Hibernate Validator + 國際化 | JSR-303 標準、前後端統一錯誤碼 |
| 日誌 | SLF4J + Log4j DailyRolling | 按天切割、生產級輸出格式 |
這套架構經過生產環境驗證,核心優勢在於配置集中化、組件解耦合、性能監控內建、安全權限完備。建議作為微服務拆分前的單體最佳實踐基線,或作為學習 Spring 生態整合的標準教材。