一、IOC/DI
Spring是一个基于IOC和AOP结构的J2EE系统的框架。IOC(Inversion Of Control)反转控制是Spring的基础。简单说就是创建对象由以前的程序员自己调用 new 构造方法,变成了交由 Spring 创建对象。
DI(Dependency Inject)依赖注入。简单的说就是拿到的对象属性,已经被注入好相关值了,直接使用即可。
- pojo
public class Category {
private int id;
private String name;
... //省略方法函数
}
-
applicationContext.xml
文件:applicationContext.xml是Spring的核心配置文件,通过关键字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="com.ljf.pojo.Category">
<property name="name" value="category 1" />
</bean>
</beans>
- 演示通过 Spring 获取 Category对象以及该对象被注入的name属性
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());
}
}
获取对象的不同方式
- 传统方式:Ctaegory c = new Category();
- IOC方式:Category c = Spring生成
二、注入对象
- pojo
public class Product {
private int id;
private String name;
private Category category;
}
-
applicationContext.xml
(头部内容直接复制上面的xml文件)内容如下:在创建Product的时候注入一个Category对象,这里要使用ref
来注入另一个对象。
<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>
- 通过Spring拿到的Product对象已经被注入了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());
}
}
三、注解方式 IOC/DI
使用注解的方式完成注入对象中的效果。
-
applicationContext.xml
(头部内容直接复制上面的xml文件)内容如下:加入<context:annotation-config/>
表示告诉Spring要用注解的方式进行配置。
<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>
- @Autowired:在
Product.java
的category属性上加 @Autowired 注解
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的位置:除了在属性前加上 @ Autowired 这种方式外,也可以在setCategory方法前加上@Autowired 来达到相同的效果。
@Autowired
public void setCategory(Category category) {
this.category = category;
}
- @Resource:除了@Autowired之外,@Resource也是常用的手段。以下达到与@Autowired 相同的效果。
@Resource(name="c")
private Category category;
-
对 Bean 的注解:
上述例子是对 注入对象行为 的注解,对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());
}
}
网友评论