Springboot学习笔记(2)——依赖注入之一(注解配置)
控制反转(IoC)是通过依赖注入实现的。
依赖注入的步骤:
1、编写并声明Bean
package com.red.demo.ioc;
import org.springframework.stereotype.Service; @Service //1 public class Screen { public String display(){ return "我命由我不由天!"; } }
声明Bean的注解有:@Component、@Service、@Controller和@Repository等,它们在功能上没有区别,在这里都可以使用,只是它们具有各自的字面意义。
2、编写使用该Bean的类(被注入的类)
package com.red.demo.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class Cinema { @Autowired //2 Screen nezha; public String playMovie(){ return nezha.display(); } }
注入Bean的注解有:@Autowired、@Inject和@Resource。
其中,@Autowired为Spring提供的注解、@Inject(JSR-330)和@Resouce(JSR-250)都是Java提供的注解。
3、编写配置类
package com.red.demo.ioc; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration //3 @ComponentScan("com.tiger.demo.ioc") //4 public class MovieConfig { }
4、运行
package com.red.demo; import com.tiger.demo.ioc.Cinema; import com.tiger.demo.ioc.MovieConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class DemoApplication { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MovieConfig.class); Cinema cinema = context.getBean(Cinema.class); System.out.println(cinema.playMovie()); context.close(); }
运行打印的日志:
10:57:03.238 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1363fb3 10:57:03.262 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 10:57:03.368 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\IdeaProjects\demo\target\classes\com\red\demo\ioc\Cinema.class] 10:57:03.369 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\IdeaProjects\demo\target\classes\com\red\demo\ioc\Screen.class] 10:57:03.485 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 10:57:03.488 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 10:57:03.491 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 10:57:03.491 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 10:57:03.525 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'movieConfig' 10:57:03.538 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'cinema' 10:57:03.550 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'screen' 我命由我不由天! 10:57:03.582 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1363fb3, started on Sun Aug 11 10:57:03 CST 2019