spring

一、定義

輕量級模塊化的控制反轉(IOC)和麪向切面(AOP)的容器框架

IOC(Inversion of Control):是一個重要的面向對象編程的法則。 一般分爲兩種類型,依賴注入(Dependency Injection,簡稱DI)和依賴查找(Dependency Lookup)
作用:削減計算機程序的耦合問題,低耦合

AOP(Aspect Oriented Programming):通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。AOP是橫向的概念,OOP是縱向的概念
作用:分離應用的業務邏輯和系統服務以便於進行內聚性開發

二、環境

下載地址:http://projects.spring.io/spring-framework/

三、配置

spring_core.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: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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 讀取jdbc.properties屬性信息 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 實例化數據源 dataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 實例化SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>

        <property name="mappingResources">
            <list>
                <value>com/user/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="userDao" class="com.user.dao.impl.UserDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <bean id="userService" class="com.user.service.impl.UserServiceImpl">
        <property name="userDao">
            <ref bean="userDao"/>
        </property>
    </bean>

    <bean id="loginAction" class="com.user.action.LoginAction">
        <property name="userService">
            <ref bean="userService"/>
        </property>
    </bean>
    <bean id="queryUserAction" class="com.user.action.QueryUserAction">
        <property name="userService">
            <ref bean="userService"/>
        </property>
    </bean>
    <bean id="userAction" class="com.user.action.UserAction">
        <property name="userService">
            <ref bean="userService"/>
        </property>
    </bean>

    <!-- 實例化事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <!-- 聲明式事務管理AspectJ xml配置實現 -->
    <tx:advice id="trasactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut expression="execution(* com.user.service.impl.*.*(..))" id="mypointcut"/>
        <aop:advisor advice-ref="trasactionAdvice" pointcut-ref="mypointcut"/>
    </aop:config>


</beans>

jdbs.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_project
jdbc.username=root
jdbc.password=a123

web.xml

<!-- spring監聽器 加載spring核心配置文件 默認是在: /WEB-INF/applicationContext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 自已指定加載配置文件 spring_core.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring_core.xml</param-value>
    </context-param>

四、bean的作用域

singleton:單例
prototype:每次調用的時候都創建,等同於new
request:每次請求都創建
session:每次會話創建
global session

五、初始化、銷燬

  1. xml
    init-method=”方法名”
    destory-method=”方法名”
  2. 實現接口
    JavaBean implements InitlizatingBean
    JavaBean implements DesposableBean

六、註解

@Bean
@Repository 持久層 dao
@Service 業務層 service
@Component
@Autowired 主動注入,可以放在屬性,set方法上
@Controller 控制層 action
可以不要set方法
spring_core.xml中需要加入

<!-- 掃描註解 -->
    <context:component-scan base-package="com.user"></context:component-scan>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章