Seasar Conference 2009 Autumnã§ã®ãã¢
Seasar Conference 2009 Autumn ãJavaããã°ã©ãã«æ§ããScalaå ¥éãã§ãã¢ã§ä½¿ã£ãã½ã¼ã¹ã§ãã
S2Console
åæã«ååã¤ãã¾ããããè¦ã¯Scalaã®ã¤ã³ã¿ããªã¿ããS2Containerãæä½ããããã®ã¹ã¯ãªããã§ãã
ãã®ã¹ã¯ãªããã¯Seasarç¨ã§ãããåããããªèãæ¹ã§GuiceãSpringãªã©ä»ã®ã³ã³ãããå©ç¨ã§ããã§ãããã
import _root_.scala.tools.nsc.MainGenericRunner import scala.reflect.Manifest import scala.collection.jcl.Conversions._ import org.seasar.framework.container.factory.SingletonS2ContainerFactory import org.seasar.framework.container.S2Container import org.seasar.extension.jdbc.JdbcManager // your pakcages import com.yuroyoro.scala.sastruts.service._ import com.yuroyoro.scala.sastruts.entity._ val cc = Thread.currentThread( ).getContextClassLoader Thread.currentThread( ).setContextClassLoader( cc.getParent.getParent ) implicit lazy val ct:S2Container = { SingletonS2ContainerFactory.init SingletonS2ContainerFactory.getContainer } def typeOf[T](implicit ct: S2Container, m: Manifest[T]):T = ct.getComponent( m.erasure ).asInstanceOf[T] def nameOf[T]( name:String )(implicit ct: S2Container, m: Manifest[T]):T = ct.getComponent( name ).asInstanceOf[T] val jm = typeOf[JdbcManager]
使ãæ¹
1.pom.xmlã«ãmaven-scala-toolsã追è¨ãã¾ãã
<!-- ⼠追å --> <properties> <scala.version>2.7.5</scala.version> </properties> <!-- Ⲡ追å --> <pluginRepositories> <!-- ⼠追å --> <pluginRepository> <id>scala-tools.org</id> <name>Scala-Tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </pluginRepository> <!-- Ⲡ追å --> </pluginRepositories> <repositories> <!-- ⼠追å --> <repository> <id>scala-tools.org</id> <name>Scala-Tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </repository> <!-- Ⲡ追å --> </repositories> <dependencies> <!-- ⼠追å --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-compiler</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-tools.testing</groupId> <artifactId>specs</artifactId> <version>1.4.4</version> <scope>test</scope> </dependency> <!-- Ⲡ追å --> </dependencies> <build> <plugins> <!-- ⼠追å --> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> <!-- Ⲡ追å --> </plugins> </build>
2.ããã±ã¼ã¸ã®ã¤ã³ãã¼ãã追å ãã
S2Console.scalaã®ä¸ã§ãå©ç¨ããããã¸ã§ã¯ãã®ããã±ã¼ã¸åã«å¿ããimportã追å ãã¾ãã
// your pakcages ããã®ããã±ã¼ã¸åãä¿®æ£ãã¦ãâ import com.yuroyoro.scala.sastruts.service._ import com.yuroyoro.scala.sastruts.entity._
3.ã³ã³ãã¤ã«ãã¦ãã
mvn complieãªã©ã§ã³ã³ãã¤ã«ãã¾ãã
4.ã¤ã³ã¿ããªã¿èµ·å
mvn scala:console ã§scalaã¤ã³ã¿ããªã¿ãèµ·åãã¾ãã
5.S2Console.scalaãLoadãã
ã¤ã³ã¿ããªã¿ä¸ã§ã:load S2Console.scalaã¨å ¥åãã¦ã¹ã¯ãªãããèªã¿è¾¼ã¿ã¾ãã
scala > :load S2Console.scala
6.ã³ã³ãã¼ãã³ããåãåºã
typeOf[ã¯ã©ã¹å]ãnameOf("ã³ã³ãã¼ãã³ãå")ã§ã³ã³ãã¼ãã³ããã³ã³ããããåå¾ã§ãã¾ãã
scala > val dept = typeOf[DeptService] scala > dept.findAll // æ¤ç´¢ scala > dept.findAll.foreach( d => println( d.deptNo + ":" + d.deptName )) // scalaã®ã³ã¬ã¯ã·ã§ã³APIãå©ç¨ 10:ACCOUNTING 20:RESEARCH 30:SALES 40:OPERATIONS 99:Hoge 100:hogehoge scala> val d = new Dept // entityãä½æ scala> d.deptNo = 111 scala> d.deptName = "Dept1" scala> d.loc="Tokyo" scala> dept.insert(d) // insertãã!
ããã·ã¥ã¿ã°æ½åº
S2ConHashTag.scala
contentå
ã®HTMLã¿ã°é¤å»ã§ãä½æ³çã«ãããªããã¨ãã¦ã¾ããã§ããã¯ãã¾ãåèã«ããªãã§ï¼ï¼
import scala.xml._ import scala.io.Source object S2ConHashTag extends Application{ val url = "http://search.twitter.com/search.atom?q=sc2009autumn" val source = Source.fromURL( url ) val xml = XML.loadString( source.getLines.mkString ) for( entry <- xml \\ "entry" ; c = entry \\ "content" text ; user = entry \\ "author" \\ "name" text ){ val cxml = XML.loadString("<c>" + c + "</c>" ) println(user + ":" + cxml.child.text) } }