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

木舟0基础学习Java的第二十九天(Spring,Spring的属性注入(xml,注解))

Spring(Spring FrameWork)

Spring是一个开源框架,最早由Rod Johnson发起 Spring为简化企业级开发而生 使用Spring开发可以将Bean对象交给Spring容器来管理 这样使得很多复杂的代码在Spring中开发会变得优雅简洁 有效的降低代码的耦合度 极大的方便项目的后期维护 升级 和扩展

官网:Https://spring.io

概述:

Spring是一个轻量级控制反转(IOC)和面向切面(AOP)为核心的容器框架

正转
Person.java

javac

Person.class

new

Person对象

第一个Spring程序(IOC案例)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd">
<!--下面这句话等同于 Users user=new User();告诉Spring这个com.spring.pojo.Users类交给你管理了--><bean id="user" class="com.spring.pojo.Users"></bean></beans>
@Data
public class Users implements Serializable {private int id;private String uname;private String pwd;
}
public class Demo {//控制正转的方式@Testpublic void run1(){Users user = new Users();user.setId(1);user.setUname("张三");user.setPwd("333");System.out.println(user);}//Spring中的控制反转技术(简称IOC技术)@Testpublic void run2(){//加载spring.xml配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");//通过id从spring容器的对象缓存池中取bean(Users)对象//ac.getBean("user");//这时对象会被自动提升为Object对象 需要强转回来Users user =(Users) ac.getBean("user");user.setId(1);user.setUname("妮妮");user.setPwd("0105");System.out.println(user);}
}

基于xml映射文件中 bean标签的属性注入

@Data
public class Users implements Serializable {private int id;private String uname;private String pwd;//持有Clazz 引用数据类型private Clazz clazz;private Account account;
}
public class Clazz implements Serializable {
private String cname;
private int room;public Clazz() {}public Clazz(String cname, int room) {this.cname = cname;this.room = room;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public int getRoom() {return room;}public void setRoom(int room) {this.room = room;}@Overridepublic String toString() {return "Clazz{" +"cname='" + cname + '\'' +", room=" + room +'}';}
}
/*其他数据类型的注入*/
public class Data implements Serializable {private String[] arr;private List<Integer> list;private Set set;private Map map;private Properties properties;public Data() {}public Data(String[] arr, Properties properties, Map map, Set set, List<Integer> list) {this.arr = arr;this.properties = properties;this.map = map;this.set = set;this.list = list;}public String[] getArr() {return arr;}public void setArr(String[] arr) {this.arr = arr;}public List<Integer> getList() {return list;}public void setList(List<Integer> list) {this.list = list;}public Set getSet() {return set;}public void setSet(Set set) {this.set = set;}public Map getMap() {return map;}public void setMap(Map map) {this.map = map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "Data{" +"arr=" + Arrays.toString(arr) +", list=" + list +", set=" + set +", map=" + map +", properties=" + properties +'}';}
}
/*账户*/
public class Account {private int aid;private long money;public Account() {}public Account(int aid, long money) {this.aid = aid;this.money = money;}public int getAid() {return aid;}public void setAid(int aid) {this.aid = aid;}public long getMoney() {return money;}public void setMoney(long money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"aid=" + aid +", money=" + money +'}';}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName"
>
<!--全局自动注入↑  下面的每个bean都会自动注入--><!--下面这句话等同于 Users user=new User();告诉Spring这个com.spring.pojo.Users类交给你管理了--><bean id="user" class="com.spring.pojo.Users" scope="prototype"><!--scope="singleton"单态模式 默认是这个 从bean取出的对象为 同一 对象--><!--scope="prototype"原型模式 从bean取出的对象为 不同 对象--><!--这个标签不仅可以声明类 也可以给该类赋值--><property name="id" value="1"></property><property name="uname" value="小依"></property><!--等同于user.setUname("小依");--><property name="pwd" value="111"></property></bean>
<!--通过set方法给属性注入值--><bean id="clazz" class="com.spring.pojo.Clazz"><property name="cname" value="计算机应用科学"></property><property name="room" value="101"></property></bean><!--通过构造方法给属性注入值相当于new Clazz("英语",201)-->
<bean id="claz" class="com.spring.pojo.Clazz"><constructor-arg name="cname" value="英语"></constructor-arg><constructor-arg name="room" value="201"></constructor-arg>
</bean>
<!--给引用数据类型 持有的Clazz注入值--><bean id="user2" class="com.spring.pojo.Users" scope="prototype"><!--scope="singleton"单态模式 默认是这个 从bean取出的对象为 同一 对象--><!--scope="prototype"原型模式 从bean取出的对象为 不同 对象--><!--这个标签不仅可以声明类 也可以给该类赋值--><property name="id" value="2"></property><property name="uname" value="小贰"></property><property name="pwd"><value/></property><!--<value/>或者<null/>空值注入--><property name="clazz" ref="clazz"></property><!--ref="clazz"关联clazz--></bean><bean id="data" class="com.spring.pojo.Data"><property name="arr"><array><value>北京</value><value>上海</value><value>广州</value><value>深圳</value></array></property><property name="list"><list><value>10</value><value>20</value><value>30</value><value>40</value></list></property><property name="set"><set><value>庐山</value><value>衡山</value><value>华山</value><value>庐山</value></set></property><property name="map"><map><entry key="北京" value="010"></entry><entry key="广州" value="021"></entry><entry key="上海" value="020"></entry></map></property><property name="properties"><props><prop key="省会">奉天</prop><prop key="菜品">烧烤</prop><prop key="特色">洗浴</prop></props></property></bean>
<!--局部自动注入--><bean id="account" class="com.spring.pojo.Account"><property name="aid" value="11"></property><property name="money" value="12000"></property></bean><bean id="user3" class="com.spring.pojo.Users" scope="prototype" autowire="byType"><!--autowire=""自动注入--><!--autowire="byName"根据名字account自动注入 名字对应Users中持有的名称private Account account;--><!--autowire="byType"根据类型com.spring.pojo.Account自动注入 注意:不能同时有两个com.spring.pojo.Account--><property name="id" value="3"></property><property name="uname" value="小山"></property><property name="pwd" value="333"></property><property name="clazz" ref="clazz"></property><!--ref="clazz"关联clazz--></bean>
</beans>

基于注解的注入

<?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 https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- bean definitions here --><!--以下的标签是在当前文件被加载之后,扫描指定的包下所有的实体类--><context:component-scan base-package="com.spring.pojo"></context:component-scan></beans>
@Component/*这个注解是告诉spring容器,当你扫描com.spring.pojo包下所有的实体类时
捕捉该@Component注解 将含有这个注解的类 加载到容器中 并创建对象。。。*/
/*该注解相当于 Users users=new Users(); 当前并没有指定该类名称 默认首字母小写为该类名称 users*/
/*@Component("u") 指定该类名称为u*/
//@Scope("singleton")//默认 指定该类为单例模式
@Scope("prototype")//指定该类为多例(原形)模式
public class Users implements Serializable {@Value("2")private int id;@Value("小贰")//给uname 赋值private String uname;@Value("222")private String pwd;//@Resource//引用类型的数据 根据bean的类型自动注入@Autowired//自动注入 同@Resource一样 建议使用private Clazz clazz;//@Qualifier//声明注入 根据指定的bean的名字自动注入 要与@Autowired搭配使用 建议不用@Resource//引用类型的数据 根据其类型自动注入private Account account;public Users() {}public Users(int id, String uname, String pwd, Clazz clazz, Account account) {this.id = id;this.uname = uname;this.pwd = pwd;this.clazz = clazz;this.account = account;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUname() {return uname;}public void setUname(String uname) {this.uname = uname;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public Clazz getClazz() {return clazz;}public void setClazz(Clazz clazz) {this.clazz = clazz;}public Account getAccount() {return account;}public void setAccount(Account account) {this.account = account;}@Overridepublic String toString() {return "Users{" +"id=" + id +", uname='" + uname + '\'' +", pwd='" + pwd + '\'' +", clazz=" + clazz +", account=" + account +'}';}
}

 

/*账户*/
@Component(value = "account")//括号内的value可以省略
public class Account {@Value("101")private int aid;@Value("12000")private long money;public Account() {}public Account(int aid, long money) {this.aid = aid;this.money = money;}public int getAid() {return aid;}public void setAid(int aid) {this.aid = aid;}public long getMoney() {return money;}public void setMoney(long money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"aid=" + aid +", money=" + money +'}';}
}
public class Demo {@Testpublic void run1(){ApplicationContext ca = new ClassPathXmlApplicationContext("Spring.xml");Users users =(Users) ca.getBean("users");users.setId(1);users.setUname("小依");users.setPwd("111");users.setClazz(new Clazz("计算机",101));users.setAccount(new Account(1,12000));System.out.println(users);}@Testpublic void run2(){ApplicationContext ca = new ClassPathXmlApplicationContext("Spring.xml");Users users =(Users) ca.getBean("users");System.out.println(users);}
}

 

Spring的IOC(注解)

@Component:表示当前修饰的类交给Spring容器管理 修饰一个类 将其交给Spring容器

与@Component相同功能的 还有三个衍生注解 都是用来修饰类:

@Repository:添加在mapper的接口类上(数据访问层)

@Service:添加在Service实现类上(业务层)

@Controller:添加在Controller类上(控制层)

@Configration:添加在用于配置信息的类上(SpringBoot中自动配置)

使用场景:

XML:可以使用在任何场景 开发中用来注入框架实例 基于xml操作的是.class文件

注解:有些地方用不了 比如这个类不是自己写的(注解必须写在源代码上) 开发中用来注入自己写的Java类 基于注解操作的是源代码

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MinGW版)
  • leetcode第十四题:最长公共前缀
  • CTC loss 博客转载
  • 鸿蒙【项目打包】- .hap 和 .app;(测试如何安装发的hap包)(应用上架流程)
  • 【机器学习】--- 自然语言推理(NLI)
  • 巨人网络参展云栖大会,两款“游戏+AI”自研大模型应用首发
  • docker之自定义镜像上传至阿里云
  • 山体滑坡检测系统源码分享
  • 力扣(leetcode)每日一题 2374 边积分最高的节点
  • Godot游戏如何提升触感体验
  • 大佬,简单解释下“嵌入式软件开发”和“嵌入式硬件开发”的区别
  • Mistral AI 又又又开源了闭源企业级模型——Mistral-Small-Instruct-2409
  • 【图像压缩与重构】基于标准+改进BP神经网络
  • Vue 3:实现页面返回上一页的功能
  • AI驱动TDSQL-C Serverless 数据库技术实战营-ai学生选课系统数据分析
  • @angular/forms 源码解析之双向绑定
  • [Vue CLI 3] 配置解析之 css.extract
  • create-react-app做的留言板
  • Just for fun——迅速写完快速排序
  • MQ框架的比较
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • vue脚手架vue-cli
  • 阿里云购买磁盘后挂载
  • 工程优化暨babel升级小记
  • 算法系列——算法入门之递归分而治之思想的实现
  • 一、python与pycharm的安装
  • #Linux(权限管理)
  • #pragma once与条件编译
  • #pragma pack(1)
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (LeetCode C++)盛最多水的容器
  • (poj1.3.2)1791(构造法模拟)
  • (二)原生js案例之数码时钟计时
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (七)Activiti-modeler中文支持
  • (四)库存超卖案例实战——优化redis分布式锁
  • (算法)大数的进制转换
  • ... 是什么 ?... 有什么用处?
  • .net core 6 集成和使用 mongodb
  • .net core 连接数据库,通过数据库生成Modell
  • .net core开源商城系统源码,支持可视化布局小程序
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .Net组件程序设计之线程、并发管理(一)
  • /var/lib/dpkg/lock 锁定问题
  • ??eclipse的安装配置问题!??
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • @autowired注解作用_Spring Boot进阶教程——注解大全(建议收藏!)
  • @我的前任是个极品 微博分析
  • [ IO.File ] FileSystemWatcher
  • [ 常用工具篇 ] AntSword 蚁剑安装及使用详解
  • []sim300 GPRS数据收发程序