Spring @Autowired 注解常用的依赖性接口及数组、Map
2016-03-31 21:37
513 查看
@Autowired
Private ApplicationContext context
这段代码就可以获得IOC容器的上下文信息,类似的依赖性接口还有ResourseLoader,BeanFactory
关于数组与Map,我们用例子说明
第一步:写出核心xml、一个接口类BeanInterface、2个实现类BeanImpOne和BeanimpTwo、一个中间类、测试类
核心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:component-scan base-package="com.imooc.beanannotation"></context:component-scan>
</beans>
接口类BeanInterface:
package com.imooc.beanannotation;
public interface BeanInterface {
}
2个实现类:
package com.imooc.beanannotation;
import org.springframework.stereotype.Component;
@Component
public class BeanImpOne implements BeanInterface {
}
package com.imooc.beanannotation;
import org.springframework.stereotype.Component;
@Component
public class BeanimpTwo implements BeanInterface {
}
这里用@Component是为了让Spring框架发现这2个类
中间类:
package com.imooc.beanannotation;
import java.util.List;
import java.util.Map;
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(null!=list)
{
for(BeanInterface bean:list)
{
System.out.println(bean.getClass().getName());
}
}
if(null!=map&& 0!=map.size())
{
for(Map.Entry<String, BeanInterface> entry :map.entrySet())
{
System.out.println(entry.getKey()+entry.getValue().getClass().getName());
}
}
}
}
这是重点要讲的,我们在 @Autowired注解的list与map',作用就是告诉Spring找出BeanInterface所有的list和map(这里的Spring就是实例bean的id)
测试类
package com.imooc.beanannotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInjection {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-beanannotation.xml");
// InjectionService service=(InjectionService) ctx.getBean("injectionServiceimpl");
// service.save("this is a autowired");
beanInvoker BI=(beanInvoker) ctx.getBean("beanInvoker");
BI.say();
}
}
最后,补充一下@Order(value) 的作用,value越小,越先执行
要注意@order只针对数组,不能用于Map
看到这里,大家可能会有疑问,如果我不想输出所有类怎么办,Spring给我们提供了Qualifier,它可以缩小范围。
Qualifier的参数是bean的id,在xml中也可以配置Qualifier
Private ApplicationContext context
这段代码就可以获得IOC容器的上下文信息,类似的依赖性接口还有ResourseLoader,BeanFactory
关于数组与Map,我们用例子说明
第一步:写出核心xml、一个接口类BeanInterface、2个实现类BeanImpOne和BeanimpTwo、一个中间类、测试类
核心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:component-scan base-package="com.imooc.beanannotation"></context:component-scan>
</beans>
接口类BeanInterface:
package com.imooc.beanannotation;
public interface BeanInterface {
}
2个实现类:
package com.imooc.beanannotation;
import org.springframework.stereotype.Component;
@Component
public class BeanImpOne implements BeanInterface {
}
package com.imooc.beanannotation;
import org.springframework.stereotype.Component;
@Component
public class BeanimpTwo implements BeanInterface {
}
这里用@Component是为了让Spring框架发现这2个类
中间类:
package com.imooc.beanannotation;
import java.util.List;
import java.util.Map;
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(null!=list)
{
for(BeanInterface bean:list)
{
System.out.println(bean.getClass().getName());
}
}
if(null!=map&& 0!=map.size())
{
for(Map.Entry<String, BeanInterface> entry :map.entrySet())
{
System.out.println(entry.getKey()+entry.getValue().getClass().getName());
}
}
}
}
这是重点要讲的,我们在 @Autowired注解的list与map',作用就是告诉Spring找出BeanInterface所有的list和map(这里的Spring就是实例bean的id)
测试类
package com.imooc.beanannotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInjection {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-beanannotation.xml");
// InjectionService service=(InjectionService) ctx.getBean("injectionServiceimpl");
// service.save("this is a autowired");
beanInvoker BI=(beanInvoker) ctx.getBean("beanInvoker");
BI.say();
}
}
最后,补充一下@Order(value) 的作用,value越小,越先执行
要注意@order只针对数组,不能用于Map
看到这里,大家可能会有疑问,如果我不想输出所有类怎么办,Spring给我们提供了Qualifier,它可以缩小范围。
Qualifier的参数是bean的id,在xml中也可以配置Qualifier
相关文章推荐
- spring 中事务的PROPAGATION_REQUIRED,Readonly的解释
- Java与Javac版本不一致问题解决方案
- java中extends与implements的区别
- java代理模式
- Java中Map的遍历方式
- java 代码块的划分及其作用
- Java中MD5算法的实现
- spring--注解
- 【转】java io复用模型
- java中单项链表实现方法:增加、删除、插入数据
- 类像“一台数据引擎”
- Java序列化机制中的类版本问题
- java项目——发邮件之阿里云邮箱推送服务(二)
- JDK源码之AbstractCollection
- JavaEE web项目目录结构
- Java多态的体现之继承
- java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
- Java_SE05-Comparable,Map接口,队列,栈
- java串行化
- 如何用Java解析CSV文件