转载

Spring入门学习手册 2:怎么用注解来DI/IOC

在beans标签后加上一个

<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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--在此处加上此标签-->
    <context:annotation-config/>
    <bean name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>

        <!--将下面这行注释掉,因为代码中将使用自动装配来将categroy实例注入product-->
        <!--<property name="category" ref="c"></property>-->

    </bean>

</beans>
复制代码

二、注解应该怎么用?

如果是使用@Autowired注解的话,可以加在两个地方前面。

属性前面 以及 setter方法前面。

如下代码:

package com.learn.springDemo;

import org.springframework.beans.factory.annotation.Autowired;

public class Product {
    private int id;
    private String name;

    //加在这个地方
    @Autowired
    private Category category;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    //加在这个地方
    @Autowired
    public void setCategory(Category category) {
        this.category = category;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}

复制代码

在这里必须要import org.springframework.beans.factory.annotation.Autowired;

运行结果:

a product's id is 1008611 and its category name is Eather
复制代码

三、注入同一个类的不同实例应该怎么办?

上一篇笔记 里提到了 xml里面可以写多个相同的bean吗 这个问题,也就是注入同一个类的不同实例应该怎么办?在使用xml配置时很简单,改一下name就行。

此时就可以使用@Resource注解

先改一下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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--在此处加上此标签-->
    <context:annotation-config/>
    <bean name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="cc" class="com.learn.springDemo.Category">
        <property name="name" value="David"></property>
        <property name="id" value="10086"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>

        <!--将下面这行注释掉-->
        <!--<property name="category" ref="c"></property>-->

    </bean>

</beans>
复制代码

注解应该加在属性的前面

package com.learn.springDemo;

import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

public class Product {
    private int id;
    private String name;

    @Resource(name = "c")
    private Category category;

    @Resource(name = "cc")
    private Category category1;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public void setCategory1(Category category1) {
        this.category1 = category1;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }

    public Category getCategory1() {
        return category1;
    }
}
复制代码

测试代码:

package com.learn.springTest;

import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});


        Product p = (Product) ac.getBean("p");

        System.out.println(p.getName() + "'s id is " + p.getId() + " and its category name is "  + p.getCategory().getName() + " and " + p.getCategory1().getName());

    }

}
复制代码

运行结果:

a product's id is 1008611 and its category name is Eather and David
复制代码

四、xml配置文件里可不可以 bean 也不写?

上述例子都是对注入对象行为的注解,对于bean对象本身的使用可不可以也用注解的方式进行,而不写到配置文件中呢?

答案当然是可以,修改一下配置文件,去掉所有 beansbean 标签,加上

<context:component-scan base-package="com.learn.springDemo"/>
复制代码

base-package用来指定要扫描的包。

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--在此处加上此标签-->
    <context:component-scan base-package="com.learn.springDemo" />

</beans>
复制代码

类定义代码是:

package com.learn.springDemo;

import org.springframework.stereotype.Component;

@Component("c")
public class Category {
    private int id;
    private String name = "我是一个Category";

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}



package com.learn.springDemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component("p")
public class Product {
    private int id;
    private String name = "我是一个product";

    @Autowired
    private Category category;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCategory(Category category) {
        this.category = category;
    }


    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}
复制代码

测试代码是:

package com.learn.springTest;

import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});


        Product p = (Product) ac.getBean("p");

        System.out.println("product 名字是 " + p.getName() + " category 名字是 "  + p.getCategory().getName());

    }

}
复制代码

运行结果是:

product 名字是 我是一个product category 名字是 我是一个Category
复制代码

至于前面的第三个问题应该怎么解决,暂时我也不知道,可能这个问题没有太大的意义吧!

原文  https://juejin.im/post/5c4db735f265da615705d547
正文到此结束
Loading...