ผมพยายามจะเขียนว่า Spring คืออะไร Dependency Injection (หรือ Inversion of Control) กับ Aspect oriented programming มาเกี่ยวอะไร แต่แหมมันก็ยากไปหน่อย แต่ถ้าก็อ่าน draft ของ Spring in Action แทนได้ที่ http://www.manning.com/walls3/walls_meapch1.pdf หรือชอบใจอยากอ่านทั้งเล่มก็ดูได้ที่ http://www.manning.com/walls3/ (เสียเงิน) เขายังเขียนไม่เสร็จนะแต่ก็ขายได้
หาเรื่องที่ง่ายเขียนดีกว่า การเล่น Spring + Hibernate ครั้งนี้ ผมใช้ Maven (ที่ผมรักนักรักหนา) แทนที่จะเป็น Ant
ขั้นตอนแรกก็เริ่มจากการสร้าง project ของ Maven เลย ง่ายๆ แต่ไม่เคยจำได้
mvn archetype:create -DgroupId=org.kunyit -DartifactId=sprib
เพียงเท่านี้ Maven ก็จะสร้าง Project เป็นโครงๆ ขึ้นมาให้ทันที ต่อจากนั้นเราก็ไปแก้ pom.xml ว่า project ของเราต้องใช้ package อะไรบ้าง
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
แต่ว่าเพิ่มเท่านี้ก็ยังไม่พอที่จะใช้ Hibernate tool ที่ต้องใช้ Hibernate tool ก็เพราะว่าอยากให้ Hibernate create table ให้จาก .hbm.xml เลย (เพราะว่ายังงงๆ convention ของ Hibernate อยู่เลยไม่กล้า create เอง :-P)
สมมุติว่าใช้ mysql ละกันนะ ก็ต้องแก้ pom.xml อีกตามเคย
<repositories>
<repository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
เพราะว่า Hibernate3 plugin สำหรับมา Maven ไม่ได้อยู่ใน repository หลัก เราเลยต้องเพิ่มเข้าไปว่าจะไปดูด Hibernate3 plugin มาได้จากไหน
<build>
<extensions>
<extension>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>3.1.11</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
</component>
<component>
<name>hbm2hbmxml</name>
<outputDirectory>src/main/resources</outputDirectory>
</component>
</components>
<componentProperties>
<drop>true</drop>
<configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
</plugin>
</plugins>
</build>
ส่วน code ข้างบนนี่ อธิบายคร่าวก็คือบอกว่า ฉันจะใช้ Hibernate3 plugin นะ (อธิบายละเอียดไม่ได้เพราะว่า ผมงงอะ)
ทำมาพักใหญ่ผมก็เริ่มงงๆ ว่าเขียน Java หรือ XML แต่ไม่เป็นไร ผมจะยืดหยัดเขียนมันต่อไป แต่ว่าเอาไว้ต่อในตอนหน้าดีกว่า นี่มันยาวไปละ


เมนูของ vsatayamas





เมื่อ จ. 24 ก.ย. 2550 @ 19:12
396699 [ลบ]
ผมใช้ maven 2.0.7 ครับ
ลอง mvn archetype:create -DgroupId=org.kunyit -DartifactId=sprib
Build Error ครับ
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] **************************************************************
[INFO] Starting Jakarta Velocity v1.4
[INFO] RuntimeInstance initializing.
[INFO] Default Properties File: org\apache\velocity\runtime\defaults\velocity.pr
operties
[INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.
resource.ResourceManagerImpl)
[INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLo
aderResourceLoader
[INFO] ClasspathResourceLoader : initialization starting.
[INFO] ClasspathResourceLoader : initialization complete.
[INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.
ResourceCacheImpl)
[INFO] Default ResourceManager initialization complete.
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Include
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
[INFO] Created: 20 parsers.
[INFO] Velocimacro : initialization starting.
[INFO] Velocimacro : adding VMs from VM library template : VM_global_library.vm
[ERROR] ResourceManager : unable to find resource 'VM_global_library.vm' in any
resource loader.
[INFO] Velocimacro : error using VM library template VM_global_library.vm : org
.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'V
M_global_library.vm'
[INFO] Velocimacro : VM library template macro registration complete.
[INFO] Velocimacro : allowInline = true : VMs can be defined inline in templates
[INFO] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT
replace previous VM definitions
[INFO] Velocimacro : allowInlineLocal = false : VMs defined inline will be glob
al in scope if allowed.
[INFO] Velocimacro : initialization complete.
[INFO] Velocity successfully started.
[INFO] [archetype:create]
[INFO] Defaulting package to group ID: org.kunyit
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
for updates from apache-snapshots
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
for updates from apache-incubating
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
for updates from java.net
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
for updates from central
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
GroupId: org.apache.maven.archetypes
ArtifactId: maven-archetype-quickstart
Version: RELEASE
Reason: Unable to determine the release version
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=
maven-archetype-quickstart \
-Dversion=RELEASE -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=org.apache.maven.archetypes -DartifactId=mave
n-archetype-quickstart \
-Dversion=RELEASE -Dpackaging=jar -Dfile=/path/to/file \
-Durl=[url] -DrepositoryId=[id]
org.apache.maven.archetypes:maven-archetype-quickstart:jar:RELEASE
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9 seconds
[INFO] Finished at: Mon Sep 24 19:15:52 ICT 2007
[INFO] Final Memory: 4M/8M
[INFO] ----------------------------------------------------------------------แก้ไงดี