完全注解开发
(1)创建配置类,替代xml配置文件
package com.atguigu.spring.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.atguigu.spring"})
public class SpringConfig {}
(2)编写测试类
package com.atguigu.spring.testdemo;import com.atguigu.spring.config.SpringConfig;
import com.atguigu.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void testService2(){//加载配置类ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();}}