@Autowired注解
Posted 啊啊喔喔smile~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@Autowired注解相关的知识,希望对你有一定的参考价值。
@Autowired注解,默认情况下,如果因为找不到合适的bean将会导致autowiring抛出失败异常,
可以通过@Autowired(required=false)的方式避免.
@Autowired注解可以用于成员变量、set方法、构造器(一个类中只能有一个构造被注释成@Autowired(required=true))、list集合、map。
新建一个web项目,导入jar包,jar包如图:
一、@Autowired注解用于成员变量、set方法、构造器(一个类中只能有一个构造被注释成@Autowired(required=true))
1.新建一个service 接口和一个serviceImpl实现类
1)InjectionService接口
package com.moocer.injection.service; public interface InjectionService { public void save(String arg); }
2)新建InjectionServiceImpl实现类
package com.moocer.injection.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.moocer.injection.dao.InjectionDao; import com.moocer.injection.service.InjectionService; @Service public class InjectionServiceImpl implements InjectionService{ /** * @Autowired注解,默认情况下,如果因为找不到合适的bean将会导致autowiring抛出失败异常, * 可以通过@Autowired(required=false)的方式避免 */ //@Autowired//用于成员变量 private InjectionDao injectionDao; //@Autowired//用于set方法 public void setInjectionDao(InjectionDao injectionDao) { this.injectionDao = injectionDao; } @Autowired//用于注解构造器 ,一个类中只能有一个构造被注释成@Autowired(required=true) public InjectionServiceImpl(InjectionDao injectionDao){ this.injectionDao = injectionDao; } @Override public void save(String arg) { System.out.println("InjectionServiceImpl service接受参数"+arg); arg = arg+":"+this.hashCode(); injectionDao.save(arg); } }
2.新建dao用于数据保存,这里只是模拟数据库保存操作,并不真正操作数据库
1)、新建InjectionDao接口
package com.moocer.injection.dao; public interface InjectionDao { public void save(String args); }
2)、新建InjectionDaoImpl实现类
package com.moocer.injection.dao.impl; import org.springframework.stereotype.Repository; import com.moocer.injection.dao.InjectionDao; @Repository public class InjectionDaoImpl implements InjectionDao{ @Override public void save(String args) { //模拟保存数据 System.out.println("InjectionDaoImpl 保存数据: "+args); } }
3.新建一个测试类并继承测试类的工具类
1)、新建测试类工具类
package com.moocer.test; 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 JUnitBaseUtil { private ClassPathXmlApplicationContext context; private String springXmlpath; public JUnitBaseUtil() {} public JUnitBaseUtil(String springXmlpath) { this.springXmlpath = springXmlpath; } @Before public void before() { if (StringUtils.isEmpty(springXmlpath)) { springXmlpath = "classpath*:spring-*.xml"; } try { context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\\\s]+")); context.start(); } catch (BeansException e) { e.printStackTrace(); } } @After public void after() { context.destroy(); } @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; } } }
2)、新建测试类并继承工具类
package com.moocer.test; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.moocer.injection.service.impl.InjectionServiceImpl; @RunWith(BlockJUnit4ClassRunner.class) public class InjectionTest extends JUnitBaseUtil{ public InjectionTest(){ super("classpath*:config/spring-injection.xml"); } @Test public void testInjection(){ InjectionServiceImpl injection = super.getBean("injectionServiceImpl"); injection.save("this is autowired 注解"); } }
4.新建一个spring-injection.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.moocer.injection"></context:component-scan> </beans>
5.执行测试类得到结果
二、@Autowired注解用于list集合、map
1.新建一个XBeanInter的接口
package com.moocer.annotation.mulbean; public interface XBeanInter { }
2,新建两个实现类实现接口,不需要任何方法体,空着就行
1)、XBeanInterOne
package com.moocer.annotation.mulbean; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(value=2)//@Order(value=2)//这个注解是用来排序的,但是这个排序只针对list有效,针对map没效 @Component public class XBeanInterOne implements XBeanInter { }
2)、XBeanInterTwo
package com.moocer.annotation.mulbean; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(value=1)//@Order(value=1) @Component public class XBeanInterTwo implements XBeanInter { }
3.新建一个CoInvoker类
package com.moocer.annotation.mulbean; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CoInvoker { @Autowired//用于list private List<XBeanInter> list; @Autowired//用于map private Map<String,XBeanInter> map; public void say(){ if(null !=list && 0 != list.size()){ //循环list,把list中每一个两个实现类XBeanInterOne XBeanInterTwo //全部通过@Autowired注解到list中去 System.out.println("list"); for(XBeanInter bean:list){ System.out.println(bean.getClass().getName()); } }else{ System.out.println("List<XBeanInter> is null!!!"); } if(map != null && 0 != map.size()){ System.out.println("map"); for(Map.Entry<String,XBeanInter> entry:map.entrySet()){ System.out.println(entry.getKey()+":"+entry.getValue().getClass().getName()); } }else{ System.out.println("Map<String,XBeanInter> is null!!!!"); } } }
4、新建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.moocer.annotation"></context:component-scan> </beans>
5、新建测试类测试
package com.moocer.test; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.moocer.annotation.BeanAnnotation; import com.moocer.annotation.mulbean.CoInvoker; @RunWith(BlockJUnit4ClassRunner.class) public class AnnotationTest extends JUnitBaseUtil{ public AnnotationTest(){ super("classpath*:config/spring-beanannotation.xml"); } @Test public void testMt(){ CoInvoker co = super.getBean("coInvoker"); co.say(); } }
6.执行测试类结果
以上是关于@Autowired注解的主要内容,如果未能解决你的问题,请参考以下文章