• <td id="ae6ms"><li id="ae6ms"></li></td>
  • <xmp id="ae6ms"><td id="ae6ms"></td><table id="ae6ms"></table>
  • <table id="ae6ms"></table>
  • <td id="ae6ms"></td>
    <td id="ae6ms"></td>
  • <table id="ae6ms"></table><table id="ae6ms"><td id="ae6ms"></td></table>
  • <td id="ae6ms"></td>
  • <table id="ae6ms"><li id="ae6ms"></li></table>
  • <table id="ae6ms"></table>
    西西軟件園多重安全檢測下載網站、值得信賴的軟件下載站!
    軟件
    軟件
    文章
    搜索

    首頁編程開發java → jfinal集成spring JFinal如何配置springPlug

    jfinal集成spring JFinal如何配置springPlug

    相關軟件相關文章發表評論 來源:西西整理時間:2015/5/27 11:08:00字體大?。?em class="fontsize">A-A+

    作者:西西點擊:620次評論:0次標簽: jfinal

    jfinal demo1.9 官方最新版
    • 類型:編程控件大?。?i>24.2M語言:中文 評分:10.0
    • 標簽:
    立即下載

    jfinal 是 orm+mvc 而且有易與擴展的render plugin等機制。
    JFinal框架也整合了Spring框架,下面實現JFinal怎么去配置Spring框架。在JFinal中整合Spring使用到的類是SpringPlugin和IocInterceptor類。

    Eclipse IDE for Java EE Developers 中

    1、創建 Dynamic Web Project

    2、修改 Default Output Folder,推薦輸入 WebRoot\WEB-INF\classes

    特別注意:此處的  Default out folder 必須要與  WebRoot\WEB-INF\classes  目錄
    完全一致才可以使用  JFinal  集成的  Jetty  來啟動項目。

    3、修改 Content directory,推薦輸入 WebRoot

    注 意 : 此 處 也 可 以 使 用 默 認 值 WebContent ,   但 上 一 步 中 的
    WebRoot\WEB-INF\classes 則需要改成 WebContent\WEB-INF\classes 才能對應上。 

    4、去官網下載最新的jar包(我這是JFinal-lib-1.9)http://www.pirinnaturalssoapandspa.com/soft/130406.html

    把jetty-server-8.1.8.jar 和JFinal-bin-1.4.jar放到項目 WEB-INF\lib下,jetty-server-8.1.8.jar是開發時使用的運行環境,用tomact和生產環境下就不需要了

    5、添加到web.xml


    <filter><filter-name>jfinal</filter-name><filter-class>com.jfinal.core.JFinalFilter</filter-class><init-param><param-name>configClass</param-name><param-value>demo.DemoConfig</param-value></init-param></filter><filter-mapping><filter-name>jfinal</filter-name><url-pattern>/*</url-pattern></filter-mapping>


    6、在項目 src 目錄下創建 demo 包,并在 demo 包下創建 DemoConfig 文件,   內容如下:


    package demo;import com.jfinal.config.*;public class DemoConfig extends JFinalConfig {public void configConstant(Constants me) {
    me.setDevMode(true);
    }public void configRoute(Routes me) {
    me.add("/hello", HelloController.class);
    }public void configPlugin(Plugins me) {}public void configInterceptor(Interceptors me) {}public void configHandler(Handlers me) {}
    }


    注意:DemoConfig.java 文件所在的包以及自身文件名必須與 web.xml 中的param-value 標簽內的配置相一致(在本例中該配置為 demo.DemoConfig)。

    在 demo 包下創建 HelloController 類文件,  內容如下:


    package demo;import com.jfinal.core.Controller;public class HelloController extends Controller {public void index() {
    renderText("Hello JFinal World.");
    }
    }


    6、右擊項目名

    選中com.jfinal.core.JFinal  ok
    7、瀏覽器輸入http://localhost/hello輸出內容為 Hello JFinal World 證明項目框架搭建完成。

    注意:在 tomcat 下開發或運行項目時,需要先刪除  jetty-server-xxx.jar 這個包,否則會引起沖突。

    (抄襲官網api,罪過罪過....)

    jfinal真的挺簡單,迅速,強大的一個框架,沒有ssh的N多xml配置文件,后面做個簡單的學生信息管理,配合FreeMarker

    jfinal集成spring

    SpringIplugin類:

    SpringPlugin 是作為 JFinal 的 Plugin 而存在的,所以使用時需要在 JFinalConfig 中配置SpringPlugin,以下是 Plugin 配置示例代碼:


    @Override  public void configPlugin(Plugins me) {
    <span style="white-space:pre">		</span>//配置Spring掛件    me.add(new SpringPlugin());  }


    若創建 SpringPlugin 對 象 時 未 指 定 構 造 方 法 中 的 形 參 , SpringPlugin 將 自動去WebRoot/WEB-INF 目錄下尋找 applicationContext.xml 作為配置文件進行初始化。您還可以通過另外兩個構造方法指定配置文件或 ApplicationContext 對象。

    以前學習Spring養成了習慣將Spring的配置放在src下,這里我還是放在src的spring包中,如下:


    @Override  public void configPlugin(Plugins me) {    //配置Spring掛件, 自動找spring包中所有的xml配置文件    me.add(new SpringPlugin("classpath*:spring/*.xml"));  }


    IocInterceptor類:


    IocInterceptor 攔截 action 并對其進行依賴注入,以下是示例代碼:


    package com.tenghu.core.controller;import java.util.List;import com.jfinal.aop.Before;import com.jfinal.core.Controller;import com.jfinal.plugin.spring.Inject;import com.jfinal.plugin.spring.IocInterceptor;import com.tenghu.core.model.Users;import com.tenghu.core.service.LoginService;import com.tenghu.core.validator.LoginValidator;
    @Before(IocInterceptor.class)public class IndexController extends Controller{  @Inject.BY_NAME  private LoginService loginService;  public void index(){    List<Users> testList=Users.dao.find("select * from users");    setAttr("testList", testList);    render("login.html");  }    public void login(){    String username=getPara("name");    String password=getPara("password");    if(loginService.login(username, password)){      renderText("登錄成功");    }else{      renderText("登錄失敗");    }  }
    }


    上例將對 loginService 按屬性名稱進行注入。注解@Inject.BY_NAME 按屬性名進行注入,@Inject.BY_TYPE 按類型進行注入。不指定注入類型時將不會進行注入。

    登錄服務接口與實現類:


    package com.tenghu.core.service;public interface LoginService {  /**   * 登錄處理   */  public boolean login(String username,String password);
    }
    package com.tenghu.core.service.impl;import com.tenghu.core.service.LoginService;public class LoginServiceImpl implements LoginService{  /**   * 登錄處理   */  public boolean login(String username, String password) {    if("admin".equals(username)&&"admin".equals(password)){      return true;    }    return false;  }
    }


    Spring配置文件:


    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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"
    <span style="white-space:pre">			default-autowire="byName"</span>>  <bean id="loginService" class="com.tenghu.core.service.impl.LoginServiceImpl"/></beans>


    配置完成

    JFinal Dao 集成到 Spring

    最近公司其它部門的同事還有朋友都表示對jfinal有很大的興趣,我發現最主要的一點是jfianl極簡風格和牛x的開發效率讓大家為之興奮,尤其是jfinal的dao設計。至于沒有在新項目中進行嘗試,因為好多項目需要對事務尤其是多庫事務上進行處理,而這點也讓大家犯難了起來。公司目前的項目是基于springmvc+mybatis,所以我將jfinal dao 集成到spring上,利用spring 強大的事務抽象能力解決事務難題。

          不說了,先上代碼。。

    ?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
        <bean id="jFinalDaoConfig" class="com.jfinal.plugin.activerecord.JFinalDaoConfig" init-method="init">
            <property name="configName" value="main" />
            <property name="dataSource" ref="dataSource"/>
            <property name="dialect">
                 <bean class="com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect"/>
            </property>
            <property name="modelsClasses">
                <set>
                     <value>test.AAA</value>
                </set>
            </property>        
        </bean>

    ?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    public class JFinalDaoConfig {
        protected final Logger log = Logger.getLogger(getClass());
        public void init(){
            if (null == dialect) {
                log.warn("Using mysql dialect as default.");
                dialect = new MysqlDialect();//默認mysql方言
            }
            //config與dataSource相關綁定
            Config config = new Config(configName, dataSource, dialect);
            DbKit.addConfig(config);
            Iterator<Class<Model>> iterModel = modelsClasses.iterator();
            Class modelClass = null;
            while (iterModel.hasNext()) {
                modelClass = iterModel.next();
                Class superClass = modelClass.getSuperclass();
                if (null==superClass || superClass!=Model.class) {
                    log.warn(modelClass + " should extends com.jfinal.plugin.activerecord.Model");
                    continue;
                }
                DbKit.addModelToConfigMapping(modelClass, config);//model與config綁定
                TableBinding tb = (TableBinding) modelClass.getAnnotation(TableBinding.class);//獲取model對應的表信息
               &nbnbsp;if (tb != null) {
                    Table table = null;
                    if (StrKit.notBlank(tb.pkName())) {
                        table = new Table(tb.tableName(), tb.pkName(), modelClass);
                    } else {
                        table = new Table(tb.tableName(), modelClass);
                    }
                    tableList.add(table);
                }
            }
            if (!tableList.isEmpty()){
                TableBuilder.build(tableList, config);
            }
            Db.init();
        }
        private List<Table> tableList = new ArrayList<Table>();
        private String configName;
        private DataSource dataSource;
        private Dialect dialect;
        private Set<Class<Model>> modelsClasses;
        public void setConfigName(String configName) {
            if (configName == null) {
                throw new IllegalArgumentException("Config name can not be null");
            }
            this.configName = configName;
        }
        public void setDataSource(DataSource dataSource) {
            if (dataSource == null) {
                throw new IllegalArgumentException("DataSource can not be null");
            }
            this.dataSource = dataSource;
        }
        public void setDialect(Dialect dialect) {
            this.dialect = dialect;
        }
        public void setModelsClasses(Set<Class<Model>> modelsClasses) {
            this.modelsClasses = modelsClasses;
        }
    }

        JFinalDaoConfig的作用就是將config與數據庫綁定,模型與config進行綁定,這個類的作用我相信大家如果對jfinal比較熟悉,應該不難理解。

        jfianl Model、DbPro 的獲取和釋放連接采用了spring的DataSourceUtils進行替換

        //conn = config.getConnection();
           conn = DataSourceUtils.getConnection(config.getDataSource());

            JdbcUtils.closeStatement(pst);
            DataSourceUtils.releaseConnection(conn, config.getDataSource());

        由于jfianl某些類的可見性,JFinalDaoConfig需要放到com.jfinal.plugin.activerecord下

        這樣就可以利用spring的事務和jfinal dao的方便性了。

      相關評論

      閱讀本文后您有什么感想? 已有人給出評價!

      • 8 喜歡喜歡
      • 3 頂
      • 1 難過難過
      • 5 囧
      • 3 圍觀圍觀
      • 2 無聊無聊

      熱門評論

      最新評論

      發表評論 查看所有評論(0)

      昵稱:
      表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
      字數: 0/500 (您的評論需要經過審核才能顯示)
      女人让男人桶30分钟免费视频,女人张开腿让男人桶个爽,一进一出又大又粗爽视频
    • <td id="ae6ms"><li id="ae6ms"></li></td>
    • <xmp id="ae6ms"><td id="ae6ms"></td><table id="ae6ms"></table>
    • <table id="ae6ms"></table>
    • <td id="ae6ms"></td>
      <td id="ae6ms"></td>
    • <table id="ae6ms"></table><table id="ae6ms"><td id="ae6ms"></td></table>
    • <td id="ae6ms"></td>
    • <table id="ae6ms"><li id="ae6ms"></li></table>
    • <table id="ae6ms"></table>