spring框架

2017-09-25  本文已影响0人  小沙鹰168

体系结构:

1、Spring core 是spring框架的核心,提供了IOC和依赖注入特性
2、Spring Context 提供了一种框架风格的方式来访问对象,继承了beans包的功能,同时增加了国际化、事件传播、资源装载、以及透明创建上下文
3、Spring AOP 通过配置管理,直接将面向方面编程集成到了框架之中。
4、Spring DAO 提供了JDBC的抽象层。可以消除冗长的JDBC编码和数据库厂商特有的错误代码

开发步骤:
1、引入spring jar包
2、添加applicationContext.xml
3、创建一个javabean里面属性有set get方法即可
4、配置applicationContext.xml
<bean id="he" class="entity.Hello">
<property name="hello" value="我爱中国!!!"></property>
</bean>
5、开发测试
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Hello bean1 = (Hello) ac.getBean("he");//第1种获取bean方式,需要强转
Hello bean2 = ac.getBean("he", Hello.class);//第2种获取bean方式
System.out.println(bean1.getHello());

控制反转:
控制反转(IOC--Inversion of control)所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转

依赖注入:
注入方式:
1、set 注入

<bean id="he" class="entity.Hello"><!--  第1种  set注入 -->
<property name="hello" value="我爱北京!!!"></property>
</bean>

2、构造方法注入

<bean id="stu" class="entity.Student"><!--  第2种  构造方法注入 -->
<constructor-arg index="0" value="张三丰"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>

3、参考注入(接口注入)

<bean id="girl" class="entity.Girl">

<property name="boy" ref="girl"></property><!--  第3种参考注入 -->
</bean>

数组注入:

1 、public class Teacher {
private String[]info;//数组
private List list;//List集合
private Set set;//Set集合
private Map map;//Map键值对

//所以对象都有set get方法。。。。
2、

<bean id="tea" class="entity.Teacher">
<property name="info"><!-- 注入数组 -->
<array>
<value>教授</value>
<value>副教授</value>
<value>高工</value>
</array>
</property>
<property name="list"><!-- 注入List集合 -->
<list>
<value>博士</value>
<value>硕士</value>
<value>学士</value>
</list>
</property>
<property name="set"><!-- 注入Set集合 -->
<set>
<value>北京</value>
<value>上海</value>
<value>南京</value>
</set>
</property>
<property name="map"><!-- 注入Map集合 -->
<map>
<entry key="姓名" value="李小龙"></entry>
<entry key="工资" value="8888"></entry>
<entry key="性别" value="男"></entry>
</map>
</property>
</bean>

3、

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    Teacher tea = ac.getBean("tea",Teacher.class);
    String[] info = tea.getInfo();//取出数组
    for(String ss:info){
        System.out.println(ss);}
    List list = tea.getList();//取出List集合
    for(int i=0;i<list.size();i++){
        System.out.println(list.get(i));}
    Set set = tea.getSet();//取出Set集合
    Iterator iterator = set.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next());
    }
      Map map = tea.getMap();//取出Map中的键和值
      Iterator iterator2 = map.keySet().iterator();
      while(iterator2.hasNext()){
          Object next = iterator2.next();//获取键
          System.out.println(next+":"+map.get(next));//get获取值
      }
      System.out.println(map);

五种类型通知:
1、前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。
3、正常返回通知[After returning advice]:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行。
3、异常返回通知[After throwing advice]:在连接点抛出异常后执行。
4、返回通知[After (finally) advice]:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。
5、环绕通知[Around advice]:环绕通知围绕在连接点前后,比如一个方法调用的前后。这是最强大的通知类型,能在方法调用前后自定义一些操作。环绕通知还需要负责决定是继续处理join point(调用ProceedingJoinPoint的proceed方法)还是中断执行。

aop和oop区别:
1、oop是java在面向对象编程
aop是面向切面编程,AOP主要应用于日志记录,性能统计,安全控制,事务处理等方面,它是为程序员解耦而生.

aop全注解开发:
注解名 说明
@Controller 注解控制层组件,(如struts中的action)
@Service 注解业务层组件,service层组件
@Repository 注解数据访问层组件,DAO层组件
@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
@Autowired 默认是按照类型装配注入(spring)
@Resource 默认是按照名称来装配注入(j2ee)
@Scope 注解用于指定scope作用域的(用在类上)
@Transactional 添加事务

注解配置AOP,为三步
1)使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before、 @After 、@AfterReturning 、@AfterThrowing 、@Around )
2)开发需要被拦截的类
3)将切面配置到xml中,也可以使用自动扫描bean方式
案例:
@Aspect// 使用spring 全注解定义一个切面
@Component("aop")
// 泛指组件当组件不好归类的时候,我们可以使用这个注解进行标注

public class AspInterceptor {
    @Before("execution(* com.hw.service..*.add*(com.hw.entity.Student))")
    public void before() {// 前置通知
        System.out.println("before....使用本程序先交9美金!!!");
    }

    @AfterReturning("execution(* com.hw.service.impl.*.add*(String))")
    public void after() {// 正常返回通知
        System.out.println("after....程序使用正常9美金很值!!!");
    }

    @AfterThrowing("execution(* com.hw.service.impl.*.*(..))")
    public void afterthrowing() {// 异常返回通知
        System.out.println("throwing....程序使用有异常9美金上当了!!!");
    }

    @After("execution(* com.hw.service.impl.*.add*(..))")
    public void afterfinally() {// 返回最终通知
        System.out.println("afterfinally....别想着程序了,9美金已交了!!!");
    }

    @Around("execution(* com.hw.service.impl.*.update*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {// 环绕通知
        System.out.println("环绕通知,要工作了");
        pj.proceed();
        System.out.println("环绕通知,要发工资了");
        /*
         * 环绕通知:能在方法调用前后自定义一些操作。环绕通知还需要负 
         * 责决定是继续处理join point(调用ProceedingJoinPoint的
         * proceed方法)还是中断执行
         */
    }

spring中配置数据源的4中形式:
spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源,JNDI数据源。

实际开发中一般使用
1 DBCP数据源(http://www.cnblogs.com/adolfmc/archive/2013/01/22/2872298.html)
DBCP的配置依赖于2个jar包commons-dbcp.jar,commons-pool.jar

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/cc6?useUnicode=true&characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root" />
        <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="3" />
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="300" />
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="2" />
        <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1" />
    </bean>

2 C3P0是一个开放源代码的JDBC数据源实现项目,C3P0依赖于jar包c3p0.jar

<bean id="dataSource" class=""com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       <property name="url" value="jdbc:mysql:/localhost:3306/cc2?useUnicode=true&characterEncoding=utf8"></property>
       <property name="username" value="root"></property>
       <property name="password" value="root"></property>
   </bean>
上一篇 下一篇

猜你喜欢

热点阅读