使用配置类替代xml配置文件

创建配置类

package com.xxx.spring.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//作为配置类,替换xml配置文件,不使用xml方式进行包扫描
@ComponentScan(basePackages = {"com.xxx.spring"})
public class MyConfig {
}

测试代码

@Test
    public void testAnno(){
        //加载配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }

输出:

UserDaoImpl2...
service add...

 

 

posted @ 2023-02-09 21:40  Mr_sven  阅读(23)  评论(0编辑  收藏  举报