【尚硅谷】spring学习笔记(13):组件装配

  • 组件装配
  • <context:component-scan> 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.


  • 使用 @Autowired 自动装配 Bean
  • @Autowired 注解自动装配具有兼容类型的单个 Bean属性
  • 构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
  • 默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
  • 默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
  •  @Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
  • @Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean. 
  • @Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值。


  • 使用 @Resource 或 @Inject 自动装配 Bean
  • Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
  • @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
  • @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
  • 建议使用 @Autowired 注解

package com.atguigu.spring.beans.annotation;

import org.springframework.stereotype.Component;

//@Component
public class TestObject {

}

package com.atguigu.spring.beans.annotation.controller;

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

import com.atguigu.spring.beans.annotation.service.UserService;

 
@Controller
public class UserController {
	
	
	private UserService userService; 
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void execute() {
		System.out.println("UserController execute...");
		userService.add();
	}

}

package com.atguigu.spring.beans.annotation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.atguigu.spring.beans.annotation.repository.UserRepository;

@Service("userService")
public class UserService {
	

	private UserRepository userRepository;
	
	@Autowired
	@Qualifier("userJDBCReepository")
	public void setUserRepository( UserRepository userRepository) {
		this.userRepository = userRepository;
	}
	
	public void add() {
		System.out.println("UserService add...");
		userRepository.save();
	}
	
}

package com.atguigu.spring.beans.annotation.repository;

public interface UserRepository {
   void save();
}

package com.atguigu.spring.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJDBCReepository implements UserRepository {

	@Override
	public void save() {
		System.out.println("UserJDBCReepository save...");
		
	}
}

package com.atguigu.spring.beans.annotation.repository;

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

import com.atguigu.spring.beans.annotation.TestObject;

@Repository
public class UserRepositoryImpl implements UserRepository {
	
	@Autowired(required=false)
	private TestObject testObject;
	
	@Override
	public void save() {
		System.out.println("UserRepository Save...");
		System.out.println(testObject);
	}
}

package com.atguigu.spring.beans.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.atguigu.spring.beans.annotation.controller.UserController;
import com.atguigu.spring.beans.annotation.repository.UserRepository;
import com.atguigu.spring.beans.annotation.service.UserService;

public class Main {
	public static void main(String[] args) {
		ApplicationContext apl = new ClassPathXmlApplicationContext("beans-annotation.xml");
//		TestObject to = (TestObject) apl.getBean("testObject");
//		System.out.println(to);
	
		UserController userController = (UserController) apl.getBean("userController");
		System.out.println(userController);
		userController.execute();
	
//		UserService userService = (UserService) apl.getBean("userService");
//		System.out.println(userService);
	
//		UserRepository userRepository = (UserRepository) apl.getBean("userRepository");
//		System.out.println(userRepository);
	}

}

结果:

六月 15, 2018 2:39:38 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Fri Jun 15 14:39:38 CST 2018]; root of context hierarchy
六月 15, 2018 2:39:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-annotation.xml]
com.atguigu.spring.beans.annotation.controller.UserController@6b57696f
UserController execute...
UserService add...
UserJDBCReepository save...


猜你喜欢

转载自blog.csdn.net/oqkdws/article/details/80705253