Spring IOC/DI

2018-08-20  本文已影响21人  PC_Repair
一、IOC/DI

Spring是一个基于IOC和AOP结构的J2EE系统的框架。IOC(Inversion Of Control)反转控制是Spring的基础。简单说就是创建对象由以前的程序员自己调用 new 构造方法,变成了交由 Spring 创建对象。
DI(Dependency Inject)依赖注入。简单的说就是拿到的对象属性,已经被注入好相关值了,直接使用即可。

public class Category {
    private int id;
    private String 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.ljf.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
 
</beans>
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        Category c = (Category) context.getBean("c");
        System.out.println(c.getName());
    }
}
获取对象的不同方式

二、注入对象
public class Product {
    private int id;
    private String name;
    private Category category;
}

<bean name="c" class="com.how2java.pojo.Category">
    <property name="name" value="category 1" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
    <property name="name" value="product1" />
    <property name="category" ref="c" />
</bean>
public class TestSpring {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        Product p = (Product) context.getBean("p");
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
    }
}

三、注解方式 IOC/DI

使用注解的方式完成注入对象中的效果。

<context:annotation-config/>
<bean name="c" class="com.how2java.pojo.Category">
    <property name="name" value="category 1" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
    <property name="name" value="product1" />
<!-- <property name="category" ref="c" /> -->
</bean>
public class Product {
    private int id;
    private String name;
    @Autowired
    private Category category;
}
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        Product p = (Product) context.getBean("p");
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
    }
}
@Autowired
public void setCategory(Category category) {
  this.category = category;
}
@Resource(name="c")
private Category category;

上述例子是对 注入对象行为 的注解,对bean对象本身进行注解,可以将Category、Product等移出applicationContext.xml配置文件。
applicationContext.xml内容如下:

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

注:其作用是告诉Spring,bean都放在 com.ljf.pojo 这个包下。
为pojo添加 @Component 注解:

@Component("c")
public class Category {
    private int id;
 //因为配置从applicationContext.xml中移出来了,所以属性初始化放在属性声明上进行
    private String name="category 1"; 
}
@Component("p")
public class Product {
    private int id;
    private String name="product 1";
    @Autowired
    private Category category;
}

测试:

public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        Product p = (Product) context.getBean("p");
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
    }
}
上一篇 下一篇

猜你喜欢

热点阅读