Spring

  • 一、SpringIOC配置文件
    • 1、bean标签
    • 2.得到对象
  • 二、IOC注解开发
    • 1.xml配置
  • 三、AOP配置
  • 四、AOP注解
    • 1.增强类上
    • 2.方法上
  • 五、spring配置c3p0连接池
  • 六、spring事务
    • 1.配置xml
    • 2.注解实现
  • 七、较全的springxml约束
  • 八、相关jar包

一、SpringIOC配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    # 有参注入属性
    <bean id="" class="">
        <constructor-arg name="" value="">constructor-arg>
    bean>

    # get注入属性
    <bean id="" class="">

        # 普通类型注入
        <property name="属性名" value="值">property>

        # 自定义class注入
        <property name="属性名" ref="id值">property>

        # 数组、list注入
        <property name="属性名">
            <list>
                <value>value>
                <value>value>
                <value>value>
            list>
        property>

        # map注入
        <property name="属性名">
            <map>
                <entry key="键" value="值">entry>
                <entry key="键" value="值">entry>
                <entry key="键" value="值">entry>
            map>
        property>

        # properties注入
        <property name="属性名">
            <props>
                <prop key="键" >entry>
                <prop key="键" >entry>
                <prop key="键" >entry>
            props>
        property>

    bean>

    # 名称空间注入属性
    // xmlns:名称空间="http://www.springframework.org/schema/名称空间"
    <bean id="" class="" 名称空间:属性名="值">bean>
beans>

1、bean标签

  • id : 无特殊符号

  • class :类路径

  • name : 有特殊符号

  • scope :[singleton(默认,单例)/prototype(多例)/requset/session/globalSession]

  • factory-method : 工厂方法名

  • factory-bean : 工厂对象

2.得到对象

ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = (User) context.getBean("id值");


二、IOC注解开发

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

    
    <context:component-scan base-package="cn.mdx">context:component-scan>

    
    
beans>

2.创建对象(下面四个现在无区别)

@Component([value=]”id”)

@Controller([value=]”id”) web层

@Service([value=]”id”) 业务层

@Repository([value=]”id”) 持久层

3.对象scope

@Scope(“”)

4.对象注入

@Autowired

@Resource(name = “id值”)


三、AOP配置

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="id" class="类路径">bean>

    <bean id="id" class="类路径">bean>

    
    <aop:aspectj-autoproxy>aop:aspectj-autoproxy>


    
    <aop:config>
        
        <aop:pointcut  expression="execution(* *.*(..))" id="切入点id" />

        
        <aop:aspect ref="增强类id值">
            <aop:before method="方法名" pointcut-ref="切入点id值"/>
            <aop:after method="方法名" pointcut-ref="切入点id值"/>

            
            <aop:around method="方法名" pointcut-ref="切入点id值"/>
        aop:aspect>
    aop:config>   
beans>


四、AOP注解

1.增强类上

@Aspect

2.方法上

@Before(value= “execution(* .(..))”)

@After(value= “execution(* .(..))”)

@Around(value= “execution(* .(..))”)


五、spring配置c3p0连接池

id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hhd?characterEncoding=UTF-8">property>
        <property name="user" value="root">property>
        <property name="password" value="root">property>


六、spring事务

1.配置xml


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource">property>
bean>


<tx:advice id="txadvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*"/>
    tx:attributes>
tx:advice>


<aop:config>
    <aop:pointcut id="pointcut1" expression="execution(* cn.mdx.c3p0.UserService.*(..))"/>
    <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
aop:config>

2.注解实现


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource">property>
bean>


<tx:annotation-driven transaction-manager="transactionManager"/>

在需要事务的类上添加 @Transactional


七、较全的springxml约束


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


beans>


八、相关jar包

  • commons-logging-1.2.jar

  • log4j-1.2.16.jar

  • spring-beans-4.3.3.RELEASE.jar

  • spring-context-4.3.3.RELEASE.jar

  • spring-core-4.3.3.RELEASE.jar

  • spring-expression-4.3.3.RELEASE.jar

-

  • aopalliance-1.0.jar

  • aspectjweaver-1.8.7.jar

  • spring-aop-4.3.3.RELEASE.jar

  • spring-aspects-4.3.3.RELEASE.jar

-

  • spring-jdbc-4.3.3.RELEASE.jar

  • spring-tx-4.3.3.RELEASE.jar

  • mchange-commons-java-0.2.11.jar

  • c3p0-0.9.2.1.jar

  • 数据库驱动jar包

你可能感兴趣的:(SSM)