IOC 操作 Bean 管理(基于注解方式)
1、spring针对Bean管理中创建对象提供注解
(1)@Component 任何场景都能用
(2)@Service 用在service层
(3)@Controller web层
(4)@Repository 在持久层,dao层
上面的四个注解功能是一样的,都可以用来创建bean实例
2、基于注解方式实现对象创建
1、引入依赖
2、 开启组件扫描
<contextx:component-scan base-package="com.atguigu.spring5.dao,com.atguigu.spring5.service"/>
<contextx:component-scan base-package="com.atguigu"/>
3、 创建类,在类上面添加创建对象注解
//在注解里面,value属性值可以省略不写
//默认值是类名称,首字母小写
@Service(value = "userService")
public class UserService {
public void add(){
System.out.println("service add()");
}
}
4.开启组件扫描细节配置
<context:component-scan base-package="com.atguigu.spring5" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.atguigu.spring5">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
5、基于注解方式实现属性注入
1.@Autowired:根据属性类星星进行自动装配
2.@Qualifier:根据属性名称进行注入
3.@Resource:可以根据类型注入,也可以根据名称注入
4.@Value:注入普通类型属性
6、完全注解开发
@Configuration
@ComponentScan(basePackages = {"com.atguigu.spring5"})
public class springConfig {
}