24 Star 56 Fork 24

ylx252 / spring-jfinal

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

#spring-jfinal 作用于 spring 和 jfinal(理论上支持所有版本) 的整合。

#依赖 jar jfinal、javassist v3.*、Spring v3+(3.2.0之前需要依赖cglib)

#使用 Maven

	<dependency>
	  <groupId>cc.ecore</groupId>
	  <artifactId>spring-jfinal-plugin</artifactId>
	  <version>x.x.x</version>
	</dependency>

1、web.xml

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/spring.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    <filter>
        <filter-name>jfinal</filter-name>
    	<filter-class>com.jfinal.core.SpringJFinalFilter</filter-class>
    	<init-param>
    		<param-name>configClass</param-name>
    		<param-value>com.demo.test.CommonConfig</param-value>
    	</init-param>
    </filter>
    <filter-mapping>
    	<filter-name>jfinal</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>

以上通过 SpringJFinalFilter 过滤器加载 spring 。 其中 JfinalConfig 自动注册 springbean(JfinalConfig 中可以使用 注解注入属性哦,或者 实现 ApplicationContextAware 接口自动注入 ApplicationContext)。

初始化顺序(不配置 ContextLoaderListener 的情况下): SpringJFinalFilter -> ApplicationContext -> JFinal -> JFinalConfig ... (其他 照旧 ) . 初始化顺序(配置 ContextLoaderListener 的情况下)。 ApplicationContext -> SpringJFinalFilter -> JFinal -> JFinalConfig ... (其他 照旧 ) .

2、JFinalConfig

	public class HelloJFinalConfig extends JFinalConfig {
	
		@Autowired
		public void setApplicationContext(ApplicationContext ctx) {
			System.out.println("HelloController attr[ApplicationContext] 已注入。。");
		}
		...
	}

其中 HelloJFinalConfig 自动注入 springbean , scope = "singleton" .

3、Controller

	public class HelloController extends Controller {
	
		@Autowired
		public void setApplicationContext(ApplicationContext ctx) {
			System.out.println("HelloController attr[ApplicationContext] 已注入。。");
		}
	}

其中 Controller 自动注入 springbean , scope = "prototype" 原因是 jfinal 也是每次请求创建实例 .

4、spring 事物管理

    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="username" value="${db.userName}" />
		<property name="password" value="${db.passWord}" />
		<property name="url" value="${db.jdbcUrl}" />
		<property name="driverClassName" value="${db.driverClassName}" />
		<property name="initialSize" value="${db.initialSize}" />
		<property name="maxActive" value="${db.maxActive}" />
		<property name="minIdle" value="${db.minIdle}" />
	</bean>
	<!-- spring 事物管理 ,ActiveRecordPlugin可以获得此 dataSource 可以把事务交给spring 管理 -->
	<bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
		<property name="targetDataSource" ref="druidDataSource" />
	</bean>

	<!-- ================================事务相关控制================================================= -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSourceProxy"></property>
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
			<tx:method name="insert*" propagation="REQUIRED" read-only="false" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" />

			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="select*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>

	<!-- 把事务控制在Service层 -->
	<aop:config>
		<aop:pointcut id="pc" expression="execution(public * demo.service.*.*(..))" />
		<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
	</aop:config>

以上是 xml 配置方式。 重点是在 ActiveRecordPlugin 中注入 代理数据源。

5、jetty 启动.

	public static void main(String[] args) {
		CtFactory.toClass();// 这个必须启动前执行
		// ...
	}

6、更多支持

The MIT License (MIT) Copyright (c) 2016-2016 d3leaf@126.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

用于整合 spring + jfinal . 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/ylx252/spring-jfinal.git
git@gitee.com:ylx252/spring-jfinal.git
ylx252
spring-jfinal
spring-jfinal
master

搜索帮助