当前位置: 首页 > news >正文

Spring基于xml半注解开发

目录

@Component的使用

依赖注解的使用

非自定义Bean的注解开发


@Component的使用

基本Bean注解,主要是使用注解的方式替代原有的xml的<bean>标签及其标签属性的配置,使用@Component注解替代<bean>标签中的id以及class属性,而对于是否延迟加载或是Bean的作用域,则是其他注解

xml配置

注解

描述

<bean scope="">

@Scope

在类上或使用了@Bean标注的方法上,标注Bean的作用范围,取值为singleton或prototype

<bean lazy-init="">

@Lazy

在类上或使用了@Bean标注的方法上,标注Bean是否延迟加载,取值为true或false

<bean init-method="">

@PostConstruct

在方法上使用,标注Bean的实例化后执行的方法

<bean destroy-method="">

@PreDestroy

在方法上使用,标注Bean的销毁前执行方法

下面就是基于注解的测试案例

首先需要开启自动扫描注解功能,这个功能还是需要在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 https://www.springframework.org/schema/context/spring-context.xsd"><!--开启自动扫描--><context:component-scan base-package="com.zmt"/>
</beans>
@Component("userService")
@Lazy(true)//开启懒加载
@Scope("singleton")//单例模式
public class UserServiceImpl implements UserService {public UserServiceImpl() {System.out.println("UserService被构造");}@PostConstructprivate void init(){System.out.println("执行init方法");}@PreDestroyprivate void destroy(){System.out.println("执行销毁方法");}
}

测试代码 

public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");Object bean = context.getBean("userService");System.out.println(bean);context.close();}
}

执行结果如下

UserService被构造
执行init方法
com.zmt.service.impl.UserServiceImpl@45afc369
执行销毁方法

为了方便区分不同的业务层,@Component注解又衍生了三个注解

  • @Service:Component的派生注解,多添加在Service的实现类上
  • @Controller:Component的派生注解,多添加在Controller类上
  • @Repository:Component的派生注解,多添加在Dao实现类上

依赖注解的使用

Bean的依赖注入的注解,主要是替代xml中的<property>标签中的注入操作

<bean id="" class=""><property name="" ref=""/><property name="" value=""/>
</bean>

Spring提供的注解如下,用于Bean内部进行属性注入的

属性注入注解

描述

@Value

使用在字段或方法上,用于注入普通数据

@Autowired

使用在字段或方法上,用于根据类型(byType)注入引用数据

@Qualifier

使用在字段或方法上,结合@Autowired使用,根据名称注入

@Resource

使用在字段或方法上,根据类型或名称进行注入

这些注解的工作原理实际上是通过暴力反射然后赋值,因此不需要set方法,但是添加了set方法在set方法上添加注解也可以使用。

下面是简单的使用案例

@Value该注解可以对普通数据类型进行赋值,一种是直接指定需要注入的值,一种是通过占位符读取需要注入的值信息

<?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 https://www.springframework.org/schema/context/spring-context.xsd"><!--开启自动扫描--><context:component-scan base-package="com.zmt"/><!--将test.txt文件加载到Spring中,供赋值使用--><context:property-placeholder location="classpath:test.txt"/>
</beans>

@Component("userService")
public class UserServiceImpl implements UserService {
//    @Value("zhangsan")@Value("${name}")private String name;@Overridepublic void show() {System.out.println(name);}
}

以上两种都可以将值赋值给name变量,但前者使用的意义不大,因此通常我们使用后者去读取配置文件中的值。


@Autowired的使用默认是根据类型注入,但如果相同类型存在多个,则根据名称进行注入,但是如果不存在属性变量名的Bean对象,那么注入失败

@Component("userService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Autowiredpublic void setUserDao(UserDao userDao) {this.userDao = userDao;}//以上两种写一个即可@Overridepublic void show() {}
}

@Qualifier需要搭配@Autowired注解使用,特定指定在多个相同类型的Bean对象时,注入哪个名称的bean对象

@Component("userService")
public class UserServiceImpl implements UserService {@Autowired@Qualifier("userDao")private UserDao userDao2;@Overridepublic void show() {}
}

这里即使属性变量名为userDao2但是实际上注入的还是名为userDao的bean对象。


@Resource既可以类型注入也可以名称注入,如果指定名称,那么只能根据名称注入

@Component("userService")
public class UserServiceImpl implements UserService {@Resource(name = "userDao")private UserDao userDao2;//实际上注入的是名称为userDao的bean对象@Overridepublic void show() {}
}

@Autowired的扩展使用,实际上该注解不仅仅可以添加在set方法上,可以添加在任何方法上,下面是一个测试案例

@Component("userService")
public class UserServiceImpl implements UserService {@Overridepublic void show() {}@Autowiredpublic void xxx(UserDao userDao){System.out.println("xxx:"+userDao);}@Autowiredpublic void yyy(List<UserDao> userDaoList){System.out.println("yyy:"+userDaoList);}
}

运行结果如下

非自定义Bean的注解开发

对于非自定义的Bean我们需要使用工厂来进行加载,需要使用@Bean注解,如果需要参数,对于基本类型需要@Value注解,对于引用类型,只要Spring中存在,可以直接根据类型注入。

@Component
public class OtherBean {@Beanpublic DataSource dataSource(@Value("${name}") String name){DataSource dataSource = new DataSource();System.out.println(name);return dataSource;}
}
http://www.lryc.cn/news/257830.html

相关文章:

  • 功能测试,接口测试,自动化测试,压力测试,性能测试,渗透测试,安全测试,具体是干嘛的?
  • oracle 下载java之前版本
  • LLM之Agent(四)| AgentGPT:一个在浏览器运行的Agent
  • AGM离线下载器使用说明
  • viple与物理机器人(一):线控模拟
  • Appium 并行测试多个设备
  • 高防IP是什么? 防护CC 对抗DDOS
  • 使用消息队列遇到的问题—kafka
  • Linux系统---基于Pipe实现一个简单Client-Server system
  • CentOS7安装最新版本git
  • Java项目-瑞吉外卖Day3
  • Java集合框架之争:ArrayList vs LinkedList
  • 一个用于处理嵌入式系统中的 NAND Flash 存储器的工具 `kobs-ng`
  • 【小白专用】MySQL查询数据库所有表名及表结构其注释
  • 数据库中常用的锁
  • 关于对向量检索研究的一些学习资料整理
  • 软件开发流程分析
  • 017 OpenCV 向量机SVM
  • Qt练习题
  • 文本转图像 学习笔记
  • 开源CDN软件GoEdge —— 筑梦之路
  • 基于SpringBoot+Vue会员制医疗预约服务管理信息系统(Java毕业设计)
  • 【Linux | 编程实践】防火墙 (网络无法访问)解决方案 Vim常用快捷键命令
  • 仅 CSS 阅读进度条
  • 深度剖析中国居民消费价格指数CPI数据可视化案例-Python可视化技术实现(附完整源码)【数据可视化项目案例-16】
  • SpringBoot——嵌入式 Servlet容器
  • 王炸升级!PartyRock 10分钟构建 AI 应用
  • 文件管理和操作工具Path Finder mac功能介绍
  • 转换 pytorch 格式模型为 caffe格式模型 pth2caffemodel
  • 【S32DS RTD实战】-1.3-S32K3工程生成S19,BIN,Hex文件,以及Post-build steps的妙用