Spring注入Bean的方式
Spring注入Bean的方式
基于XML配置
基于Jave Config配置
自动化装配
1.基于XML配置
2.基于Java Config 配置类注入Bean
注解:@Component 、@Controller 、@Service 、@Repestory 、@Configuration
@Component 注解的派生性:@Component与@ComponentSanner成对存在 。当前类被@ComponentScanner注释,则Spring会自动扫描当前类所在的包以及子包。将包中所有被@Component注解注释的都会被加入IOC容器。由@Component的派生性,以上注解都继承了@Component,所以也可以被扫描。
@Component派生性的应用:可以自动写一个注解然后继承@Component,也可以被IOC扫描到。
@ComponentScanner 扫描当前类所在的包以及子包所有被@Component注释的类。
注解:@SpringBootApplication
@SpringBootApplication 相当于 @ComponentSanner、 @Component、 @EnabbleAutoConfiguration 三个注解。
3.自动装配
注解:@Import
@Import导入Bean有两种
------> 直接导入一个JavaConfig配置类 【静态导入】
------> 导入一个ImportSelector的实现类【动态导入】
@Import注解的作用:
- @Import导入其他包的JvavaConfig配置类【静态导入】
@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext ca = SpringApplication.run(App.class, args);
Cat bean = ca.getBean(Cat.class);
System.out.println(bean);
}
}
public class OtherConfig {
@Bean
public Cat cat(){
return new Cat();
}
}
@Component
@Import(value={OtherConfig.class})
public class SpringConfig {
}
Console控制台打印:
2019-09-03 17:02:33.921 INFO 3984 --- [ main] com.differ.demo5.App : Starting App on OK0LJFOQX5TIUTB with PID 3984 (C:\Users\Administrator\Desktop\demo\demo\target\classes started by Administrator in C:\Users\Administrator\Desktop\demo\demo)
2019-09-03 17:02:33.954 INFO 3984 --- [ main] com.differ.demo5.App : No active profile set, falling back to default profiles: default
2019-09-03 17:02:48.974 INFO 3984 --- [ main] com.differ.demo5.App : Started App in 20.134 seconds (JVM running for 20.703)
com.differ.demo3.Cat@518caac3
- @Import导入ImportSelector接口实现类,实现Bean的批量导入【自动装配的原理】
@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext ca = SpringApplication.run(App.class, args);
Cat bean = ca.getBean(Cat.class);
Dog dog = ca.getBean(Dog.class);
System.out.println(bean);
System.out.println(dog);
}
}
@Component
@UseOtherConfigAnnotatin
public class SpringConfig {
}
@Import导入类为ImportSelector的实现类
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(value=MyAutoConfigImportSelector.class)
public @interface UseOtherConfigAnnotatin {
}
Console控制台打印:
2019-09-03 17:19:41.592 INFO 5792 --- [ main] com.differ.demo5.App : Started App in 1.158 seconds (JVM running for 1.606)
com.differ.demo3.Cat@1b8a29df
com.differ.demo3.Dog@4fbe37eb
- 实现自动装配功能 【动态导入】
注解:@EnableAutoConfiguration
@EnableAutoConfiguration原理:该注解继承了@Import注解。@Import注解导入了ImportSelector的实现类 AutoConfigurationImportSelctor类。
新建项目 gupao-core ,作为一个组件进行编写。无需注入完整的依赖。
包结构:
pom.xml
只需要可以使用@Configuration注解、@Bean注解即可,所以只注入了context包,无需注入完整的spring包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>gupao-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</project>
java代码
@Configuration
@Configuration
public class GuPaoConfig {
@Bean
public Student student(){
return new Student();
}
}
@Configuration
public class GuPaoOtherConfig {
@Bean
public People people(){
return new People();
}
}
在resources下新建/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gupao.core.GuPaoConfig,\
com.gupao.core.GuPaoOtherConfig
然后使用maven工具 install生成jar包
新建项目 gupao ,引入组件gupao-core,实现自动装配
在gupao的pom.xml中引入组件的依赖。
<dependency>
<groupId>com</groupId>
<artifactId>gupao-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext ca = SpringApplication.run(App.class, args);
People bean = ca.getBean(People.class);
Student dog = ca.getBean(Student.class);
System.out.println(bean.say());
System.out.println(dog.say());
}
}
Console控制台打印
2019-09-03 18:08:52.860 INFO 1892 --- [ main] com.differ.demo5.App : No active profile set, falling back to default profiles: default
2019-09-03 18:08:54.367 INFO 1892 --- [ main] com.differ.demo5.App : Started App in 1.993 seconds (JVM running for 2.454)
我是人
我是学生
@Import的属性:
- @Import必须配合@Component注解使用
- @Import的派生性:其他注解可以继承@Import,然后使用其他注解进行进行标注类,@Import类的导入作用依旧存在。
@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext ca = SpringApplication.run(App.class, args);
Cat bean = ca.getBean(Cat.class);
System.out.println(bean);
}
}
public class OtherConfig {
@Bean
public Cat cat(){
return new Cat();
}
}
@Component
@UseOtherConfigAnnotatin
public class SpringConfig {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(value=OtherConfig.class)
public @interface UseOtherConfigAnnotatin {
}
Console控制台打印:
2019-09-03 17:02:33.921 INFO 3984 --- [ main] com.differ.demo5.App : Starting App on OK0LJFOQX5TIUTB with PID 3984 (C:\Users\Administrator\Desktop\demo\demo\target\classes started by Administrator in C:\Users\Administrator\Desktop\demo\demo)
2019-09-03 17:02:33.954 INFO 3984 --- [ main] com.differ.demo5.App : No active profile set, falling back to default profiles: default
2019-09-03 17:02:48.974 INFO 3984 --- [ main] com.differ.demo5.App : Started App in 20.134 seconds (JVM running for 20.703)
com.differ.demo3.Cat@518caac3
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。