自动装配:
         spring利用依赖注入(DI),完成对IOC容器中各个组件的依赖关系赋值;
 
 1) @Autowired:自动注入:
         1. 默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);
         2. 如果找到多个相同类型的组件,再将属性的名称座位组件的id去容器中查找 applicationContext.getBean("bookDao")
         3. @Qualifier("bookDao");使用@Qualifier指定需要装配的组件的id,而不是使用属性名
         4. 自动状态默认一定要将属性赋值好,没有就会报错;可以使用@Autowired(required=false);
         5. @primary;让Spring进行自动装配的时候,默认使用首选的bean;也可以继续使用@qualifier执行需要装配的bean的名字
         BookService{
             @Autowired
             BookDao bookDao;
         }
 2) Spring还支持使用@Resource(JSR250)和@Inject(JSR330)[java规范的注解]
         @Resource:
             可以和@Autowired一样实现自动装配功能;默认是按照组件名称进行装配的;
             没有支持@primary功能,没有支持@Autowired(required=false);
        @Inject:
            需要导入javax.inject的包,和Autowired的功能一样.没有required=false的功能;
        
        @Autowired:Spring定义的;@Resource,@inject都是java规范

 3) 自动以组件想要使用Spring容器底层的一些组件(APplicationContext、BeanFactory、xxx);

         自定义组件实现xxxAware接口;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;

         把Spring底层一些组件注入到自定义Bean中;

        xxxAware:功能使用xxxProcessor;(后置处理器)

                ApplicationContextAware --> ApplicationContextAwareProcessor;


示例:

package com.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.spring.dao.BookDao;

@ComponentScan({"com.spring.dao","com.spring.service","com.spring.controller"})
@Configuration
public class MainConfigAutowired {
	
	@Primary
	@Bean("bookDao2")
	public BookDao bookDao(){
		BookDao bookdao = new BookDao();
		bookdao.setLevel("2");
		return bookdao;
	}
	
}
package com.spring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.spring.dao.BookDao;

@Service
public class BookService {

	
	@Autowired(required=false)
	private BookDao bookDao;

	public void print(){
		System.out.println(bookDao);
	}

	@Override
	public String toString() {
		return "BookService [bookDao=" + bookDao + "]";
	}
	
}
package com.spring.controller;

import javax.annotation.Resource;

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

import com.spring.service.BookService;

@Controller
public class BookController {
	@Resource
	private BookService bookService;
}
package com.spring.dao;

import org.springframework.stereotype.Repository;


@Repository
public class BookDao {

	private String level = "1";
	
	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	@Override
	public String toString() {
		return "BookDao [level=" + level + "]";
	}
	
}

 测试类:

package com.spring.test;


import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.spring.config.MainConfigAutowired;
import com.spring.dao.BookDao;
import com.spring.service.BookService;

public class IOCTest_Auto {

	@Test
	public void test01(){
		AnnotationConfigApplicationContext	context = new AnnotationConfigApplicationContext(MainConfigAutowired.class);
		
		BookService bookService = context.getBean(BookService.class);
		System.out.println(bookService);
		
		/*BookDao bookDao = context.getBean(BookDao.class);
		System.out.println(bookDao);*/
		
		context.close();
	}
}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐