Autowired是按照类型注入的(byType)
@Component->通用组件
@Controller->控制器
@Service-> Service
@Repository -> DAO
applicationContext中加入:
```
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.neuedu.test.anotation"></context:component-scan>
```
java类的实现,
```
@Lazy
@Service
public class TestService {
@Autowired
@Qualifier("testSubDAO1")
private TestDAO testDAO;
public void test()
{
System.out.println(testDAO);
}
}
```
```
public class TestDAO {
public void test()
{
System.out.println("testDAO");
}
}
```
```
@Repository
public class TestSubDAO1 extends TestDAO{
public void test()
{
System.out.println("testSubDAO1");
}
}
```
```
@Repository
public class TestSubDAO2 extends TestDAO{
public void test()
{
System.out.println("testSubDAO2");
}
}
```
测试类的实现
```
@Test
public void testIOC4()
{
//1. 启动spring ioc的容器(BeanFactory, ApplicationContext)
//BeanFactory尽量晚的实例化对象,等到getBean()的时候再实例化。
//applicationContext:尽量早的实例化对象,启动的时候,把单例的类都实例化好。非单例的类,等到第一次调用getBean实例化
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.
TestService t = (TestService)ctx.getBean("testService");
t.test();
}
```