首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
技术百科首页 >Spring Framework >如何在Spring Framework中配置和管理Bean?

如何在Spring Framework中配置和管理Bean?

词条归属:Spring Framework

在Spring Framework中配置和管理Bean,可以采用以下几种方式:

XML配置文件

即通过XML文件配置Bean的属性和依赖关系:

<bean id="userService" class="com.example.UserServiceImpl"> <property name="userRepository" ref="userRepository"/> </bean> <bean id="userRepository" class="com.example.UserRepositoryImpl"/>

注解配置

即通过注解来配置Bean的属性和依赖关系:

@Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; // other methods }

Java配置

即通过Java代码来配置Bean的属性和依赖关系:

@Configuration public class AppConfig { @Bean public UserService userService() { UserServiceImpl userService = new UserServiceImpl(); userService.setUserRepository(userRepository()); return userService; } @Bean public UserRepository userRepository() { return new UserRepositoryImpl(); } }

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
领券