spring-jfinal用于整合 spring和jfinal。
依赖jarjfinal、javassistv3.*、Springv3+(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、JFinalConfigpublicclassCommonConfigextendsJFinalConfig{@AutowiredpublicvoidsetApplicationContext(ApplicationContextctx){System.out.println("HelloControllerattr[ApplicationContext]已注入。。");}...}其中HelloJFinalConfig自动注入springbean,scope="singleton".
3、ControllerpublicclassHelloControllerextendsController{@AutowiredpublicvoidsetApplicationContext(ApplicationContextctx){System.out.println("HelloControllerattr[ApplicationContext]已注入。。");}}其中Controller自动注入springbean,scope="prototype"原因是jfinal也是每次请求创建实例.
4、spring事务管理<beanid="druidDataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="username"value="${db.userName}"/><propertyname="password"value="${db.passWord}"/><propertyname="url"value="${db.jdbcUrl}"/><propertyname="driverClassName"value="${db.driverClassName}"/><propertyname="initialSize"value="${db.initialSize}"/><propertyname="maxActive"value="${db.maxActive}"/><propertyname="minIdle"value="${db.minIdle}"/></bean><!--spring事务管理,ActiveRecordPlugin可以获得此dataSource可以把事务交给spring管理--><beanid="dataSourceProxy"class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"><propertyname="targetDataSource"ref="druidDataSource"/></bean><!--================================事务相关控制=================================================--><beanname="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSourceProxy"></property></bean><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="delete*"propagation="REQUIRED"read-only="false"/><tx:methodname="insert*"propagation="REQUIRED"read-only="false"/><tx:methodname="update*"propagation="REQUIRED"read-only="false"/><tx:methodname="find*"propagation="SUPPORTS"/><tx:methodname="get*"propagation="SUPPORTS"/><tx:methodname="select*"propagation="SUPPORTS"/></tx:attributes></tx:advice><!--把事务控制在Service层--><aop:config><aop:pointcutid="pc"expression="execution(public*demo.service.*.*(..))"/><aop:advisorpointcut-ref="pc"advice-ref="txAdvice"/></aop:config>以上是xml配置方式。重点是在ActiveRecordPlugin中注入代理数据源。
5、jetty启动.publicstaticvoidmain(String[]args){CtFactory.toClass();//这个必须启动前执行//...}
评论