java技术 spring 配置
spring 的IOC是反射注入,用来管理对象的创建与销毁。一般使用都是在启动的web服务器的时候就创建了对象,可以选择自动装配对象管理,将对象引用实现与引用分开。采用的xml配置方式。及大减少了各个类的代码。
spring IOC实际配置
<beans default-autowire="byName"> <bean id="userDao" class="org.springside.modules.security.dao.UserDao" /> <bean id="deptDao" class="org.springside.modules.security.dao.DeptDao" /> <bean id="roleDao" class="org.springside.modules.security.dao.RoleDao" /> <bean id="resourceDao" class="org.springside.modules.security.dao.ResourceDao" /> <bean id="permissionDao" class="org.springside.modules.security.dao.PermissionDao" /> </beans>
属性注入 default-autowire="byName" 这个显示自动装配
public class RoleDao extends BaseHibernateDao { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; }
spring是管理容器,负责创建创建对象,创建对象如果没有spring容器,创建的时候就是能用的时候就直接到类里面创建,如果有了spring容器,那么创建对象就是spring的事情了,这样有什么好处呢,就是能够解耦,这应该就是控制吧,就好比 (剧本 角色 饰演者)剧本需要角色,角色的实际对象是饰演者,剧本就会创建饰演者满足角色,这样就是以剧本引入角色。
public class MoAttack { public void drama() { //打演员扮演剧本角色 Role role= new performer(); //剧本角色剧情展开 role.responseAsk("墨者革离!"); } }
这样就是以剧本引入角色没有spring管理的。这样有什么问题呢,真正的饰演者也写到了剧本里面,
实际情况下应该是由导演来指定应该让谁演的。导演其实应该就是spring管理容器,这样能够将角色与饰演者分开,这就是解耦。
构造函数注入
public class MoAttack { private Role role; //①注入具体角色扮演者 public MoAttack(Role role){ this.role= role; } public void cityGateAsk(){ role.responseAsk("墨者革离!"); } }
剧本这样呢就不关心到底是扮演这个角色,只需要你传入进来真正的饰演者就可以了。有谁来传入呢,由导演来指定谁扮演
注:借鉴
public class Director { public void direct(){ //①指定角色的扮演者 德华 Role role= new LiuDeHua(); //②注入具体扮演者到剧本中 MoAttack moAttack = new MoAttack(geli); moAttack.cityGateAsk(); } }
spring 基础配置
web.xml文件
firstListener就是用来加载applicationContext.xml的监听器
spring 解决hibernate关闭及延迟的问题OpenSessionInViewFilter是将一次完成的请求与对应的线程绑定,他允许在事物提交之后加载显示所需要的对象。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:context/applicationContext.xml </param-value> </context-param> <context-param> <param-name>firstListener</param-name> <param-value>org.springframework.web.context.ContextLoaderListener</param-value> </context-param> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter>
classpath:context/applicationContext.xml
sessionFactory(依赖 dataSource mappingDirectoryLocations hibernateProperties ) 与下面代码对应
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:domain/hbm/</value>
</list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.connection.SetBigStringTryClob">${hibernate.connection.SetBigStringTryClob}</prop> </props> </property> </bean>
*****************************************************************************************
dataSource org.apache.commons.dbcp.BasicDataSource (依赖driverClassName url username password )与下面对应
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName1}</value> </property> <property name="url"> <value>${jdbc.url1}</value> </property> <property name="username"> <value>${jdbc.username1}</value> </property> <property name="password"> <value>${jdbc.password1}</value> </property> </bean>
(driverClassName url username password) (依赖根目录下jdbc.properties 配置)注:项目工程一般都直接配置在tomcat context.xml 或者server.xml里面
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean>
jdbc.properties配置文件,是通过inputStream流将配置属性读入加载
jndi.default_name=
hibernate.connection.SetBigStringTryClob=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.format_sql=false
hibernate.hbm2ddl.auto=
hibernate.show_sql=false
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=
jdbc.username=
jdbc.password=
jdbc.driverClassName1=oracle.jdbc.OracleDriver
jdbc.url1=
jdbc.username1=
jdbc.password1=
************************************************************************************************
----------------------------------------------------------------------------------------
mappingDirectoryLocations hibernate映射的目录
<property name="mappingDirectoryLocations"> <list> <value>classpath:domain/hbm/</value> <value>classpath:modules/</value> </list> </property>
-----------------------------------------------------------------------------------
hibernateProperties
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.connection.SetBigStringTryClob">${hibernate.connection.SetBigStringTryClob}</prop> </props> </property>
${hibernate.dialect}依赖于jdbc.properties里面配置 自上而下jdbc.properties配置文件,是通过inputStream流将配置属性读入加载。