首页 > 其他分享 >spring对象的获取及属性赋值方式

spring对象的获取及属性赋值方式

时间:2023-09-18 17:56:25浏览次数:34  
标签:String spring id Person context org 赋值 public 属性

1、通过bean的id获取IOC容器中的对象

SpringDemoTest.java

import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemoTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person person =(Person) context.getBean("person");
System.out.println(person);
}
}

2、通过bean的类型获取对象

SpringDemoTest.java

import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemoTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person bean = context.getBean(Person.class);
System.out.println(bean);
}
}
注意:通过bean的类型在查找对象的时候,在配置文件中不能存在两个类型一致的bean对象,如果有的话,可以通过如下方法
SpringDemoTest.java
import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemoTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person);
}
}

3、通过构造器给bean对象赋值

spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="person" class="com.mashibing.bean.Person">
<property name="id" value="1"></property>
<property name="name" value="单强"></property>
<property name="age" value="32"></property>
<property name="gender" value="男"></property>
</bean>
<!--给person类添加构造方法-->
<bean id="person1" class="com.mashibing.bean.Person">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="qiangShan"></constructor-arg>
<constructor-arg name="age" value="31"></constructor-arg>
<constructor-arg name="gender" value="nan"></constructor-arg>
</bean>
<!--在使用构造器赋值的时候可以省略name属性,但是此时就要求必须严格按照构造器参数的顺序来填写了-->
<bean id="person2" class="com.mashibing.bean.Person">
<constructor-arg value="2"></constructor-arg>
<constructor-arg value="yongfengQin"></constructor-arg>
<constructor-arg value="51"></constructor-arg>
<constructor-arg value="nv"></constructor-arg>
</bean>
<!--如果想不按照顺序来添加参数值,那么可以搭配index属性来使用-->
<bean id="person3" class="com.mashibing.bean.Person">
<constructor-arg value="wenyingJiang" index="1"></constructor-arg>
<constructor-arg value="3" index="0"></constructor-arg>
<constructor-arg value="nan" index="3"></constructor-arg>
<constructor-arg value="30" index="2"></constructor-arg>
</bean>
<!--当有多个参数个数相同,不同类型的构造器的时候,可以通过type来强制类型-->
<bean id="person4" class="com.mashibing.bean.Person">
<constructor-arg value="5"></constructor-arg>
<constructor-arg value="lisi"></constructor-arg>
<constructor-arg value="20" type="java.lang.Integer"></constructor-arg>
</bean>
<!--如果不修改为integer类型,那么需要type跟index组合使用-->
<bean id="person5" class="com.mashibing.bean.Person">
<constructor-arg value="6"></constructor-arg>
<constructor-arg value="lisi"></constructor-arg>
<constructor-arg value="22" type="int" index="2"></constructor-arg>
</bean>
</beans>

Person.java

package com.mashibing.bean;

public class Person {
private int id;
private String name;
private Integer age;
private String gender;

public Person() {
}

public Person(int id, String name, Integer age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}

public Person(int id,String name,Integer age){
this.id=id;
this.name=name;
this.age=age;
System.out.println("age");
}

public Person(int id,String name,String gender){
this.id=id;
this.name=name;
this.gender=gender;
System.out.println("gender");
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}
}

SpringDemoTest.java

import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemoTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Person person = context.getBean("person1", Person.class);
System.out.println(person);
Person person2 = context.getBean("person2", Person.class);
System.out.println(person2);
Person person3 = context.getBean("person3", Person.class);
System.out.println(person3);
Person person4 = context.getBean("person4", Person.class);
System.out.println(person4);
Person person5 = context.getBean("person5", Person.class);
System.out.println(person5);
}
}

4、通过命名空间为bean赋值,简化配置文件中属性声明的写法

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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
2、添加配置
<bean id="person" class="com.mashibing.bean.Person" p:id="3" p:name="qiangShan" p:age="32" p:gender="nan"></bean>

5、为复杂类型进行赋值操作

在之前的测试代码中,我们都是给最基本的属性进行赋值操作,在正常的企业级开发中还会遇到给各种复杂类型赋值,如集合、数组、其他对象等。

  







标签:String,spring,id,Person,context,org,赋值,public,属性
From: https://www.cnblogs.com/shanqiang1/p/17712609.html

相关文章

  • springboot整合elasticsearch-RestHighLevelClient api查询
    1.依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.elasticsearch.client</groupId......
  • 11-计算属性 vs 监视属性
    计算属性(computed)vs监视属性(watch)1) computed能完成的功能,watch都可以完成2) watch能完成的功能,computed不一定能完成。例如watch可以进行异步操作。 两个重要的原则1) 所有被Vue管理的函数,最好写成通函数,这样this的指向才是vm或组件实例对象2) 所有不......
  • vue3 computed属性
    该随笔是根据b站小满zs的Vue3+vite+Ts+pinia+实战+源码+electron的视频学习写的,Vue3+vite+Ts+pinia+实战+源码+electron......
  • Spring Boot + Vue3 前后端分离 实战 wiki 知识库系统<一>---Spring Boot项目搭建
    前言:接下来又得被迫开启新的一门课程的学习了,上半年末尾淘汰又即将拉开序幕【已经记不清经历过多少次考试了】,需要去学习其它领域的技术作为考试内容,我选了springboot相关技术,所以。。总之作为男人,不能轻易言败,尽力而为,抱怨解决不了任何问题,逆境使人进步,我坚信这点,效果:在正式学习......
  • RestTemplate【Spring 提供的用于访问Rest 服务的模板类】
    RestTemplate基本介绍RestTemplate是Spring提供的用于访问Rest服务的模板类RestTemplate提供了多种便捷访问远程Http服务的方法老韩说明:小伙伴可以这样理解,通过RestTemplate,我们可以发出http请求(支持Restful风格),去调用Controller提供的API接口,就像我们使......
  • Spring Boot&Vue3前后端分离实战wiki知识库系统<十三>--单点登录开发二
    接着SpringBoot&Vue3前后端分离实战wiki知识库系统<十二>--用户管理&单点登录开发一继续往下。登录功能开发: 接下来则来开发用户的登录功能,先准备后端的接口。后端增加登录接口:1、UserLoginReq:先来准备用户登录的请求实体:packagecom.cexo.wiki.req;importjavax.validation.co......
  • 解密Spring Boot:JPA vs. MyBatis,哪个更适合你的项目?
    Hello大家好,我是小米!今天我要和大家聊聊一个在Java开发中经常会遇到的问题,那就是如何在SpringBoot项目中区分何时该使用JPA,何时该使用MyBatis。这个问题一直困扰着很多开发者,但其实只要理清一些基本概念和场景,就能轻松解决。废话不多说,让我们一起深入探讨吧!了解JPA和MyBatis首先,让......
  • springboot vue电子班牌系统源码,以云平台、云服务器为基础,融合课程管理、物联控制、
    随着时代进步,数字信息化不断发展,很多学校都开始了数字化的转变。智慧校园电子班牌系统是电子班牌集合信息化技术、物联网、智能化,电子班牌以云平台、云服务器为基础,融合了班级文化展示、课程管理、物联控制、教务管理、考勤管理、素质评价、资源管理、家校互联等一系列应用。实现了......
  • 2023-09-18 taro小程序之onGetPhoneNumber无法获取用户手机号回调?console.log没反应??==
    问题描述:一个微信登录按钮,点击获取用户手机号进而登录;按钮用的是taro框架的button组件,其中用到button的onGetPhoneNumber方法,给这个方法绑定一个事件A,用户点击获取手机号后产生回调进而做下一步的业务;问题就是事件A没有获得任何回调,仿佛onGetPhoneNumber不存在。原因:没有满足使用......
  • struts2.1 + Spring 3.X + hibernate3.X架构搭建问题记录
    目前正在试图搭建一个SSH的架构,之前在myeclipse8.6+ssh(struts2.1,spring2.5,hibernate3) +mysql+tomcat6.0做过例子,当时有老师带着,遇到问题也都解决了。而且,自己练习单表的增删改查时也能独立完成了。但是现在换成了myeclipse2014+orcle后,就是通不过去,郁闷中:现在想一遍解......