- 浏览: 1089196 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
skyesx:
这是2PC实现,更常用的是一个柔性事务的实现,可以参考http ...
Spring分布式事务实现 -
ddbird:
这第一句就不严谨“分布式事务是指操作多个数据库之间的事务”,显 ...
Spring分布式事务实现 -
呵呵6666:
基于互联网支付系统的微服务架构分布式事务解决方案http:// ...
Spring分布式事务实现 -
小黄牛:
写得不错,交流群:472213887
Spring分布式事务实现 -
jiaoqf321456:
这明明是用的apache的压缩,给ant.jar有半毛钱关系吗 ...
使用ant.jar进行文件zip压缩
1.web.xml中载入spring配置
或者
配置完成之后,即可通过WebApplicationContextUtils.getWebApplicationContext
方法在Web应用中获取ApplicationContext引用。
为了在Struts中加载Spring Context,在struts-config.xml中增加如下部分:
2.关于hbm.xml文件的载入
通常在spring中会这么写代码:
如果X.hbm.xml文件很多时,则写起来会很不方便,可以像下面这种写法就简单多了:(其中假设所有的.hbm.xml文件都存于com/model目录)
首先,如果使用mysql,确定mysql为InnoDB类型。
事务管理的控制应该放到商业逻辑层。你可以写个处理商业逻辑的JavaBean,在该JavaBean中调用DAO,然后把该Bean的方法纳入spring的事务管理。
com.yz.spring.service.implement.UserManageImpl就是我们的实现商业逻辑的JavaBean。我们通过parent元素声明其事务支持。
4.加载多个xml配置文件,生成ApplicationContext实例
(1)ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext1.xml","applicationContext2.xml"});
BeanFactory factory=(BeanFactory)context;
(2)<beans> <import resource="conf/jms-applicationContext.xml"/></beans>
5.给bean指定别名
如:通过不同的名字来引用同一个数据源
6.JNDI定位DataSource(通常由应用程序服务器管理)
7.加载属性文件
8.在JSP里调用spring管理的bean取得数据
9.通过BeanFactoryAware从Spring容器中取Bean
10.使用ApplicationContextAware让Spring容器传递ApplicationContext
11.在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
web.xml中的加载顺序为:listener >> filter >> servlet >> spring。其中filter的执行顺序是filter- mapping在web.xml中出现的先后顺序。
加载顺序会影响对spring bean的调用。比如filter 需要用到bean ,但是加载顺序是先加载filter 后加载spring,则filter中初始化操作中的bean为null。所以,如果过滤器中要使用到 bean,可以将spring 的加载改成Listener的方式。
在servlet或者filter或者Listener中使用spring的IOC容器的方法是:
由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
附参考资料:
http://www.family168.com/oa/tech/spring.html
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
或者
<servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>
配置完成之后,即可通过WebApplicationContextUtils.getWebApplicationContext
方法在Web应用中获取ApplicationContext引用。
为了在Struts中加载Spring Context,在struts-config.xml中增加如下部分:
<struts-config> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /> </plug-in> </struts-config>
2.关于hbm.xml文件的载入
通常在spring中会这么写代码:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="mappingResources"> <list> <value>Student.hbm.xml</value> <value>Course.hbm.xml</value> … </list> </property> </bean>
如果X.hbm.xml文件很多时,则写起来会很不方便,可以像下面这种写法就简单多了:(其中假设所有的.hbm.xml文件都存于com/model目录)
<bean id="sessionFactory" class="org.springframework. orm.hibernate.LocalSessionFactoryBean"> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">100</prop> </props> </property> </bean>3.Spring中实现事务管理
首先,如果使用mysql,确定mysql为InnoDB类型。
事务管理的控制应该放到商业逻辑层。你可以写个处理商业逻辑的JavaBean,在该JavaBean中调用DAO,然后把该Bean的方法纳入spring的事务管理。
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="userManage" parent="txProxyTemplate"> <property name="target"> <bean class="com.yz.spring.service.implement.UserManageImpl"> <property name="userDAO"> <ref bean="userDAO"/> </property> </bean> </property> </bean>
com.yz.spring.service.implement.UserManageImpl就是我们的实现商业逻辑的JavaBean。我们通过parent元素声明其事务支持。
4.加载多个xml配置文件,生成ApplicationContext实例
(1)ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext1.xml","applicationContext2.xml"});
BeanFactory factory=(BeanFactory)context;
(2)<beans> <import resource="conf/jms-applicationContext.xml"/></beans>
5.给bean指定别名
如:通过不同的名字来引用同一个数据源
<alias name="dataSource" alias="componentA-dataSource"/> <alias name="dataSource" alias="componentB-dataSource"/>
6.JNDI定位DataSource(通常由应用程序服务器管理)
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/myds</value> </property> </bean>
7.加载属性文件
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc-config.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc_driver}" p:url="${jdbc_url}" p:username="${username}" p:password="${password}"/>
8.在JSP里调用spring管理的bean取得数据
<%@ page import="org.springframework.context.ApplicationContext"%> <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%> <%@ page import="com.yourcompany.service.CategoryService"%> <% //applicationContext.xml中一定要有完整的依赖链,从dataSource到CategoryService ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); CategoryService cs = (CategoryService) ctx.getBean("CategoryService"); List list =cs.getCategoryDAO().findAll(); %>
9.通过BeanFactoryAware从Spring容器中取Bean
public final class BeanFactoryHelper implements BeanFactoryAware { private static BeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory factory) throws BeansException { beanFactory = factory; } public BeanFactory getBeanFactory() { return beanFactory; } public static Object getBean(String name) { return beanFactory.getBean(name); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object getBean(String name, Class clazz) { return beanFactory.getBean(name, clazz); } }
<bean id="beanFactoryHelper" class="net.demo.spring3.util.BeanFactoryHelper"></bean>
10.使用ApplicationContextAware让Spring容器传递ApplicationContext
public final class ApplicationContextHelper implements ApplicationContextAware{ private static ApplicationContext appCtx; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { appCtx = applicationContext; } public static ApplicationContext getApplicationContext() { return appCtx; } public static Object getBean(String beanName ) { return appCtx.getBean(beanName); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object getBean(String name, Class requiredType) throws BeansException { return appCtx.getBean(name, requiredType); } public static boolean containsBean(String name) { return appCtx.containsBean(name); } public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return appCtx.isSingleton(name); } }
<bean id="springApplicationHelper" class="net.demo.spring3.util.ApplicationContextHelper"></bean>
11.在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
web.xml中的加载顺序为:listener >> filter >> servlet >> spring。其中filter的执行顺序是filter- mapping在web.xml中出现的先后顺序。
加载顺序会影响对spring bean的调用。比如filter 需要用到bean ,但是加载顺序是先加载filter 后加载spring,则filter中初始化操作中的bean为null。所以,如果过滤器中要使用到 bean,可以将spring 的加载改成Listener的方式。
在servlet或者filter或者Listener中使用spring的IOC容器的方法是:
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
附参考资料:
http://www.family168.com/oa/tech/spring.html
发表评论
-
SpringBoot开发WebService之Axis
2019-07-14 23:56 4646一、服务器端发布WebService服务 1、POM.xml文 ... -
SpringBoot开发WebService之CXF
2019-07-14 23:56 1210一、在服务器端的WebSerivce服务发布 1、POM.xm ... -
SpringBoot项目非web方式启动
2019-07-03 17:02 46881、springboot 1.x中以非web方式启动 @S ... -
SpringBoot使用Druid数据库密码加密
2019-03-06 23:28 14341、生成公钥、私钥和密码加密串 java -cp drui ... -
Spring Annotation
2010-12-02 17:14 0Spring2.x引入了组件自动扫描机制,可以在类路径底 ... -
Spring分布式事务实现
2010-11-10 14:28 83138分布式事务是指操作多个数据库之间的事务,spring的 ... -
Spring3 Annotation + Hibernate3-jpa2.0 + CGLIB + 多数据源
2010-08-19 09:30 10482一、定义一个测试用Entity。 @Entity pub ... -
使用iBatis2.0
2010-05-26 10:20 0一、NULL问题 ibatis操作oracle数据库时, ... -
使用AspectJ LTW(Load Time Weaving)
2010-01-04 14:25 10721在Java 语言中,从 ... -
Spring2.0 AOP AspectJ 注释实现
2010-01-04 14:24 5540一、AOP基本概念 切面(Aspect): 一个关注点的模块 ... -
Spring + JPA + Hibernate配置
2010-01-04 14:24 34696<1>persistence.xml放到类路径下的 ... -
配置spring数据源
2009-11-06 16:47 1215配置一个数据源 Spring在第三方依赖包中包含了两 ... -
hibernate的dialect
2009-07-23 10:04 5407一、hibernate的dialect RDBM ... -
spring ibatis入门
2009-04-20 14:16 3842一、applicationContext.xml <?x ... -
Hibernate缓存配置/批量处理
2009-03-25 21:50 10911Hibernate除了自动对Se ... -
Hibernate的一级与二级缓存
2009-03-25 21:24 1676缓存是介于应用程序和物理数据源之间,其作用是为了降低应用 ... -
spring jdbcTemplate使用
2008-07-15 17:17 78163一、使用示例 (1)springJdbcContext.xml ... -
Spring2.X以AspectJ 式AOP 配置事务
2008-07-10 13:23 2035(1)配置: Spring的事务管理是通过AOP代理实 ... -
spring 事务管理
2008-07-08 16:35 11924声明式的事务管理(Declarative transactio ... -
Hibernate中one-to-many/many-to-one和many-to-many
2008-06-28 17:03 3937<1>one-to-many/many-to-on ...
相关推荐
spring配置 spring配置 spring配置 spring配置 spring配置
ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssm...
spring xml配置的12個技巧 spring xml配置的12個技巧 spring xml配置的12個技巧
Spring配置 Spring 配置 映射 加注释!!!!Spring配置 Spring 配置 映射 加注释!!!!
spring相关配置资料,几乎在里面。包括spring-aop-3.1等文件。
spring事务配置详解 spring事务配置详解
spring配置文件实例
NULL 博文链接:https://a-bin.iteye.com/blog/1006301
Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合
Spring的基本配置Spring的基本配置Spring的基本配置Spring的基本配置Spring的基本配置Spring的基本配置Spring的基本配置
Spring Cloud配置中心获取不到最新配置信息的问题
spring数据源配置
使用Spring配置文件实现AOP
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
spring 详细配置spring 详细配置 spring 详细配置spring 详细配置
SpringCloud——分布式配置中心(Spring Cloud Config)之高可用的分布式配置中心
spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件...
spring cloud2.0 eureka server spring security配置,与spring cloud1.X还是有很大区别
spring配置和启动方式 博客地址:https://blog.csdn.net/u010476739/article/details/76696756
spring MVC配置详解