发表于: 2017-06-18 20:59:59

1 1071


java



今天所学:学习spring基本的IOC和AOP


IOC 反转控制

这里学习了一个简单的例子来看看ioc/di的效果

package com.how2java.pojo;
 
public class Category {
 
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private int id;
    private String name;

}



在src下创建一个配置文件,这个spring最核心的地方,通过给bean一个名称去获取我那个实体类

的对象,获取的到了这个对象在里面输入字符串category 1到name中

<?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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

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

    xsi:schemaLocation="

   http://www.springframework.org/schema/beans 

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

   http://www.springframework.org/schema/aop 

   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

   http://www.springframework.org/schema/tx 

   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/context      

   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  

    <bean name="c" class="com.how2java.pojo.Category">

        <property name="name" value="category 1" />

    </bean>

  

</beans>

在创建一个测试类。run,查看spring获取Category对象,以及该对象被注入的name属性。

如图所示,可以打印出通过Spring拿到的Category对象的name属性

package com.how2java.test;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.how2java.pojo.Category;

 

public class TestSpring {

 

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext(

                new String[] { "applicationContext.xml" });

 

        Category c = (Category) context.getBean("c");

         

        System.out.println(c.getName());

    }

}




AOP 面向切面变成

首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 

所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 

所谓的周边功能,比如性能统计,日志,事务管理等等 

周边功能在Spring的变相切面编程AOP思想里,即被定义为切面 

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 

然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP


在这里我看到了这个解说,我认为,AOP的思想是把业务代码和后台处理的代码分开来管理,

不用放在一起,更好的管理和应用吧,只要他们有需要了。就通过,XML配置文件来应用他们。

没什么事的时候互不打扰,需要用到就高效合体工作。但是自己这个所谓的切

面为什么叫做低耦合可选择性的


准备一个业务类

package com.how2java.service;
 
public class ProductService {
     
    public void doSomeService(){
         
        System.out.println("doSomeService");
         
    }
     
}



测试类


package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
 
public class TestSpring {
 
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });

        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}


该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

Object object = joinPoint.proceed();


就是将来与某个核心功能编织之后,用于执行核心功能的代码



package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
 
public class LoggerAspect {
 
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}



核心配置文件

<?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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

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

    xsi:schemaLocation="

   http://www.springframework.org/schema/beans 

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

   http://www.springframework.org/schema/aop 

   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

   http://www.springframework.org/schema/tx 

   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/context      

   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  

    <bean name="c" class="com.how2java.pojo.Category">

        <property name="name" value="yyy" />

    </bean>

 

    <bean name="p" class="com.how2java.pojo.Product">

        <property name="name" value="product1" />

        <property name="category" ref="c" />

    </bean>

     

    <bean name="s" class="com.how2java.service.ProductService">

    </bean>    

     

    <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>

     

    <aop:config>

        <aop:pointcut id="loggerCutpoint"

            expression=

            "execution(* com.how2java.service.ProductService.*(..)) "/>

             

        <aop:aspect id="logAspect" ref="loggerAspect">

            <aop:around pointcut-ref="loggerCutpoint" method="log"/>

        </aop:aspect>

    </aop:config>     

  

</beans>



明天计划的事补一补需要的spring的基本知识,决定每天早上把基础补一下。下去学新的知识。明天在看一看怎么调试和在看一次log4j


遇到的问题:今天的遇到的问题,又在整合mybatis和spring的报错,以为是log4j的JAR包XML问题,结果不是。找了很久没找到。明天看看是什么情况。

  


 2.在学习spring和mybatis的时候,很多东西都不懂,一个知识点要看好久好就,都不能理解是什么意思。学习起来非常的吃力,基础太差已经很难看懂后面的问题了。只能跟着案例去敲,这样不能理解很苦恼。明天决定早上就看基础,下午就学新的知识。


收获:今天的收获是学习了简单的AOP和IOC例子知识,知道了一点点spring工作的核心。和尝试着在整合spring和mybatis。也学习到了如何配置log4j。 



返回列表 返回列表
评论

    分享到