包扫描
1、xml方式
<?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.demo.ioc"/> </beans>
2、注解方式
package com.demo.ioc; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; // 创建配置文件,可以认为是一个xml配置文件 @Configuration // 设置需要扫描的包 @ComponentScan(value = "com.demo.ioc") public class MyConfiguration { } package com.demo.ioc; import org.springframework.stereotype.Component; // 增加注解后会被Spring扫描到, 默认的Id是类名首字母小写 @Component(value = "person") public class Person { }
Bean别名
xml形式
<bean id="person" name="person1, person2" class="com.demo.ioc.Person"/> <alias name="person" alias="person3"/>
注解方式
package com.demo.ioc; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; // 创建配置文件,可以认为是一个xml配置文件 @Configuration public class MyConfiguration { @Bean(name = {"person1", "person2"}) public Person person(){ return new Person(); } }
通过注解注入Bean
- 通过方法注入Bean
- 通过构造方法注入Bean
- 通过Set方法注入Bean
- 通过属性注入Bean
- 集合类Bean类型注入
- 直接注入集合实例
- 将多个泛型的实例注入到集合
- 将多个泛型实例注入到List
- 控制泛型示实例在List中的顺序
- 将多个泛型的实例注入到Map
- String、Integer等类型直接赋值
- SpringIoC容器内置接口示例注入
准备Bean
package com.demo.ioc; import org.springframework.stereotype.Component; @Component public class Cat { } package com.demo.ioc; import org.springframework.stereotype.Component; @Component public class Dog { } package com.demo.ioc; import org.springframework.stereotype.Component; @Component public class Pig { }
配置文件
package com.demo.ioc; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; // 创建配置文件,可以认为是一个xml配置文件 @Configuration @ComponentScan(value = "com.demo.ioc") public class MyConfiguration { @Bean(name = "stringList") public List<String> stringList() { List<String> list = new ArrayList<>(); list.add("Tom"); list.add("Jack"); return list; } // 此优先级高于 List<String> @Bean @Order(2) // 指定优先级 public String string1(){ return "string1"; } @Bean @Order(1) public String string2(){ return "string2"; } @Bean public Map<String, Integer> stringIntegerMap(){ Map<String, Integer> map = new HashMap<>(); map.put("dog", 23); map.put("pig", 24); return map; } // 优先级高于 Map<String, Integer> @Bean public Integer integer1(){ return 222; } @Bean public Integer integer2(){ return 223; } }
注入实例
package com.demo.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @Component public class Person { private Dog dog; // 1、构造方法注入 @Autowired public Person(Dog dog) { this.dog = dog; } private Cat cat; // 2、Set方法注入 @Autowired public void setCat(Cat cat) { this.cat = cat; } // 3、通过属性注入 @Autowired private Pig pig; private List<String> stringList; // 4、注入List类型 @Autowired // 指定Bean的ID // @Qualifier(value = "stringList") public void setStringList(List<String> stringList) { this.stringList = stringList; } public List<String> getStringList() { return stringList; } // 5、Map类型注入 private Map<String, Integer> stringIntegerMap; public Map<String, Integer> getStringIntegerMap() { return stringIntegerMap; } @Autowired public void setStringIntegerMap(Map<String, Integer> stringIntegerMap) { this.stringIntegerMap = stringIntegerMap; } private String name; // 6、String、Integer等类型直接赋值 @Value("Tom") // 如果是Integer会自动转型 public void setName(String name) { this.name = name; } // 7、容器内置接口注入 private ApplicationContext context; @Autowired public void setContext(ApplicationContext context) { this.context = context; } public ApplicationContext getContext() { return context; } @Override public String toString() { return "Person{" + "dog=" + dog + ", cat=" + cat + ", pig=" + pig + ", stringList=" + stringList + ", stringIntegerMap=" + stringIntegerMap + ", name='" + name + '\'' + '}'; } }
测试
package com.demo.ioc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class BeanTest { @Test public void testBean() { // 获取上下文 ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); // 获取Bean Person bean = context.getBean("person", Person.class); System.out.println(bean); System.out.println(bean.getStringList()); System.out.println(bean.getStringIntegerMap()); System.out.println(bean.getContext().getBean("cat")); } }
测试输出
Person{dog=com.demo.ioc.Dog@441772e, cat=com.demo.ioc.Cat@7334aada, pig=com.demo.ioc.Pig@1d9b7cce, stringList=[string2, string1], stringIntegerMap={integer1=222, integer2=223}, name='Tom' } [string2, string1] {integer1=222, integer2=223} com.demo.ioc.Cat@7334aada