Spring(8):通过注解注入Bean

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u010886217/article/details/102759658

一、注解基本使用介绍

二、环境

三、代码实现

1.构造方法注入

2.Set方法注入

3.直接属性上注解

4.List注入

5.Map注入

6.简单类型String

7.常用接口注入

四、总结

1.@Bean和@Component区别


一、注解基本使用介绍

注解注入各种类型的Bean。

二、环境

1.Pom

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <!--单元测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>


三、代码实现

1.构造方法注入

(1)AnotherBean 类

package com.spring.ioc;

import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2019/10/26.
 */
@Component
public class AnotherBean {
}

(2)MyBean类

package com.spring.ioc;

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

/**
 * Created by Administrator on 2019/10/26.
 */
@Component
public class MyBean {

    private AnotherBean anotherBean1;

    @Autowired
    public MyBean(AnotherBean anotherBean1) {
        this.anotherBean1 = anotherBean1;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "anotherBean1=" + anotherBean1 +
                '}';
    }
}


(3)MyConfiguration类(相当于之前的spring.xml文件)

package com.spring.ioc;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {
}

(4)测试

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);
    }
}

结果

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@548a102f}

2.Set方法注入

(1)修改MyBean类
添加属性anotherBean2以及对应set方法,同时重写toString方法

package com.spring.ioc;

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

/**
 * Created by Administrator on 2019/10/26.
 */
@Component
public class MyBean {

    //构造方法
    private AnotherBean anotherBean1;

    //Set方法
    private AnotherBean anotherBean2;

    @Autowired
    public void setAnotherBean2(AnotherBean anotherBean2) {
        this.anotherBean2 = anotherBean2;
    }

    @Autowired
    public MyBean(AnotherBean anotherBean1) {
        this.anotherBean1 = anotherBean1;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "anotherBean1=" + anotherBean1 +
                ", anotherBean2=" + anotherBean2 +
                '}';
    }
}

(2)测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);
    }
}

结果:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@1a84f40f, anotherBean2=com.spring.ioc.AnotherBean@1a84f40f}

3.直接属性上注解

(1)修改MyBean类

package com.spring.ioc;

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

/**
 * Created by Administrator on 2019/10/26.
 */
@Component
//@Scope
public class MyBean {

    //构造方法
    private AnotherBean anotherBean1;

    //Set方法
    private AnotherBean anotherBean2;

    //属性直接注解
    @Autowired
    private AnotherBean anotherBean3;
    
    @Autowired
    public void setAnotherBean2(AnotherBean anotherBean2) {
        this.anotherBean2 = anotherBean2;
    }

    @Autowired
    public MyBean(AnotherBean anotherBean1) {
        this.anotherBean1 = anotherBean1;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "anotherBean1=" + anotherBean1 +
                ", anotherBean2=" + anotherBean2 +
                ", anotherBean3=" + anotherBean3 +
                '}';
    }
}

(2)测试

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);
    }
}


结果:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@258e2e41, anotherBean2=com.spring.ioc.AnotherBean@258e2e41, anotherBean3=com.spring.ioc.AnotherBean@258e2e41}

4.List注入

(1)MyBean:List直接set注入

package com.spring.ioc;

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

import java.util.List;

/**
 * Created by Administrator on 2019/10/26.
 */
@Component
//@Scope
public class MyBean {

    //构造方法
    private AnotherBean anotherBean1;

    //Set方法
    private AnotherBean anotherBean2;

    //属性直接注解
    @Autowired
    private AnotherBean anotherBean3;

    public List<String> getStringList() {
        return stringList;
    }

    @Autowired
    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    //List注入
    private List<String> stringList;


    @Autowired
    public void setAnotherBean2(AnotherBean anotherBean2) {
        this.anotherBean2 = anotherBean2;
    }

    @Autowired
    public MyBean(AnotherBean anotherBean1) {
        this.anotherBean1 = anotherBean1;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "anotherBean1=" + anotherBean1 +
                ", anotherBean2=" + anotherBean2 +
                ", anotherBean3=" + anotherBean3 +
                '}';
    }
}


(2)MyConfiguration 类

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

    @Bean
    public List<String> stringList(){
        List<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        return list;
    }
}

(3)测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);

        for (String s:myBean.getStringList()){
            System.out.println("s = "+s);
        }
    }
}

结果:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@6a01e23, anotherBean2=com.spring.ioc.AnotherBean@6a01e23, anotherBean3=com.spring.ioc.AnotherBean@6a01e23}
s = aaa
s = bbb

(4)第二种注入list方法:修改MyConfiguration
即:如果一个list的属性需要注入,则spring会在上下文中寻找List对应泛型的所有实例,并且注入到里面

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

//    @Bean
//    public List<String> stringList(){
//        List<String> list=new ArrayList<String>();
//        list.add("aaa");
//        list.add("bbb");
//        return list;
//    }

    @Bean
    public String string1(){
        return "33333";
    }

    @Bean
    public String string2(){
        return "222222";
    }
}

测试代码结果:

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);

        for (String s:myBean.getStringList()){
            System.out.println("s = "+s);
        }
    }
}

结果:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@6dbb137d, anotherBean2=com.spring.ioc.AnotherBean@6dbb137d, anotherBean3=com.spring.ioc.AnotherBean@6dbb137d}
s = 33333
s = 222222

(5)如果两种注入都存在,则按照第二种方法注入;如果需要第一种,则需要指定Bean的id。

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

    @Bean("stringList")
    public List<String> stringList(){
        List<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        return list;
    }

    @Bean
    public String string1(){
        return "33333";
    }

    @Bean
    public String string2(){
        return "222222";
    }
}

修改myBean,指定注入id


测试代码:

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);

        for (String s:myBean.getStringList()){
            System.out.println("s = "+s);
        }
    }
}

结果:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@6b19b79, anotherBean2=com.spring.ioc.AnotherBean@6b19b79, anotherBean3=com.spring.ioc.AnotherBean@6b19b79}
s = aaa
s = bbb

(6)控制list的元素数据的顺序
通过@Order方法注入顺序,序号越小越靠前

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

    @Bean("stringList")
    public List<String> stringList(){
        List<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        return list;
    }

    @Bean
    @Order(54)
    public String string1(){
        return "33333";
    }

    @Bean
    @Order(4)
    public String string2(){
        return "222222";
    }
}

MyBean类去除,注入Bean的id


测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);

        for (String s:myBean.getStringList()){
            System.out.println("s = "+s);
        }
    }
}

结果:222222在前面

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@43301423, anotherBean2=com.spring.ioc.AnotherBean@43301423, anotherBean3=com.spring.ioc.AnotherBean@43301423}
s = 222222
s = 33333

5.Map注入

(1)MyBean类,添加map属性


(2)Config类

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

    @Bean("stringList")
    public List<String> stringList(){
        List<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        return list;
    }

    @Bean
    @Order(54)
    public String string1(){
        return "33333";
    }

    @Bean
    @Order(4)
    public String string2(){
        return "222222";
    }

    @Bean
    public Map<String,Integer> integerMap(){
        Map<String ,Integer> map =new HashMap<String, Integer>();
        map.put("aaa",123456);
        map.put("bbb",234567);
        return map;
    }
}

(3)测试

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

    @Bean("stringList")
    public List<String> stringList(){
        List<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        return list;
    }

    @Bean
    @Order(54)
    public String string1(){
        return "33333";
    }

    @Bean
    @Order(4)
    public String string2(){
        return "222222";
    }

    //map的注入bean
    @Bean
    public Map<String,Integer> integerMap(){
        Map<String ,Integer> map =new HashMap<String, Integer>();
        map.put("aaa",123456);
        map.put("bbb",234567);
        return map;
    }
}


(4)第二种方法注入,在MyConfiguration 类

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/26.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class MyConfiguration {

    @Bean("stringList")
    public List<String> stringList(){
        List<String> list=new ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        return list;
    }

    @Bean
    @Order(54)
    public String string1(){
        return "33333";
    }

    @Bean
    @Order(4)
    public String string2(){
        return "222222";
    }

    //map的注入bean
    @Bean
    public Map<String,Integer> integerMap(){
        Map<String ,Integer> map =new HashMap<String, Integer>();
        map.put("aaa",123456);
        map.put("bbb",234567);
        return map;
    }
    
    //map第二种方法
    @Bean("ini1")
    public Integer integer1(){
        return 222222;
    }
    @Bean("int2")
    public Integer integer2(){
        return 333333;
    }
}

测试代码不变,结果默认map的key为对应Bean的id:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@51e5fc98, anotherBean2=com.spring.ioc.AnotherBean@51e5fc98, anotherBean3=com.spring.ioc.AnotherBean@51e5fc98}
s = 222222
s = 33333
entry = ini1=222222
entry = int2=333333


6.简单类型String

(1)MyBean类

package com.spring.ioc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/26.
 */
@Component
//@Scope
public class MyBean {

    //构造方法
    private AnotherBean anotherBean1;

    //Set方法
    private AnotherBean anotherBean2;

    //属性直接注解
    @Autowired
    private AnotherBean anotherBean3;

    public List<String> getStringList() {
        return stringList;
    }

    @Autowired
//    @Qualifier("stringList")
    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    //List注入
    private List<String> stringList;

    //map注入
    private Map<String,Integer> integerMap;

    //string
    private String string;

    public String getString() {
        return string;
    }

    @Value("string11111")
    public void setString(String string) {
        this.string = string;
    }

    public Map<String, Integer> getIntegerMap() {
        return integerMap;
    }

    @Autowired
    public void setIntegerMap(Map<String, Integer> integerMap) {
        this.integerMap = integerMap;
    }

    @Autowired
    public void setAnotherBean2(AnotherBean anotherBean2) {
        this.anotherBean2 = anotherBean2;
    }

    @Autowired
    public MyBean(AnotherBean anotherBean1) {
        this.anotherBean1 = anotherBean1;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "anotherBean1=" + anotherBean1 +
                ", anotherBean2=" + anotherBean2 +
                ", anotherBean3=" + anotherBean3 +
                ", stringList=" + stringList +
                ", integerMap=" + integerMap +
                ", string='" + string + '\'' +
                '}';
    }
}


(2)测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);

        for (String s:myBean.getStringList()){
            System.out.println("s = "+s);
        }

        //很帅气
        for (Map.Entry<String, Integer> stringIntegerEntry : myBean.getIntegerMap().entrySet()) {
            System.out.println("entry = "+ stringIntegerEntry);
        }

        //string类型
        System.out.println(myBean.getString());


    }
}


结果:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@48fa0f47, anotherBean2=com.spring.ioc.AnotherBean@48fa0f47, anotherBean3=com.spring.ioc.AnotherBean@48fa0f47, stringList=[222222, 33333], integerMap={ini1=222222, int2=333333}, string='string11111'}
s = 222222
s = 33333
entry = ini1=222222
entry = int2=333333
string11111

7.常用接口注入

(1)MyBean类

(2)测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

/**
 * Created by Administrator on 2019/10/26.
 */
public class testcode {

    @Test
    public void test(){
        ApplicationContext  context=new AnnotationConfigApplicationContext(MyConfiguration.class);

        MyBean myBean=context.getBean("myBean",MyBean.class);
        System.out.println("myBean = " + myBean);

        for (String s:myBean.getStringList()){
            System.out.println("s = "+s);
        }

        //很帅气
        for (Map.Entry<String, Integer> stringIntegerEntry : myBean.getIntegerMap().entrySet()) {
            System.out.println("entry = "+ stringIntegerEntry);
        }

        //string类型
        System.out.println(myBean.getString());

        //接口测试
        AnotherBean anotherBean = myBean.getContext().getBean("anotherBean", AnotherBean.class);
        System.out.println("anotherBean = " + anotherBean);

    }
}


结果获得anotherBean:

myBean = MyBean{anotherBean1=com.spring.ioc.AnotherBean@75329a49, anotherBean2=com.spring.ioc.AnotherBean@75329a49, anotherBean3=com.spring.ioc.AnotherBean@75329a49, stringList=[222222, 33333], integerMap={ini1=222222, int2=333333}, string='string11111'}
s = 222222
s = 33333
entry = ini1=222222
entry = int2=333333
string11111
anotherBean = com.spring.ioc.AnotherBean@75329a49


四、总结

1.@Bean和@Component区别

@Bean注解在方法上;@Component注解在类上,两者生成的实例,供spring使用。

猜你喜欢

转载自blog.csdn.net/u010886217/article/details/102759658