⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spring2.txt

📁 Java大部分的基础知识,EJB3,EJB2,WEBSERVICE,SOAP,JMS,MQ,还有些面试题
💻 TXT
字号:
十、spring最有价值的两个功能?
	ioc(inverse of control)/di:控制返转/依赖注入
		将类与类的依赖关系写在配置文件中,程序在运行时
		根据配置文件动态加载所依赖的类,让类与类之间的依赖解藕合。
		强藕合:
			List al=new ArrayList();
		弱一点的藕合:
			IFacade facade=FacadeFactory.getFacade();
		很弱的藕合:
			spring的ioc,将依赖的类写在配置文件中,
			程序在运行的过程中动态加载,如果要换成另一个类
			只需修改配置文件,客户端及工厂都不用改。
十一、spring的三种注入方式?
	set
	构造子
	接口
十二、spring的核心配置文件,请写一个典型的配置?
	--核心配置文件是appliationContext.xml
	<beans>
		<bean id="customer" class="vo.Customer" init-method="init" destroy-method="destroy">
			<property name="customerName" value="张三">
			<property name="sex">
				<null/>
			</property>
			<property name="address">
				<ref local="address"/>
			</property>
		</bean>
		<bean id="address" class="vo.Address">
			<property name="state" value="广东">
			</property
		</bean>		
		<bean id="customer1" class="vo.Customer" init-method="init" destroy-method="destroy">
			<constructor-args>
				<value>张三</value>
			</constructor-args>
			<constructor-args>
				<value>男</value>
			</constructor-args>
			<constructor-args>
				<ref local="address"/>
			</constructor-args>
		</bean>
	</beans>
	--核心类
		ApplicationContext ac=new XMLFileApplicationContextSupport("applicationContext.xml");
		Object customer=ac.getBean("customer");
三、加载多个applicationContext.xml文件
	在web.xml文件中加入如下内容:
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml,classpath*:dao/applicationContext-*.xml,classpath*:applicationContext.xml</param-value>
	<context-param>

action中overode Action的setServlet获得spring的context对象,从而使用spring特性
public void setServlet(ActionServlet actionServlet) {    
        try {    
            super.setServlet(actionServlet);    
            ServletContext servletContext = actionServlet.getServletContext();    
            context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        } catch(Exception e) {    
            e.printStackTrace();    
        }    
    }   

-------------------------struts2-------------------------
		struts中可以有多个配置文件,一般是一个功能模板一个,不同团队不影响,实现如下:
	1、在/WEB-INF/下加入新的struts-config.xml文件
	2、在web.xml中在ActionServlet的配置中说明具体的struts-config.xml文件所在位置
		多个文件用,分隔开。
		<init-param>
		      <param-name>config</param-name>
		      <param-value>/WEB-INF/struts-config-1.xml,
		      	/WEB-INF/struts-config-2.xml</param-value>
		   </init-param>

二、什么是aop?aop相关的名词是什么?
	aop是面向方面编程,是对oop的补充,oop主要是纵向的业务,aop主要
	应用横向的服务,主事务、日志、安全,不可以替代oop,只是补充。
	aop相关的名词:
		拦截器(代理):向客户端公开通过它找到真实对象。
		真实对象:做具体的处理。
		装备:代理调用服务类如TransactionManager
		关切点:为那些方法提供什么样的服务,如是否需要的事务。
		连接点:连接的是方法还是属性,一般是方法。

三、spring&hibernate的配置过程?

	dataSource
	sessionFactory
		dataSource
	userDao
		sessionFactory
	realFacade
		userDao
	transactionManager
		sessionFactory
	abstractProxy	TransactionProxyFactoryBean:产生代理
		transactionManager:引入装备
		transactionAttributes:什么方法需要什么样的事务
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
			<props>
	facade  parent="abstractProxy"
		target=realFacade
四、代理模式
	有三个角色,分别是通用接口、真实对象、代理,真实对象、代理
	实现的同一个接口,将真实对象作为代理的一个属性,向客户端公开的
	代理,客户端调用代理的一方法时,代到调用真实对象的方法,在调用真实
	对象的方法之间之后可以做一些服务。
五、什么是aop
	aop不是oop,aop是面向方面编程,是对oop的一个补充
	oop是纵向的业务处理,aop是横向的服务,如事务、安全、
	日志
六、aop实现的原理及名词
	aop实现的原理就是代理模式
	名词:
		拦载器:代理
		真实对象:本原
		装备:做具体的服务
		关切点:哪些方法需要什么样的服务
		连接点:拦截的是方法还是属性,一般是方法。
七、事务的实现
	声明式:什么方法需要什么样的事务写在配置文件中,
		如spring的aop、ejb2、ejb3
	代码式:手动的写代码进行事务管理
		con.setAutoCommit(false);
		con.commit();
		con.rollback();
		
		Transaction trans=session.beginTransaction();
		trans.rollback();
		trans.commit();
八、struts与spring组合
	1、在struts中加入一插件加载spring的配置文件applicationContext.xml
	2、struts-config.xml中的action标签内的type属性不再指向具体的action
	    而是DelegatingActionProxy(代表action的代理),通过该代理进入spring
	    环境。
	3、在spring中加入bean标签,说明具体的action
		<bean name="/insert" class="action.InsertAction">
			<property name="facade">
				<ref local="facade(是代理,只有代理才可以提供声明式的事务服务)"/>
			</property>
		</bean>
三、spring&struts的组合?
	1、在struts-config.xml加入一个插件加载applicationContext.xml文件
		类是:ContextLoaderPlugIn
		参数:
			contextConfigLocation=/WEB-INF/applicationContext.xml
	2、修改struts-config.xml中的action标签的type属性,其不再指向具体
	    Action,而是DelegatingActionProxy
	3、在applicationContext.xml中配置具体的action
		<bean name="/login" type="struts.action.LoginAction"/>
八、spring2的事务配置(三种方式)
	(一)通过xml配置
		1、加入tx及aop命名空间
		2、通过tx说明关切点即那些方法需要什么样的服务及装备
			<tx:advice id="facadeTransactonMethod" transaction-manager="transactionManager">
				<tx:attributes>
					<tx:method name="select*" read-only="true"/>
					<tx:method name="*"/>
				</tx:attributes>
			</tx:advice>
		3、通过aop前缀说明被拦截的类,将拦截类与tx整合在一起。
			<aop:config>
				<aop:pointcut id="facadeClass" expression="execution(* facade.*.*(..))"/>
				<aop:advisor advice-ref="facadeTransactonMethod" pointcut-ref="facadeClass"/>
			</aop:config>
	(二)通过注解配置:
		在xml中加入如下标记,支持注解的事务处理
			<tx:annotation-driven/>
		在代码中加入注解,说明具体的事务
			需要事务处理
				@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
			不需要事务处理

	spring中的事务声明,因xml格式说明用了schema,所以支持命名空间,使用了AspectJ语法
	<!--说明事务策略,说明拦截那些方法-->
		<tx:advice id="facadeTransactonMethod" transaction-manager="transactionManager">
			<tx:attributes>
				<tx:method name="select*" read-only="true"/>
				<tx:method name="*"/>
			</tx:attributes>
		</tx:advice>
	<!--拦截那些类-->
		<aop:config>
			<!--关切那些类-->
			<aop:pointcut id="facadeClass" expression="execution(* facade.*.*(..))"/>
			<aop:advisor advice-ref="facadeTransactonMethod" pointcut-ref="facadeClass"/>
		</aop:config>
	<!--启动容器对注解型事务管理功能的支持,必须加,如果没有,事务处理注解不起作用-->
			<tx:annotation-driven/>
		具体注解内容:
			需要事务处理
				@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
			不需要事务处理
				@Transactional(readOnly=true)				@Transactional(readOnly=true)
	(三)applicationContext.xml

	<bean id="customerDao" class="dao.impl.HibernateCustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>	
	<bean id="buyDao" class="dao.impl.HibernateBuyDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="realFacade" class="facade.Facade">
		<property name="buyDao" ref="buyDao"></property>
		<property name="customerDao" ref="customerDao"></property>
	</bean>
	
	<!-- 配置通用的代理-->
	<bean id="abstractProxy" abstract="true" 						class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
	<!-- 配置具体的facade代理 -->
	<bean id="facade" parent="abstractProxy">
		<property name="target" ref="realFacade"></property>
	</bean>
	<!--通过struts进行处理-->
	<bean name="/insert" class="struts.action.InsertAction">
		<property name="facade" ref="facade"></property>
	</bean>
spring2的两种事务处理
	1、通过xml文件说明事务的规则。
		--说明装备及拦截那些方法给什么服务
		<tx:advice  id="methodAdvice" transaction-manager="transactionManager">
			<tx:attribute>
				<tx:method name="select*" read-only="true">
				<tx:method name="*"/>
			</tx:attribute>
		</tx:advice>
		--说明拦截那些类
		<aop:config>
			<aop:pointcut id="classPointcut" expression="execution (* facade.*.*(..))"/>
			<aop:advisor advice-ref="methodAdvice" pointcut-ref="classPointcut"/>
		</aop:config>
	2、通过注解说明事务的规则
		1、让spring支持注解
			<annotation-driven/>
		2、在具体方法前加入注解说明需要什么样的事务

八、spring2的事务配置
	1、加入tx及aop命名空间
	2、通过tx说明关切点即那些方法需要什么样的服务及装备
		<tx:advice id="facadeTransactonMethod" transaction-manager="transactionManager">
			<tx:attributes>
				<tx:method name="select*" read-only="true"/>
				<tx:method name="*"/>
			</tx:attributes>
		</tx:advice>
	3、通过aop前缀说明被拦截的类,将拦截类与tx整合在一起。
		<aop:config>
			<aop:pointcut id="facadeClass" expression="execution(* facade.*.*(..))"/>
			<aop:advisor advice-ref="facadeTransactonMethod" pointcut-ref="facadeClass"/>
		</aop:config>

	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -