一、前言
前面更新了Spring5总述的(一)和(二),(二)讲述的是IOC的概念以及基于xml配置文件方式实现Bean管理操作。(二)之后还有大量内容需要补充,所以留下了(三)和(四)的位置待以后创作。基于以上原因,本次便以(五)为顺序进行博客的创作。
二、摘要
本博客通过文字解释和代码结合进行说明,主要讲解内容为 :
1.注解的概念
2.基于注解方式实现对象的创建
3.基于注解方式实现属性的注入
4.完全注解开发
三、正文
1、注解概念
1.什么是注解?
- 注解是代码特殊标记,格式:@注解名称(属性名称 = 属性值 , 属性名称 = 属性值 , …)
- 使用注解,注解可以作用在类上面,方法上面,属性上面
- 目的:简化xml配置
2.Spring针对Bean管理中创建对象提供的注解有
1. @Component
2. @Service
3. @Controller
4. @Repository
上面的四个注解的功能都是一样的,都可以用来创建Bean实例
2、基于注解方式实现对象的创建
- 第一步:引入依赖(如何引入依赖(一)中有所讲解)
- 创建类,并添加相应注解
//value就类似于xml文件中的id属性
@Component(value = "userService")
public class UserService {
public void add(){
System.out.println("service add.......");
}
}
- 在xml配置文件中开启组件扫描
- 如果扫描多个包,多个包用逗号隔开,或者直接扫描他们共同的上层目录
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Xml文件创建成功之后,加入context命名空间-->
<!--service代表包名,上面的UserService类就在这个包中-->
<context:component-scan base-package="service"></context:component-scan>
</beans>
- 创建测试类
public class Testservice {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService service = context.getBean("userService", UserService.class);
service.add();
}
}
补充说明:
组件扫描的细节配置
<!--不能搜索到service包下的@Controller注解的类-->
<context:component-scan base-package="service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--use-default-filters的默认值为true,即扫描整个包;设置为false则是代表不扫描整个包-->
<context:component-scan base-package="service" use-default-filters="false">
<!--只能搜索到service包下的@Controller注解的类-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
3、基于注解方式实现属性的注入
- @AutoWired:根据属性类型进行自动装配、注入
- @Qualifier:根据属性名称进行注入,此属性的使用必须和AutoWired一起使用
- @Resource:可以根据类型注入,也可以根据名称注入
- @Value:注入普通类型的属性
(具体实现效果在第四点完全注解开发中演示)
//name="abc"
@Value(value = "abc")
private String name;
//实现了这样的过程:userDaolmpl = new UserDao();
@Autowired
@Qualifier(value = "user")
private UserDaolmpl userDao;
4、完全注解开发
1.创建配置类,代替xml配置文件
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = "dao")//代表需要扫描的包
@ComponentScan(basePackages = "service")
public class SpringConfig {
}
2.创建UserDaolmpl类
@Repository(value = "user")
public class UserDaoImpl {
public void print() {
System.out.println("dao add......");
}
}
3.创建UserService类
@Component(value = "userService")
public class UserService {
@Value(value = "abc")
private String name;
//@Autowired单独使用效果相同
@Autowired
@Qualifier(value = "user")
private UserDaoImpl userDao;
public void add(){
System.out.println("service add.......");
userDao.print();
System.out.println(name);
}
}
4.创建测试类
- 注意观察该类下面的第一行代码,与前面xml配置文件对比学习
public class Testservice {
@Test
public void testadd1(){
//与xml配置文件使用的不同点,实现类的使用不同
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService service = context.getBean("userService", UserService.class);
service.add();
}
}
测试结果
service add.......
dao add......
abc
❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
若对Spring基础知识感兴趣的可以关注一下博主,我会持续更新Spring基础知识(一边学习一边记录),一起进步,有错误的地方也可以在评论区指出来喔,谢谢大家啦!!!