นี่คือสคริปต์ที่รวมการ เช็ค Bean ชนกัน เข้ากับโครงสร้างแบบวนลูปทุก Sub-directory จะช่วยให้สคริปต์ยืดหยุ่นมาก ไม่ว่าคุณจะย้ายโปรเจกต์ไปไว้ที่ไหนก็ยังใช้งานได้
check-beans-all-repos.sh
BASH / Shell
#!/bin/bash
echo "🚀 Starting Advanced Bean Collision Check..."
echo "Location: $ (pwd) "
echo "--------------------------------------------------"
for dir in */; do
dirname=${dir%/}
if [ -d "$dir" ] && [ "$dirname" != "target" ]; then
echo "📂 Processing: $dirname"
# 1. Identify Duplicate Class Names and Analyze Bean Status
find "$dir" -name "*.java" -not -path "*/target/*" | xargs basename 2>/dev/null | sort | uniq -d | while read -r fileName; do
className="${fileName%.java}"
echo " 🔍 Analyzing Duplicate Class: [$className]"
# Find all paths for this class name
find "$dir" -name "$fileName" -not -path "*/target/*" | while read -r filePath; do
# Check for Spring Annotations
annotation=$ (grep -E "@Component|@Service|@RestController|@Repository|@Configuration" "$filePath") if [ -z "$annotation" ]; then
echo " ⚪ POJO: $filePath (Not a Spring Bean) "
else
# Extract Bean Name from annotation if exists, e.g., @Component ("myBean") specificName=$ (echo "$annotation" | sed -n 's/.* ("\ ([^"]*\) ") .*/\1/p') if [ -z "$specificName" ]; then
# Spring default: class name with first letter lowercase
defaultBeanId="$ (tr '[:upper:]' '[:lower:]' <<< ${className:0:1}) ${className:1}"
echo " ✅ Spring Bean (Default Name) : $defaultBeanId"
echo " Path: $filePath"
else
echo " 🆔 Spring Bean (Named) : $specificName"
echo " Path: $filePath"
fi
fi
done
echo ""
done
# 2. Check for Duplicate Bean IDs in XML files (Direct Collision) grep -r "id=" "$dir" --include="*.xml" -not -path "*/target/*" 2>/dev/null | sed -n 's/.*id="\ ([^"]*\) ".*/\1/p' | sort | uniq -d | while read -r beanId; do
echo " ❌ FATAL: Duplicate XML Bean ID: [$beanId]"
grep -r "id=\"$beanId\"" "$dir" --include="*.xml" -not -path "*/target/*" | sed 's/^/ 📍 /'
done
fi
done
echo "--------------------------------------------------"
echo "✅ All checks completed."