top
2021.7.31

Spring_IOC容器:完全注解开发(Bean管理)

1.完全注解开发

(1)创建配置一个类,替代xml配置文件

@Configuration   //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.tinstu"})   //组件扫描
public class SpringConfig {
    
}

(2)编写测试类

    @Test
    public void test2(){
        //加载spring配置文件
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //获取配置创建对象
        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }

 

THE END
icon
0
icon
打赏
icon
分享
icon
二维码
icon
海报
发表评论
评论列表

赶快来坐沙发