Struts2というよりSpring2.5

昨日の続きでSpring2.5のAutowiredを使ってActionをインジェクションしてみる。

conponent-scanで自動バインディング

2.5から追加されたこのステキ?機能w
contextスキーマを追加するだけで使えます。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">

    <context:component-scan base-package="struts2" />
    <context:property-placeholder 
        location="classpath:database.properties"/>
    <tx:annotation-driven />
    
    <bean id="helloWorld"
        class="struts2.sample.HelloWorld" scope="request">
    </bean>
    
    < !-- Transaction Manager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    < !-- DataSource (Apache DBCP) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="${driverClassName}" />
        <property name="url"
            value="${url}" />
        <property name="username"
            value="${username}" />
        <property name="password"
            value="${password}" />
        <property name="initialSize"
            value="1" />
        <property name="maxActive"
            value="5" />
        <property name="maxIdle"
            value="2" />
        <property name="maxWait"
            value="-1" />
        <property name="defaultAutoCommit"
            value="false" />
    </bean>
</beans>

「base-package」で指定した以下のクラスを検索して登録してくれる。
DataSourceとか個別に(従来通り)記述してもOK。

Actionクラス

@Controller
@Scope("prototype")
public class UserInfoAction extends ActionSupport implements Preparable, RequestAware {
    // facadeクラスもcomponent-scanで登録→DI
    @Autowired
    private UserInfoService userInfoService;
    private Map request;

    @Override
    public void prepare() throws Exception {
    }
    @Override
    public void setRequest(Map request) {
        this.request = request;
    }
    public String loginAction() {
        return SUCCESS;
    }
}

ここで重要なのがSpringが提供してるマーカーアノテーション
「@Controller」←これが付いてるのでcomponent-scanで拾われると。
あとはstrutsのAction定義を書けばよろし。

        <action name="login" method="loginAction" class="userInfoAction">
            <result>/login.jsp</result>
        </action>


Springの定義ファイルの記述が減るのはやっぱいいね。
その代わりトランザクション定義とかアノテーションで書いていくと、ソースファイルの可読性が悪くなる場合も。
(たいていはデフォルトの振る舞いでいいと思うけど)