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

目录php

1、若是使用注解的话,在配置文件中应该作什么?

在beans标签后加上一个java

<context:annotation-config/>
复制代码

标签来声明将要使用注解就能够了。spring

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

2、注解应该怎么用?

若是是使用@Autowired注解的话,能够加在两个地方前面。bash

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

以下代码:post

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;测试

运行结果:this

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

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

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

此时就能够使用@Resource注解code

先改一下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 复制代码

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

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

相关文章
相关标签/搜索