Spring使用——通過配置類注入Bean

配置類

@Configuration //告訴spring這是一個配置類
@ComponentScan

  1. value指定要掃描的包
  2. Filter[] excludeFilters() default {}; 掃描的時候按照規則排除哪些
@ComponentScan(value = "com.ysy",excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,Service.class}))
  1. includeFilters 掃描的時候按照規則只包含哪些
  2. ASSIGNABLE_TYPE按照指定類型進行掃描
    使用aspectJ表達式
    REGEX:正則表達式
    custom:自定義規則
package com.ysy.config;

import com.ysy.Person;
import com.ysy.service.BookService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

/**
 * @author shanyangyang
 * @date 2020/5/22
 * 配置類等於以前的配置文件
 */
@Configuration  //告訴spring這是一個配置類
@ComponentScan(value = "com.ysy",includeFilters = @ComponentScan.Filter(
		type = FilterType.CUSTOM,classes = MyTypeFilter.class
//		type=FilterType.ASSIGNABLE_TYPE,classes = BookService.class
//		type = FilterType.ANNOTATION,classes = Controller.class
		),useDefaultFilters = false)
//value指定要掃描的包
//Filter[] excludeFilters() default {}; 掃描的時候按照規則排除哪些
//@ComponentScan(value = "com.ysy",excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,
//		Service.class}))
//includeFilters 掃描的時候按照規則只包含哪些
//ASSIGNABLE_TYPE按照指定類型進行掃描
//使用aspectJ表達式
//REGEX:正則表達式
//custom:自定義規則
public class MainConfig {
	@Bean("person") //給容器中註冊一個bean,爲返回值的類型,id默認是方法名
	public Person person01(){
		return new Person("lisi",20);
	}
}

實體Bean

package com.ysy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;

/**
 * @author shanyangyang
 * @date 2020/5/22
 */
public class Person {
	//使用@value賦值
	//1、基本數值
	//2、可以寫SPEL表達式 #{}
	//3、可以寫${},取出配置文件中的值(運行的環境變量中的值)
	@Value("ysy")
	private String name;
	@Value("#{10+18}")
	private Integer age;
	@Value("${person.nickName}")
	private String nickName;

	public Person() {
	}

	public Person(String name, Integer age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	@Override public String toString() {
		return "Person{" + "name='" + name + '\'' + ", age=" + age + ", nickName='" + nickName + '\'' + '}';
	}
}

測試類

package com.ysy.test;

import com.ysy.Person;
import com.ysy.config.MainConfig;
import com.ysy.config.MainConfig2;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;

/**
 * @author shanyangyang
 * @date 2020/5/22
 */
public class IOCTest {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);

	@Test
	public void testImport(){
		printBeans(context);
	}

	public void printBeans(AnnotationConfigApplicationContext context){
		String[] beanDefinitionNames = context.getBeanDefinitionNames();
		for(String name:beanDefinitionNames){
			System.out.println(name);
		}

		//工廠Bean獲取的是調用getObject創建的對象
		Object colorFactoryBean = context.getBean("colorFactoryBean");
		System.out.println("Bean的類型==="+colorFactoryBean.getClass());

		Object colorFactoryBean1 = context.getBean("&colorFactoryBean");
		System.out.println("Bean的類型==="+colorFactoryBean1.getClass());
	}

//	@Test
//	public void test03(){
//		//獲取環境變量和操作系統
//		ConfigurableEnvironment environment = context.getEnvironment();
//		//System.out.println(environment.getProperty("os.name"));
//		String[] beanNamesForTypes = context.getBeanNamesForType(Person.class);
//		for (String beanNameType:beanNamesForTypes){
//			System.out.println(beanNameType);
//		}
//
//		Map<String, Person> beansOfType = context.getBeansOfType(Person.class);
//		System.out.println(beansOfType);
//	}

//	@Test
//	public void test01(){
//		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
//
//		String[] definitionNames = context.getBeanDefinitionNames();
//		for(String name:definitionNames){
//			System.out.println(name);
//		}
//	}

//	@Test
//	public void test02(){
//		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
//
////		String[] definitionNames = context.getBeanDefinitionNames();
////		for(String name:definitionNames){
////			System.out.println(name);
////		}
//		System.out.println("容器初始化完成");
//		Object person = context.getBean("person");
//		Object person1 = context.getBean("person");
//		System.out.println(person == person1);
//	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章