我们在平时编写代码时常常用到给方法中多个参数的赋值,那么在Spring中是如何实现的呢。
案例:给方法中多个参数赋值
1、创建接口GreetingService,代码如下
package cn.csdn.service;
public interface GreetingService {
public void say();
}
解析:这个接口中只有一个方法
2、创建接口实现类GreetingServiceImpl,代码如下
package cn.csdn.service;
public class GreetingServiceImpl implements GreetingService {
/**私有属性*/
private String say;
/**在定义一个私有的属性*/
private String name;
/**
* IOC依赖注入的方式
* 2、通过构造器注入
* */
public GreetingServiceImpl(String name,String say){
this.name=name;
this.say=say;
}
@Override
public void say() {
System.out.println("你给"+name+"打的招呼是:"+say);
}
}
解析:这个接口实现类中有一个有参构造器,其中有两个参数,需要给这两个参数注入值
3、创建Spring配置文件applicationContext.xml
这里有两种方法为参数赋值:
1、利用索引方法,代码如下:
<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置一个bean 指明bean id class property属性 value属性值 加载这个文件的时候 进行初始化 (需要根据bean的配置)-->
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<!-- 通过构造器参数的索引位置匹配方法 索引位置从0开始 -->
<constructor-arg index="0">
<value>Zhang_Di</value>
</constructor-arg>
<constructor-arg index="1">
<value>你好!</value>
</constructor-arg>
</bean>
</beans>
注意:通过构造器参数的索引位置匹配方法时,索引位置是从0开始
2、利用参数类型进行注入,代码如下:
<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置一个bean 指明bean id class property属性 value属性值 加载这个文件的时候 进行初始化 (需要根据bean的配置)-->
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<!-- 通过构造器的参数类型匹配方法进行注入 -->
<constructor-arg type="java.lang.String">
<value>Zhang_Di</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>你好!</value>
</constructor-arg>
</bean>
</beans>
注意:因为两个参数的类型都为String类型,所以在赋值时是按顺序赋值,若两参数类型不同,则会自动付给相应类型的值
3、两种方法的运行结果相同:
你给Zhang_Di打的招呼是:你好!
----------------------------------------------------------------
以上属个人理解,若有不足,请各位高手指点,谢谢..
分享到:
相关推荐
Boot入门初体验是一门针对初学者准备的课程,讲解了如何一步一步进入Spring?Boot开发,课程采用循序渐进的方式,一个知识点一个知识点的讲解和学习,让初学者不知不觉的掌握各种开发技能,课程包含了主流框架搭建、...
spring-security初体验demo
Spring第二章Spring第二章Spring第二章Spring第二章Spring第二章Spring第二章Spring第二章Spring第二章
spring拦截器,高级参数绑定,controller返回值
主要介绍了Spring boot + thymeleaf 后端直接给onclick函数赋值的实现代码,需要的朋友可以参考下
Spring Cloud是一个分布框架 Spring Cloud里边包括了目前最新的所有组件共21个
搭建Spring开发环境 Bean的常用配置 三种模式实现依赖(以service调用dao举例) 在serivce里面的类创建dao里面类的对象,调用方法 用工厂模式实现,service先通过工厂创建出来的dao,再调用其方法(面向接口编辑)...
springSecurity 实现登陆验证、传参,包括源代码和MYSQL的建库脚本。 传参的功能可以实现记录登陆之前打开的页面,登陆之后自动跳转到之前打开的页面。
NULL 博文链接:https://cuics-100.iteye.com/blog/1535399
主要介绍了Spring Cloud如何使用Feign构造多参数的请求,以GET以及POST方法的请求为例进行讲解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,
介绍了SpringCloud组件的使用
此文章是记录博主在学习springsecurity时的笔记 第一步 创建一个SpringBoot工程,勾选上需要的依赖。 第二步 创建一个名为securityconfig的配置类,该类基础了父类WebSecurityConfigurerAdapter(提供用于创建...
主要介绍了springboot 使用yml配置文件给静态变量赋值教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
该项目是跟着尚硅谷周阳的Springcloud2020做出来的,里面涉及传统的 eureka、hystrix、ribbon,更是...另外是推荐小伙伴们还是得多看官网的文档,视频只是引路人,很多东西是需要通过自己总结说出来才算正在掌握了的。
Spring源码深度解析第二版
非常清晰的spring in action 第二版中文版 欢迎下载 spring入门与提高的经典书籍
动态修改参数cron的值来实现Spring自带定时器动态的执行任务
[编程] 手把手教你如何优雅编写自己自定义在SpringCloud中配置参数类
Spring攻略第二版,Spring Recipes 2nd