AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
下面通过注解`@Configuration`将 StudentServiceImpl 类注入到 IoC 中。 <br/> 步骤如下: **1. `StudentServiceImpl`类** ```java public class StudentServiceImpl { public void print() { System.out.println("StudentServiceImpl"); } } ``` **2. 创建配置类`CustomConfig`作为IoC容器** ```java //标注当前类为IoC容器 @Configuration public class CustomConfig { /** * 将StudentServiceImpl注册到当前的IoC容器中 * * @Bean注解的作用:方法的返回值作为<bean class="方法的返回值"/>,方法名作为<bean id="方法名"/> */ @Bean public StudentServiceImpl studentService() { return new StudentServiceImpl(); } } ``` **3. 测试** ```java @Test void contextLoads02() { //获取IoC容器 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomConfig.class); //从IoC容器中拿到StudentServiceImpl对象 StudentServiceImpl impl = context.getBean(StudentServiceImpl.class); impl.print(); //StudentServiceImpl } ```