想要快速掌握spring?不妨看看这篇文章

95 阅读3分钟

Spring 是一个轻量级的 IOC 和 AOP 容器框架,用来装 javabean(java对象),是为 Java 应用程序提供的基础性服务的一套开源的 JavaEE 开发框架

常见的配置方式:

【基于XML的配置】【基于注解的配置】【基于Java的配置】

Spring能做什么

除了不能帮我们写业务逻辑,其余的几户都能帮到我们简化开发;能帮我们根据配置文件创建及组装对象之间的依赖关系;Spring面向切面编程能帮我们无耦合的实现日志记录,性能统计,安全控制;能非常简单的管理数据库事务(JDBC,JPA,Hibernate)


1.DI(依赖注入)/ IOC(控制反转)

导包

【spring-core】4.1.2--核心包
【spring-context】4.1.2--文支持包
【spring-aop】4.1.2--切面 包
【spring-text】4.1.2--测试包
【junit】4.12--测试包
【aspect jweaver】1.8.8--织入包(使用springAOP必须导的包)

<dependencies>

        <!--切面包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>

        <!--核心包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>

        <!--上线文支持包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>

        <!--测试包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--织入包(使用SpringAOP必须导的包)-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.8</version>
        </dependency>

        <!--测试包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
    </dependencies>

    <!--当src/java/main扫描不到xml时导的包-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/test/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

name 【名称】

    <bean id="myBean" class="cn.zx._01reviw.MyBean">
        <constructor-arg name="name" value="嘿嘿"/>
        <constructor-arg name="age" value="12"/>
    </bean>

index 【索引】

    <bean id="myBean" class="cn.zx._01reviw.MyBean">
        <constructor-arg index="0" value="呦呦"/>
        <constructor-arg index="1" value="11"/>
    </bean>

type 【类型】

    <bean id="myBean" class="cn.zx._01reviw.MyBean">
        <constructor-arg type="java.lang.String" value="哈哈"/>
        <constructor-arg type="java.lang.Integer" value="10"/>
    </bean>

什么都没有

   <bean id="myBean" class="cn.zx._01reviw.MyBean">
        <constructor-arg value="张老板"/>
        <constructor-arg value="16"/>
    </bean>

外部 Bean 可以无限调用【全局】

   <bean id="OtherBean" class="cn.zx._01reviw.OtherBean"/>
    <bean id="myBean" class="cn.zx._01reviw.MyBean">
        <constructor-arg value="张大仙"/>
        <constructor-arg value="12"/>
        <constructor-arg ref="OtherBean"/>
    </bean>

内部 Bean 只能内部自己用【局部】

    <bean id="myBean" class="cn.zx._01reviw.MyBean">
        <constructor-arg value="是"/>
        <constructor-arg value="12"/>
        <constructor-arg>
            <bean class="cn.zx._01reviw.OtherBean"/>
        </constructor-arg>
    </bean>

特殊

// 简单属性
private Long id;
private String name;
private Boolean sex;
private BigDecimal salary;

// 对象属性
private List<OtherBean> otherBeanList;
private Set<String> set;
private Set<OtherBean> otherBeanSet;

//下面这个是重点
private Properties props1;
private Properties props2;
private String[] arrays;
private List<String> list;
<bean id="otherBean" class="cn.zx._02demo.OtherBean"/>
    <bean id="myBean" class="cn.zx._02demo.MyBean">
        <property name="id" value="12"/>
        <property name="name" value="张大仙"/>
        <property name="sex" value="true"/>
        <property name="salary" value="21"/>

        <!--List-->
        <property name="list">
            <list>
                <value>嘿嘿</value>
                <value>哈哈</value>
                <value>呦呦</value>
                <value>呦呦</value>
            </list>
        </property>

        <!--set-->
        <property name="set">
            <set>
                <value>嘿嘿</value>
                <value>哈哈</value>
                <value>呦呦</value>
                <value>呦呦</value>
            </set>
        </property>

        <!--otherBean-->
        <property name="otherBeanList">
            <list>
                <bean class="cn.zx._02demo.OtherBean"/>
                <bean class="cn.zx._02demo.OtherBean"/>
                <ref bean="otherBean"/>
                <ref bean="otherBean"/>
            </list>
        </property>

        <!--set<OtherBean>-->
        <property name="otherBeanSet">
            <set>
                <bean class="cn.zx._02demo.OtherBean"/>
                <bean class="cn.zx._02demo.OtherBean"/>
                <ref bean="otherBean"/>
                <ref bean="otherBean"/>
            </set>
        </property>

        <!--数组-->
        <property name="arrays">
            <array>
                <value>红中</value>
                <value>北风</value>
                <value>幺鸡</value>
                <value>白板</value>
            </array>
        </property>

        <!--props1(支持中文)-->
        <property name="props1">
            <props>
                <prop key="qqq">
                    sss
                </prop>
                <prop key="eee">
                    lslsls
                </prop>
                <prop key="rrr">
                    12zhong是
                </prop>
            </props>
        </property>

        <!--props2(不支持中文)-->
        <property name="props2">
            <value>dirverClass=mysql.com.xxurl=mysql:///ahahusername=root中文 </value>
        </property>
    </bean>
</beans>


AOP(面向切面编程)

首先说一下代理模式,代理模式分为 静态代理和 动态代理

静态代理:和装饰者模式一模一样,用这个实现 AOP 反而越来越复杂;

动态代理:AOP 就是通过动态代理实现的,原因就是强....

使用动态代理的方法

一: JDK 原生方案【只支持拥有接口的类,最规范,原生的】

二:CGLB 【支持没有接口的类,也支持有接口的二类】第三方jar包

spring中AOP是面向编程的技术,底层是运用的动态代理,使用的两种方案,一种是原生的JDK,这个最规范的,支持有接口的类,还有一种是第三方jar包 CGLIB都支持有没有接口 ,但是都是用JDK。最规范点


代码演示

1.xml中创建了 要再添加aop命名空间

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

2.service配置和事务管理器

    <!--service配置-->
    <bean id="employeeService" class="cn.zx._05xmlaop.service.Impl.EmployeeServiceImpl"/>

    <!--事务管理器-->
    <bean id="txManager" class="cn.zx._05xmlaop.TxManager"/>

环绕通知

用了环绕,就不需要用另外几个通知了(这个会重复)。如果是两个以上的通知,建议使用环绕

    <aop:config>
        <!--切点 -->
        <aop:pointcut id="pointcut" expression="execution(* cn.zx._05xmlaop.service.I*Service.*(..))"/>

        <!--环绕通知-->
        <aop:aspect ref="txManager">
            <aop:around method="around" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

TxManager

package cn.zx._05xmlaop;
import org.aspectj.lang.ProceedingJoinPoint;

public class TxManager {//事务管理器

    public void begin(){
        System.out.println("开始事务");
    }

    public void commit(){
        System.out.println("提交事务");
    }

    public void rollback(Throwable e){
        System.out.println("回滚事务"+ "错误原因:"+e.getMessage());
    }

    public void close(){
        System.out.println("关闭事务");
    }


    //环绕通知
    public void around(ProceedingJoinPoint joinPoint){
        try {
            begin();
            joinPoint.proceed();
            commit();
        } catch (Throwable e) {
            rollback(e);
            //e.printStackTrace();
        } finally {
            close();
        }
    }
}

SpringTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringTest {

    @Autowired//注入
    private IEmployeeService employeeService;

    @Test
    public void test() throws Exception{
       // System.out.println(employeeService);
        employeeService.save();
    }
}

IEmployeeService

public interface IEmployeeService {
    void save();
}

EmployeeServiceImpl

public class EmployeeServiceImpl implements IEmployeeService{

    public void save() {
        System.out.println("我就是真正的任务");
        //int i = 1/0;//搞一个错误的  看看回滚事务
    }
}

运行结果

运行成功--提交了事务

有报错信息