可以在传统的成员变量,构造器,和setter方法。实现注入
代码实例
Dao
package com.imooc.beanannotation.injection.dao; public interface InjectionDAO { public void save(String arg); }
package com.imooc.beanannotation.injection.dao; import org.springframework.stereotype.Repository; @Repository public class InjectionDAOImpl implements InjectionDAO { public void save(String arg) { //模拟数据库保存操作 System.out.println("保存数据:" + arg); } }
Service类
package com.imooc.beanannotation.injection.service; public interface InjectionService { public void save(String arg); }
package com.imooc.beanannotation.injection.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.imooc.beanannotation.injection.dao.InjectionDAO; @Service public class InjectionServiceImpl implements InjectionService { // @Autowired private InjectionDAO injectionDAO; @Autowired public InjectionServiceImpl(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; } // @Autowired public void setInjectionDAO(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; } public void save(String arg) { //模拟业务操作 System.out.println("Service接收参数:" + arg); arg = arg + ":" + this.hashCode(); injectionDAO.save(arg); } }
个人注:Autowired相当于给它注解的类开辟空间
测试类
package com.imooc.test.beanannotation; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.beanannotation.injection.service.InjectionService; import com.imooc.beanannotation.multibean.BeanInvoker; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestInjection extends UnitTestBase { public TestInjection() { super("classpath:spring-beanannotation.xml"); } @Test public void testAutowired() { InjectionService service = super.getBean("injectionServiceImpl"); service.save("This is autowired."); } @Test public void testMultiBean() { BeanInvoker invoker = super.getBean("beanInvoker"); invoker.say(); } }
配置文件
<?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>
Autowired实现多个类的注解
接口类
package com.imooc.beanannotation.multibean; public interface BeanInterface { }
实现类1
package com.imooc.beanannotation.multibean; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(2) @Component public class BeanImplOne implements BeanInterface { }
实现类2
package com.imooc.beanannotation.multibean; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(1) @Component public class BeanImplTwo implements BeanInterface { }
注入类
package com.imooc.beanannotation.multibean; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class BeanInvoker { @Autowired private List<BeanInterface> list; @Autowired private Map<String, BeanInterface> map; @Autowired @Qualifier("beanImplTwo") // 表示beanid private BeanInterface beanInterface; public void say() { if (null != list && 0 != list.size()) { System.out.println("list..."); for (BeanInterface bean : list) { System.out.println(bean.getClass().getName()); } } else { System.out.println("List<BeanInterface> list is null !!!!!!!!!!"); } System.out.println(); if (null != map && 0 != map.size()) { 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("Map<String, BeanInterface> map is null !!!!!!!!!!"); } System.out.println(); if (null != beanInterface) { System.out.println(beanInterface.getClass().getName()); } else { System.out.println("beanInterface is null..."); } } }