Spring入门总结(五)@Order注解

有的时候,我们希望应用Autowired注解的时候数组有序,于是就有了@Order注解

下面看一个案例:

一个接口:

package multibean;

public interface BeanInterface {

}

接口实现类一:

package multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(value = 2)
//也可以@Order(1)
@Component
public class BeanImplOne implements BeanInterface {

}

接口实现类二:

package multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(value = 1)
@Component
public class BeanImplTwo implements BeanInterface {

}

接口调用者:

package multibean;

import java.util.*;

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

@Component
public class BeanInvoker {
	@Autowired
	private List<BeanInterface> list;
	@Autowired
	private Map<String, BeanInterface> map;
	public void say() {
		if(list!=null && list.size()>0) {
			System.out.println("list : ");
			for(BeanInterface bean : list) {
				System.out.println(bean.getClass().getName());
			}
		}
		else System.out.println("The list is null!");
		
		if(map!=null && map.size() > 0) {
			System.out.println("map : ");
			for(Map.Entry<String, BeanInterface> entry : map.entrySet()) {
				System.out.println(entry.getKey() + "  " + entry.getValue().getClass().getName());
			}
		}
		else System.out.println("The map is null!");
	}
	
	
}

 《单元测试》不会刻意不必理会:

package com.tutorialspoint;


import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class UnitTestBase {
	
	private ClassPathXmlApplicationContext context;
	
	private String springXmlpath;
	
	public UnitTestBase() {}
	
	public UnitTestBase(String springXmlpath) {
		this.springXmlpath = springXmlpath;
	}
	
	@Before
	public void before() {
		if (StringUtils.isEmpty(springXmlpath)) {
			//classpath相当于src
			springXmlpath = "classpath*:spring-*.xml";
		}
		try {
			//[,\\s]正则表达式,意思是以,和空格,tab作为分隔符,分成字符串数组
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}
	
	@After
	public void after() {
		context.destroy();
//		System.out.println("after");
	}
	
	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId) {
		try {
			return (T)context.getBean(beanId);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected <T extends Object> T getBean(Class<T> clazz) {
		try {
			return context.getBean(clazz);
		} catch (BeansException e) {
			e.printStackTrace();
			return null;
		}
	}

}
package com.tutorialspoint;

import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import multibean.BeanInvoker;
import services.InjectionService;
import services.InjectionServiceImpl;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection  extends UnitTestBase{

	public TestInjection() {
		super("classpath:spring-injection.xml");
		// TODO Auto-generated constructor stub
	}
	@Test
	public void test() throws IOException {
//		InjectionService service = super.getBean("injectionServiceImpl");
		InjectionService service = super.getBean(InjectionServiceImpl.class);
		service.save("Hello MVC");
	}
	@Test
	public void testMultiBean() {
		//方式一,通过获得类
//		BeanInvoker invoker = super.getBean(BeanInvoker.class);
		//方式二,通过获得Bean的名字
		BeanInvoker invoker = super.getBean(BeanInvoker.class);
		invoker.say();
	}
}

 xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   
   <context:component-scan base-package="com.tutorialspoint,dao,services,multibean"></context:component-scan>
  <!--  <bean id="beanAnnotation" class="com.tutorialspoint.BeanAnnotation"></bean>--> 
   
</beans>

输出的结果是:

 list : 
multibean.BeanImplTwo
multibean.BeanImplOne
map : 
beanImplOne  multibean.BeanImplOne
beanImplTwo  multibean.BeanImplTwo

可以看到list的数据反过来了,但是map的数据却没有变。

因为@Order针对数组的。

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/81461451