您的位置:首页 > 编程语言 > Java开发

【java框架】0209笔记——Spring

2018-02-10 19:10 120 查看

一、spring环境搭建,IOC和DI

1.引入spring的jar包(jar包存入到百度网盘/源码/jar包/中),并且add path
2.创建一个pojo类,用于演示控制反转(IOC),和依赖注入(DI)
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;
}
3.在src目录下配置applicationContext.xml,用于IOC和DI的演示,
通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"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="pojo.Category">
       <property name="name" value="category 1" />
   </bean>
</beans>
4.编写测试类,进行测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import 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());
}
}
5.总结:IOC可以理解为由spring来创建对象,无须程序员再进行new;DI指的是有spring制动注入值;

二、注入对象

上一节中,注入了为category对象注入了一个name属性,这一节中,在product对象中,注入一个category对象。

1.创建product对象
public class Product {
   private int id;
   private String name;
   private Category 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;
   }
   public Category getCategory() {
       return category;
   }
   public void setCategory(Category category) {
       this.category = category;
   }
}
2.使用ref,把上面创建的category c对象注入到product p对象之中
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>
3.测试类方法:
Product p = (Product) context.getBean("p");
    System.out.println(p.getName());
    System.out.println(p.getCategory().getName());    

三、通过注解方式进行IOC/DI操作

1.在bean前增加注解配置:<context:annotation-config/>,并且注释掉ref配置
2.在Product.java的category属性前加上@Autowired注解,并且import org.springframework.beans.factory.annotation.Autowired;
3.直接在main方法中测试即可;

四、AOP面向切片编程

AOP 即 Aspect Oriented Program 面向切面编程。首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务。 所谓的周边功能,比如性能统计,日志,事务管理等等 。
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面。在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 。
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP。

1.准备业务类ProductService
public class ProductService {

public void doSomeService() {
System.out.println("do some service");
}
2.在main方法中调用此方法

五、使用注解方式进行测试

1.单元测试使用junit-4.12.jar,hamcrest-all-1.3.jar 两个jar包;
2.在applicationContext.xml配置好bean;
3.重写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
@Autowired
Category category;
@Test
public void test(){
System.out.println(category.getName());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: