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

栏目: Java · 发布时间: 5年前

内容简介:在beans标签后加上一个标签来声明将要使用注解就可以了。如果是使用@Autowired注解的话,可以加在两个地方前面。

在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
复制代码

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C程序设计

C程序设计

谭浩强 / 清华大学出版社 / 2005-7-1 / 26.00元

《C程序设计(第3版)》作者具有丰富的教学经验和编写教材的经验,并针对初学者的特点,精心策划、准确定位,使得《C程序设计(第3版)》概念清晰、例题丰富、深入浅出,受到专家和读者的一致好评。《C程序设计(第3版)》被普遍认为是学习C语言的好教材,并被全国大多数高校选用。十多年来《C程序设计(第3版)》累计发行了800多万册,刨同类书的全国最高记录,是学习C语言的主流用书。《C程序设计(第3版)》曾荣......一起来看看 《C程序设计》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具