Spring学习-依赖注入

Spring是基于IOC与AOP的框架,而其中的IOC(Inversion of Control)即反转控制是Spring的基础。

在以前学过的知识中,一个新的对象全部为自己手动new出来的,而在Spring中创建对象的的过程则交由了Spring框架来完成。

依赖注入(Decpendency Inject)的意思则为拿到对象的时候,该对象的属性值已被自动注入了,可以直接使用。

 

 1 package com.how2j.pojo;
 2 
 3 
 4 public class Product {
 5     private int id;
 6     private String name;
 7     private Category category;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public Category getCategory() {
21         return category;
22     }
23     public void setCategory(Category category) {
24         this.category = category;
25     }
26     
27     
28 }

 

 

ApplicationContext.xml

<?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.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
  
</beans>

 

bean标签:

  id:指定bean的名称,在获取bean和被依赖时使用。

  name:指定bean的别名。

  class:指定bean的来源。

  property标签:

    name:指定要注入的属性名。

    value:被注入属性的值。

    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>

上述将Category对象注入了Product对象之中。

 

注解方式注入对象

    <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>

在上述位置添加<context:annotation-config/>

在Product.java的category属性前加上@Autowired注解

@Autowired
private Category category;

这样即可产生相同效果。


 

我们还可以用注解bean的方式进行操作。

将上述所有bean标签去掉,添加如下代码:

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

之后在每个类后面加component注解:

@Component("p")
public class Product {

将属性初始化放到属性声明之中:

private String name="product 1";
private String name="category 1";

 

运行将产生相同效果。

 

运行测试

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.pojo.Product;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
        Product p = (Product) context.getBean("p");
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
    }
}

结果:

 

posted @ 2020-02-27 17:08  弗乱  阅读(156)  评论(0编辑  收藏  举报