spring

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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章