Spring IOC
准备pojo Category,用来演示IOC和DI
注入属性
package pojo;
public class Category {
private int id;
private String name;
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;
}
@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
配置applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入1 到id
<?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:p="http://www.springframework.org/schema/p"
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.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean name="category" class="pojo.Category">
<property name="id" value="1" />
</bean>
</beans>
测试代码,演示通过spring获取Category对象,以及该对象被注入的id属性。
public class test {
public static void main(String[] args) {
//初始化spring容器applianceContext,加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Category category=(Category) context.getBean("category");
System.out.println(category.getId());
}
}
结果
传统的方式:
通过new 关键字主动创建一个对象
IOC方式:
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。
注入对象
创建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;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", category=" + category +
'}';
}
}
配置applicationContext.xml
用ref注入对象
<?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:p="http://www.springframework.org/schema/p"
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.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean name="category" class="pojo.Category">
<property name="id" value="1" />
</bean>
<bean name="product" class="pojo.Product">
<property name="id" value="2" />
<property name="category" ref="category" />
</bean>
</beans>
通过Spring拿到的Product对象已经被注入了Category对象了
public class test {
public static void main(String[] args) {
//初始化spring容器applianceContext,加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Product product=(Product) context.getBean("product");
System.out.println(product.getId());
System.out.println(product.getCategory().getId());
}
}
使用注解的方式完成对象注入
修改applicationContext.xml
添加context:annotation-config/
<?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:p="http://www.springframework.org/schema/p"
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.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean name="category" class="pojo.Category">
<property name="id" value="1" />
</bean>
<bean name="product" class="pojo.Product">
<property name="id" value="2" />
<!--<property name="category" ref="category" />-->
</bean>
</beans>
在Product.java的category属性前加上@Autowired注解
运行结果
结果相同
使用@Resource注解也是可以的,但是要加上name,必须和bean配置的name一致
对bean进行注解
修改applicationContext.xml
添加<context:component-scan base-package=pojo"/>
作用是告诉Spring,bean都pojo这个包下
<?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:p="http://www.springframework.org/schema/p"
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.2.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pojo"/>
</beans>
为Product、Category加上@Component注解,即表明此类是bean
属性初始化放在属性声明上进
运行结果一致
总结
本次课程运用实例解释了IOC和DI,学习了注的两种方式,构造方法注入,基于注解的注入。