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