Spring常见注解

@Autowired
@Resource
@Component:类加上@Component注解,即表明此类是bean
@Aspect 注解表示这是一个切面
@Around(value = "execution(* xxx.ProductService.*(..))") 表示对xxx.ProductService 这个类

import com.how2java.pojo.Category;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
    @Autowired
    Category c;
 
    @Test
    public void test(){
        System.out.println(c.getName());
    }
}

修改TestSpring, 并运行

  1. @RunWith(SpringJUnit4ClassRunner.class)
    表示这是一个Spring的测试类

  2. @ContextConfiguration("classpath:applicationContext.xml")
    定位Spring的配置文件

  3. @Autowired
    给这个测试类装配Category对象

  4. @Test
    测试逻辑,打印c对象的名称

posted @ 2019-11-07 16:21  Lan_ht  阅读(96)  评论(0编辑  收藏  举报