Spring

一、SpringIOC配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<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配置

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

    <!-- 只扫描属性上的注解 -->
    <!--<context:annotation-config></context:annotation-config>-->
</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操作 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


    <!-- 通过xml配置进行aop操作 -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut  expression="execution(* *.*(..))" id="切入点id" />

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

            <!-- 方法中以proceedingJoinPoint.proceed();为界 -->
            <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连接池

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


六、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约束

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


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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值