一 Spring中有一个IOC对象容器(Spring内部的一个HashMap容器),用于盛放对象的!
Spring中全部生成的对象都放在IOC对象容器中的!是以name或id的值做键存在容器中的!
给对象注入值的方式:---以下的4种全部都是交给Spring框架去注入的!
1.
2.
3.
4.
5.
<ref bean="p3"/>---表示可以引用其他Xml中的Bean;
<ref local="a"/>---local表示只能引用本Xml中的Bean;
Spring中加载上下文的三种方式:
ApplicationContext act=new ClassPathXmlApplicationC
Resource resource=new ClassPathResource("application.xml");
ApplicationContext
(“F:/绝对路径/application.xml”)
二 Spring配置工厂Bean
调用getBean(“工厂bean的id”)方法,Spring返回的不是直接创建的工厂Bean的实例,而是由工厂Bean创建的Bean实例(工厂bean生产的产品实例).
三 Spring的IOC容器用于管理Struts和Hibernate
注意:在使用Spring的时候,由Hibernate生成的影射文件中要取消指定的数据库名,否则查询数据库是要报错!
----------整合部分!
四 Struts1.2+Spring1.2+Hibernate3.1的整合(重点)
1.
Struts-config.xml文件都是自动生成的!
name="user" path="/user.jsp"></forward>
</action-mappings>
</struts-config>
2.
<beans>
<!--session工厂-->
使用的事务和事务的管理都是用的Hibernate里面的东西,都在Spring的.orm.hibernate3的下面
<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
name="configLocation">
</bean>
<!--事务管理-->
<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionMana
<property name="sessionFactory" ref="sf"></property>
</bean>
配置的继承
下面的配置可以使用配置继承的方法来减少Spring的配置泛滥
先写个基础的代理,不指定target的代理目标,并且告诉Spring是抽象的不能使用,abstract=”true”!
当建对于具体的目标的Bean的时候,继承上面的基础代理就可以了!parent=”baseProxy”
再把target指定代理目标!
<!--servicei代理-->这就是代理,可以在其中加工处理的!
用事务拦截机的代理工厂Bean,针对业务层进行事务的监控--
<bean id="basePoxy"
name="transactionManager" ref="transactionManager"></property>
name="transactionAttributes">
key="insert*">PROPAGATION_REQUIRED</prop>
key="delete*">PROPAGATION_REQUIRED</prop>
key="update*">PROPAGATION_REQUIRED</prop>
key="save*">PROPAGATION_REQUIRED</prop>
key="merge*">PROPAGATION_REQUIRED</prop>
key="*">PROPAGATION_REQUIRED,readOnly</prop>
</bean>
针对业务层进行事务的监控
<bean id="userserviceproxy"
name="target" ref="userservicei"></property>
</bean>
<!--daoimpl-->要使用SessionFactory查询持久层!
<bean id="userdaoi" class="org.wllt.www.daoi.UserDaoi">
</bean>
<!--serviceimpl-->要使用Dao接口的数据
<bean id="userservicei" class="org.wllt.www.servicei.UserServicei">
</bean>
<!--Action-->要使用业务层的数据
<bean name="/user" class="org.wllt.www.action.UserAction" singleton=”true”>--默认就是true
但是不能直接来自于Servicei,因为在业务层需要事务,数据库才会生效!只有先用代理处理!
</bean>
</beans>
3.Web.xml文件
Struts1.2的配置文件自己会加载的,但是Spring则要我们自己来写配置!
---上下文的加载Spring的配置文件!
<context-param>