简介
Spring自动化装配bean的具体应用,属于DI(依赖注入)里面的具体实现和编程范畴。
1.Spring配置的可选方案
- 在XML中进行显式的配置;
- 在Java中进行显式的配置;
- 隐式的bean发现机制和自动装配;
推荐尽可能使用自动装配机制,显示装配越少越好,在必须显示装配时建议用比XML更强大且类型安全的JavaConfig注解,最后才用XML,所以本篇基本不谈第三点(自己用起来都觉得有点麻烦)
2.自动化装配bean
Spring从两个角度实现自动化装配:
- 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean
- 自动装配(autowiring):Spring自动满足bean之间的依赖
2.1创建可被发现的bean
首先定义一个接口,只有一个printMessage()方法
package helloWorld; public interface Message { void printMessage(); }
然后创建一个它的实现
package helloWorld; import org.springframework.stereotype.Component; @Component public class MessagePrinter implements Message{ @Override public void printMessage(){ System.out.println("hello world!"); } }
Component翻译过来就是组件的意思,此时的@Component注解表明该类会作为组件类,并告知Spring要为MessagePrinter类创建bean。
由于组件扫描默认是不启用的,所以还要有一个显式的配置类,从而让容器去寻找@Component注解的类,@ComponentScan默认会扫描与配置类相同的包
package helloWorld; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan public class HelloWorldConfig { }
2.1.1为组件扫描的bean命名
Spring应用上下文中所有的bean都会给定一个ID,若未制定默认ID为类名并将第一个字母变为小写。例如将上面的MessagePrinter类设置ID后如下所示:
package helloWorld; import org.springframework.stereotype.Component; @Component("printer") public class MessagePrinter implements Message{ @Override public void printMessage(){ System.out.println("hello world!"); } }
2.2通过为bean添加@Autowired注解实现自动装配
package helloWorld; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MessageMain { private MessagePrinter mess; @Autowired public MessageMain(MessagePrinter mess) { this.mess = mess; } public void print() { mess.printMessage(); } }