spring
2015-07-17 09:15
746 查看
IOC控制反转
意思是将你设计的类交给系统(指的是spring容器)去控制,而不是在你自己的类内部控制通过spring容器对类进行初始化,实例化,调用,销毁
如果没有引入spring容器,你所设计的类与类间存在依赖关系,通过类与类,对象与对象间的互相合作,完成所要完成的业务逻辑
通过引入spring ioc容器,就像给各个类间加上了一个第三方,类与类间,对象与对象间失去了依赖,软件系统中的耦合度大大降低
Spring IoC使用方法:
—如果UserService调用UserDAO—
a.在UserService实现类中,添加一个UserDAO接口类型属性及setter方法.
b.将UserService和UserDAO都交给Spring容器管理
c.在UserService的定义中使用元素指定调用关系.
定义格式如下
<property name="属性名" ref="容器中bean组件id值">
DI依赖注入
DI是实现IoC控制的主要技术.例如有类A与类B
在B中需要调用类A
原来我们在没有学习spring依赖注入时的做法是
在类B中new一个类A
在了解spring依赖注入之前,我们首先要了解什么是依赖注入,简单的来说,依赖注入就是在你需要的时候,spring为你提供你所需要的,而无需你自己去实例化你所需要的
注入的两种方法:
a.setter方式注入(推荐使用) 设值注入
b.构造方式注入
注入的使用
使用时需要注入各种类型数据,主要有以下类型
a.注入Bean对象
b.注入基本类型,例如字符串,数值,日期等
c.注入集合类型,例如List,Set,Map,Properties
如果设值注入与构造注入同时存在,最后我们看到的将是设值注入的结果,因为在实例化一个对象时,总会先执行构造方法,再初始化成员变量。所以我们最后看到的会是设值注入的结果
AOP面向切面编程
代理模式,spring AOP 代理管理事务
spring 是贯穿表现层,业务层,实体层的多层的j2ee应用框架
spring的配置文件applicationContext.xml的配置
1.放在src目录底下
参考applicationContext.xml格式配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- DriverMangerDataSource --> <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 所用数据库驱动 --> <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property> <!--所用数据库路径 --> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property> <!-- 所用数据库账号密码 --> <property name="username" value="wsx"></property> <property name="password" value="wsx"></property> </bean> <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop> key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect </prop> <prop> key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext </prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:com/bean/*.hbm.xml</value> </list> </property> </bean> <!-- 事务管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 事务通知 --> <tx:advice id="basicAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- spring AOP 代理管理事务 --> <aop:config proxy-target-class="true"> <aop:pointcut id="basicPoint" expression="execution(* com.service.*.*(..))" /> <aop:advisor advice-ref="basicAdvice" pointcut-ref="basicPoint" /> <bean name="dictionaryIFC" class="com.service.impl.DictionaryService"> <property name="dataSource" ref="dataSource"></property> </bean> <bean name="noticeService" class="com.service.NoticeService"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="mailService" class="com.service.MailService"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="surveyService" class="com.service.SurveyService"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
相关文章推荐
- java中如何导入03和07版的Excel文件数据,存入到数据库
- Java的内存分析
- java--实现将文字生成二维码图片,并在中间附上logo,下方附上文字
- Java回调函数机制
- 32位64位Eclipse和jdk对应关系
- 解决eclipse启动插件时PermGen space问题
- java.util.concurrent
- java 枚举常见用法
- Eclipse开发Android,Java项目快捷键技巧
- 解决eclipse svn 返回历史版本(update to version)异常
- java接口与抽象类的区别之缺省适配模式 .
- eclipse maven plugin 插件 安装 和 配置
- 在 Java EE 组件中使用 Camel Routes
- 除非万不得已,别 Catch!
- 透过byte数组简单分析Java序列化、Kryo、ProtoBuf序列化
- 逆矩阵实现java代码
- JAVA 多线程(一)
- HDU3779 Railroad JAVA版题解+深搜
- java连接MySQL(从驱动下载到测试)
- 类与对象特征总结