Spring-IoC-完全注解开发

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

package com.orzjiangxiaoyu.spring.test2;

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

/**
 * @author orz
 * @create 2020-08-16 22:55
 */
@Configuration//作为配置类,代替xml配置文件
@ComponentScan(basePackages = {"com.orzjiangxiaoyu.spring"})
public class SpringConfig  {

}

(2)创建对象

package com.orzjiangxiaoyu.spring.test2;

import org.springframework.stereotype.Service;

/**
 * 完全注解开发
 * @author orz
 * @create 2020-08-16 22:53
 */
@Service(value = "userService1")
public class UserService {
}

(3)编写测试类

package com.orzjiangxiaoyu.spring.test2;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author orz
 * @create 2020-08-16 22:54
 */
public class Test1 {
    @Test
    public void test1()
    {
        ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService1", UserService.class);
        System.out.println(userService);
    }
}

(4)结果

com.orzjiangxiaoyu.spring.test2.UserService@72d6b3ba

 

posted @ 2020-08-16 23:06  orz江小鱼  阅读(137)  评论(0编辑  收藏  举报