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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值