Spring是轻代码而重配置的框架, 配置比较的繁重, 影响开发效率, 所以使用注解开发是一种趋势, 注解代替xml配置, 大大提高了开发效率
也就是不止是spring, 现在基本所有的框架都有两套配置, 一套是xml文件配置, 一套是注解配置
原始注解也就是, 最早出现的一套注解, 主要是替代bean标签的配置
@Component —>使用在类上用于实例化Bean
@Controller ----> 使用在web层类上用于实例化Bean
@Service -----> 使用在service层类上用于实例化Bean
@Repository-----> 使用在dao层类上用于实例化Bean
@Autowired -----> 使用在字段上用于根据类型依赖注入
@Qualifier -----> 是按照id的值从容器中进行匹配,但是主要结合@Autowired一起使用用于根据名称进行依赖注入
@Resource------> 相当于@Autowired+@Qualifier,按照名称进行注入
@Value -------> 注入普通属性
通过组件扫描,直接通过EL表达式获取值:value(“${jdbc.username}”)
@Scope-------> 标注Bean的作用范围
@PostConstruct—>使用在方法上标注该方法是Bean的初始化方法
@PreDestroy------> 使用在方法上标注该方法是Bean的销毁方法
以上是注解的大概了解
下面对各个注解进行使用
Component : 组件的意思, 这是在类上使用的
Autowired : 自动注入, 在属性上使用
Qualifier : 配合Autowired, 用来确定注入的对象
这里有两个UserDao接口的实现类
@Component("userDaoMysql")
public class UserDaoMysqlImpl implements UserDao {
@Component("userDaoOrale")
public class UserDaoOraleImpl implements UserDao {
为这两个实现类用注解配置进入spring的容器内之后
这里有一个UserService接口的实现类
@Component("userService")
public class UserServiceImpl implements UserService {
/*
* 依赖的资源*/
@Autowired
@Qualifier("userDaoMysql")
private UserDao userDao;
同样将类配置进容器中, 并使用注解注入资源
测试
public class UserServiceTest {
@Test
public void testUserService(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) app.getBean("userService");
userService.save();
}
}
12月 15, 2021 6:34:04 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3796751b: startup date [Wed Dec 15 18:34:04 CST 2021]; root of context hierarchy
12月 15, 2021 6:34:04 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
12月 15, 2021 6:34:05 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
这是Mysql的实现方法
调用了持久层的方法
如果注入的依赖是Orale的那一个实现类
@Component("userService")
public class UserServiceImpl implements UserService {
/*
* 依赖的资源*/
@Autowired
@Qualifier("userDaoOrale")
private UserDao userDao;
12月 15, 2021 6:36:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3796751b: startup date [Wed Dec 15 18:36:19 CST 2021]; root of context hierarchy
12月 15, 2021 6:36:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
12月 15, 2021 6:36:20 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
这是Orale的实现方法
调用了持久层的方法
小结 : 说明注解可以替代xml来配置bean和注入依赖的资源, 但是在xml中记得要配置好组件扫描器, 也是在context的命名空间下.
细节点一 : 当使用注解配置来注入时, 属性的set方法可以省略不写.
细节点二 : 当注入时, 只写Autowired注解, 表示从spring容器中找属性的类型进行匹配.可以不用写Qualifier注解, 如果有搭配Qualifier就是根据id找到指定的实现类
<!--使用注解配置, 要配置包中组件扫描器-->
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
Controller : 控制器, web层
Service : 服务, service层
Repository : 仓库, dao层
这三个注解其实功能和Component是一样的, 是他的衍生注解
作用是用来区分dao层和service层和web层的bean