15.1@Autowired的使用
<!-- 配置文件:
标识类:给类打上标签,配置文件扫描全部,在通过
@Autowired为自动装配,需在扫码全部及标识类的时候使用
扫描
-->
扫描全部
<context:component-scan base-package="com.atguigu.spring">
<!-- 通过注解(expression)排除扫描-->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!-- 通过类型(expression)排除扫描-->
<!-- <context:exclude-filter type="assignable" expression="com.atguigu.spring.controller.UserController"/>-->
<!-- 扫描-->
</context:component-scan>
/*
测试类:
* @Autowired:实现自动装配的功能的注解
* 1、@Autowired注解能够标识的位置
* a>标识在成员变量上,此时不需要来设置成员变量的set方法
* b>标识在set方法上
* c>标识在为当前成员变量赋值的有参构造上
*/
@Test
public void test(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc-annotation.xml");
UserController userController = ioc.getBean(UserController.class);
userController.saveUser();
}
/* 成员变量标识 */
@Autowired
private UserService userService;