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

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


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

查看所有标签

猜你喜欢:

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

Big C++中文版

Big C++中文版

霍斯特曼 / 姚爱红 / 电子工业 / 2007-3 / 85.00元

本书是一本关于C++的优秀教材,南圣何塞州立大学知名教授Horstmann编写。全书深入探讨了C++的知识,并着重强调了安全的标准模板库;本书较厚,但它可用做程序设计专业学生的教材(两学期)。全书在介绍基础知识后,作者论及了一些高级主题。书中面向对象的设计一章探讨了软件开发生命周期问题,给出了实现类关联的实用提示。其他高级主题包括模板,C++标准模板库,设计模式,GUI,关系数据库以及XML等。本......一起来看看 《Big C++中文版》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码