spring第四篇

李俊----互联网技术传播者      2022-04-27     751

关键词:

在spring第三篇中介绍了bean元素属性 在第四篇中介绍spring注入的方式

 1 set方法注入

    建立一个User类 创建私有的属性 set  get 方法  重写toString方法 代码如下:

package bean;

public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}

}

  书写配置文件   代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="bean.User">
<property name="name" value="lijun"></property>
<property name="age" value="39"></property>
</bean>
</beans>

测试代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="bean.User">
<property name="name" value="lijun"></property>
<property name="age" value="39"></property>
</bean>
</beans>

测试结果如下图

2 构造函数注入

  在User类里面添加有参构造函数 代码是:

public User(String name,int age){
System.out.println("这是string");
this.name=name;
this.age = age;
}

配置文件写成如下人代码:

<bean name="user1" class="bean.User">
<constructor-arg name="name" value="lijun"></constructor-arg>
<constructor-arg name="age" value="39"></constructor-arg>
</bean>

constructor-arg是构造函数注入的关键词,

测试代码如下

@Test
public void func2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/lijun/demo/applicationContext.xml");

User u = (User)ac.getBean("user1");
System.out.println(u);
}

因为参数一样  所以测试结果会跟上次set测试结果一样。

springboot自学教程|第四篇:springboot整合mybatis

  引入依赖  1:在pom文件引入mybatis-spring-boot-starter的依赖:1<dependency>2<groupId>org.mybatis.spring.boot</groupId>3<artifactId>mybatis-spring-boot-starter<artifactId>4<version& 查看详情

第四篇:模块与包

第四篇:模块与包  PYTHON-模块定义搜索路径PYTHON-包相对导入&绝对导入  查看详情

开始写游戏---第四篇

开始写游戏---第四篇    今天完成了商店的物品展示功能。  写了一个物品展示面板组件:      这是代码:      使用的时候这么使用:          额。。。感觉使用有点麻烦,我又提供了... 查看详情

java后端开发第四篇:springboot中thymeleaf入门(代码片段)

thymeleaf是springboot中所支持的一种模板引擎。入门级使用如下:pom.xml中引入依赖:<!--引入模板引擎--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thy 查看详情

第四篇css

在标签上设置style属性:background-color:#2459a2;height:48px;。。。编写CSS样式:1.在标签的属性中编写1<!DOCTYPEhtml>2<htmllang="en">3<head>4<metacharset="UTF-8">5<title>Title</title>6</head 查看详情

第四篇javascript

一、正则表达式提示:此专题需要多轮复习反复的加深和理解正则表达式的两种用法:1)regexp.xxx(string);2)string.yyy(regexp);验证用户输入的手机号格式是否合法varregexp=/^1[3-8]d{9}$/;varinput="15012345678";console.log(regexp.test(input))正则表达式... 查看详情

小白入门深度学习|第四篇:配置pytorch环境

查看详情

第四篇其他软件的安装方法

这篇是预留的一篇文章,主要用来记录其他软件的安装方法。占个位置。 查看详情

第四篇alexnet——网络实战

文章目录摘要1项目结构2划分训练集和测试集3计算mean和Standard3.1标准化的作用3.2归一化的作用4训练4.1导入项目使用的库4.2设置随机因子 查看详情

第四篇alexnet——网络实战

文章目录摘要1项目结构2划分训练集和测试集3计算mean和Standard3.1标准化的作用3.2归一化的作用4训练4.1导入项目使用的库4.2设置随机因子 查看详情

coreanimation文档翻译(第四篇)

CoreAnimation文档翻译(第四篇)让Layer的content动画起来核心动画的基础接口以及为拥有Layer的View做的动画扩展接口,使得为Layer制作复杂动画变得简单化。例如改变Layer的frame的size、改变Layer在屏幕上的position、应用旋转transform、或... 查看详情

css样式第四篇

?针对现在网站的图片过大问题,可以用相应的工具进行压缩,并且可对图片进行切割处理。1.如果一个页面的图片过大,可以对其切割,代码<imgsrc=”1.jpg”><imgsrc=”2.jpg”><imgsrc=”3.jpg”>,如这样排列的图片不是块... 查看详情

第四篇express安装esasticsearch

1.首先,我们创建一个Express应用程序!我将使用express.js生成器。npminstall-gexpress-generatorexpress./autocompletercdautocompleternpminstall2.让我们准备elasticsearch。下载elasticsearch,并将其解压到文件系统的某个位置。然后运行cdlocationOfElasticsearc... 查看详情

第四篇:记录相关操作(代码片段)

一介绍MySQL数据操作:DML========================================================在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括使用INSERT实现数据的插入UPDATE实现数据的更新使用DELETE实现数据的删除使用SELECT查询数据... 查看详情

第四篇集合与容器(代码片段)

packagecom.zzp.demo.myCollection;/****用于HashMap*@authorjava**/publicclassNode2 inthash; Objectkey; Objectvalue; Node2next;  packagecom.zzp.demo.myCollection;/****自定义HashMap*@authorjava**/public 查看详情

django学习~第四篇

django表单  1 今天继续来学学django的表单    首先介绍下http的方法,这是最基本的    GET方法GET一般用于获取/查询资源信息,以?分割URL和传输数据,多个参数用&连接,login.action?name=hyddd&... 查看详情

第四篇io流技术(代码片段)

packagecom.zzp.demo01;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;/****第一个程序理解操作步骤*1、创建源*2、选择流*3、操作*4、释放资源* 查看详情

abp异常处理第四篇(代码片段)

异常ABP异常分类 ABP的异常基类源代码ABPException的源代码如下///<summary>///BaseexceptiontypeforthosearethrownbyAbpsystemforAbpspecificexceptions.///</summary>[Serializable]publicclassAbpException:Exceptio 查看详情