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


<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">

    
    <context:property-placeholder location="classpath:jdbc.properties" />

    
    <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>

    
    <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.xmlvalue>
            list>
        property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.format_sql">trueprop>
            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>

    
    <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


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring_core.xmlparam-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>

你可能感兴趣的:(框架,spring,框架)