发表于: 2018-04-20 23:18:53

1 481


今天完成的事情:             

       Spring是一个基于IOC和AOP的结构的J2EE系统的框架,IOC反转控制是Spring的基础,Inversion Of Control

简单的说就是创建对象由以前的程序员字句new构造方法来调用,变成了交由Spring创建对象DI依赖注入Dependency Inject。简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

步骤:

         1.在src目录下新建applicationContext.xml文件,applicationContext.xml是Spring的核心配置文件。

    <context:component-scan base-package="com.jnshu.pojo"/>

其作用是告诉Spring,bean都放在com.jnshu.pojo这个包下

          2.测试类,演示通过spring获取Studnt对象,以及该对象被注入的name属性。

 ApplicationContext context = new ClassPathXmlApplicationContext(

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

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

以获取对象的方式来进行比较

1.传统的方式

通过new关键字主动创建一个对象

2.IOC方式

对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。IOC时反转控制的缩写,就像控制权本来在自己手里,交给了Spring


3.AOP

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

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

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

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

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

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


注解方式AOP


注解配置业务类

使用@Component("S")注解ProductService类


package com.jnshu.service;

 

import org.springframework.stereotype.Component;

 

 @Component("s")

 public class ProductService {

    public void doSomeService(){

        System.out.println("doSomeService");

    }

     

 }


注解配置切面

@Aspect 注解表示这是一个切面

@Component 表示这是一个bean,由Spring进行管理

@Around(value = "execution(* com.jnshu.service.ProductService.*(..))") 表示对com.jnshu.service.ProductService 这个类中的所有方法进行切面操作


package com.jnshu.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;

 

@Aspect

@Component

public class LoggerAspect {  

    @Around(value = "execution(* com.jnshu.service.ProductService.*(..))")

    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;

    }

}

4.Mybatis

        平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作ConnectionStatpment,ResultSet这些其实

只是手段的辅助类。不仅如此,访问不同的表,还会写很多雷同的代码。那么用了Mybatis之后,只需要自己提供SQL语句,其他的工作,诸如建立连接,Statement,JDBC相关异常处理等等都交给Mybatis去做了,那些重复性的工作Mybatis也给做掉了,我们只需要关注在增删改查等操作层面,而把技术细节都封装在了我们看不见的地方。

    public static void main(String[] args) throws IOException {

        String resource = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new  SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session=sqlSessionFactory.openSession();

         

        List<Student> cs=session.selectList("listCategory");

        for (Student c : cs) {

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

        }

基本原理

1.应用程序找Mybatis要数据

2.mybatis从数据库中找来数据

 2.1通过mybatis-config.xml定位哪个数据库

 2.2通过Student.xml执行对应的select语句

 2.3基于Student.xml把返回的数据库记录封装在Student对象

 2.4把多个Student对象装在一个Student集合中

3.返回一个Student集合


明天计划的事情:

上传到服务器


遇到的问题:

   1.  java.lang.NullPointerException

     字符串变量未初始化

     2.Configuration problem: Id is required for element 'advice' when used as a top-level tag

    这里报错是因为“advice”里面没有设置ID属性。所以不要盲目复制代码。

    3.nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

       缺少spring所需要的jar。       

收获:

 spring IOC/DI

 spring AOP

 注解方式AOP

 mybatis CRUD

 mybatis 注解法CRUD

 spring和mybatis整合


随手保存日报!!!




返回列表 返回列表
评论

    分享到