重生之 SpringBoot3 入门保姆级学习(05、 Bean 装配) @[TOC](重生之 SpringBoot3 入门保姆级学习(05、 Bean 装配)) 2.2.1 通过 ioc.xml 文件配置 2.1.2 通过 @Configuration 注解配置 2.2.4 测试 Bean 是否生效
package com. zhong. boot302demo. bean ; import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User { private String id; private String name;
}
package com. zhong. boot302demo. bean ; import lombok. Data ;
@Data
public class Cat { private String id; private String name;
}
< dependency> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId>
</ dependency>
2.2.1 通过 ioc.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" xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > < bean id = " user" class = " com.zhong.boot302demo.bean.User" > < property name = " id" value = " 1" > </ property> < property name = " name" value = " 张三" > </ property> </ bean> < bean id = " cat" class = " com.zhong.boot302demo.bean.Cat" > < property name = " id" value = " 1" > </ property> < property name = " name" value = " 小猫" > </ property> </ bean>
</ beans>
2.1.2 通过 @Configuration 注解配置
在包下新建 config 文件夹 在 config 文件夹内新建 AppConfig 并标识为 @Configuration
package com. zhong. boot302demo. config ; import com. zhong. boot302demo. bean. User ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class AppConfig { @Bean public User user ( ) { User user = new User ( ) ; user. setId ( "1" ) ; user. setName ( "张三" ) ; return user; }
}
组件默认单实例的,在 Boot302DemoApplication 建立测试代码
package com. zhong. boot302demo ; import com. zhong. boot302demo. bean. User ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ; @SpringBootApplication
public class Boot302DemoApplication { public static void main ( String [ ] args) { var ioc = SpringApplication . run ( Boot302DemoApplication . class , args) ; for ( String s : ioc. getBeanNamesForType ( User . class ) ) { System . out. println ( s) ; } Object user01 = ioc. getBean ( "user" ) ; Object user02 = ioc. getBean ( "user" ) ; System . out. println ( user01 == user02) ; } }
通过 @Scope(“prototype”) 修改为多实例
package com. zhong. boot302demo. config ; import com. zhong. boot302demo. bean. User ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. context. annotation. Scope ;
@Configuration
public class AppConfig { @Scope ( "prototype" ) @Bean public User user ( ) { User user = new User ( ) ; user. setId ( "1" ) ; user. setName ( "张三" ) ; return user; }
}
2.2.4 测试 Bean 是否生效
修改 Boot302DemoApplication 代码查看已经注册的 Bean
package com. zhong. boot302demo ; import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ; @SpringBootApplication
public class Boot302DemoApplication { public static void main ( String [ ] args) { var ioc = SpringApplication . run ( Boot302DemoApplication . class , args) ; for ( String name : ioc. getBeanDefinitionNames ( ) ) { System . out. println ( name) ; } }
}