spring对象的获取及属性赋值方式
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、为复杂类型进行赋值操作
在之前的测试代码中,我们都是给最基本的属性进行赋值操作,在正常的企业级开发中还会遇到给各种复杂类型赋值,如集合、数组、其他对象等。
Person.java
package com.mashibing.bean;
import java.util.*;
public class Person {
private Integer id;
private String name="dahuang";
private Integer age;
private String gender;
private Address address;
private String[] hobbies;
private List<Book> books;
private Set<Integer> sets;
private Map<String,Object> maps;
private Properties properties;
public Person() {
}
public Person(Integer id, String name, Integer age, String gender, Address address, String[] hobbies, List<Book> books, Set<Integer> sets, Map<String, Object> maps, Properties properties) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.address = address;
this.hobbies = hobbies;
this.books = books;
this.sets = sets;
this.maps = maps;
this.properties = properties;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public Set<Integer> getSets() {
return sets;
}
public void setSets(Set<Integer> sets) {
this.sets = sets;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", address=" + address +
", hobbies=" + Arrays.toString(hobbies) +
", books=" + books +
", sets=" + sets +
", maps=" + maps +
", properties=" + properties +
'}';
}
}
Book.java
package com.mashibing.bean;
public class Book {
private String name;
private String author;
private double price;
public Book() {
}
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
Address.java
package com.mashibing.bean;
public class Address {
private String province;
private String city;
private String town;
public Address() {
}
public Address(String province, String city, String town) {
this.province = province;
this.city = city;
this.town = town;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", town='" + town + '\'' +
'}';
}
}
ioc.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"
xmlns:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
<bean id="person" class="com.mashibing.bean.Person">
<property name="id" value="1"></property>
<property name="name">
<!--赋空值-->
<null></null>
</property>
<property name="age" value="32"></property>
<property name="gender" value="nan"></property>
<property name="address" ref="address"></property>
<property name="books">
<list>
<!--内部bean-->
<bean id="book1" class="com.mashibing.bean.Book">
<property name="name" value="多线程与高并发"></property>
<property name="author" value="马士兵"></property>
<property name="price" value="1000"></property>
</bean>
<!--外部bean-->
<ref bean="book2"></ref>
</list>
</property>
<!--给map赋值-->
<property name="maps" ref="myMap"></property>
<!--给property赋值-->
<property name="properties">
<props>
<prop key="aaa">111</prop>
<prop key="bbb">222</prop>
<prop key="ccc">333</prop>
</props>
</property>
<!--给数组赋值-->
<property name="hobbies">
<array>
<value>book</value>
<value>movie</value>
<value>game</value>
</array>
</property>
<!--给set赋值-->
<property name="sets">
<set>
<value>123</value>
<value>456</value>
<value>789</value>
</set>
</property>
<!--<!–引用内部bean–>
<property name="address">
<bean id="address1" class="com.mashibing.bean.Address">
<property name="province" value="江西省"></property>
<property name="city" value="抚州市"></property>
<property name="town" value="南城县"></property>
</bean>
</property>-->
</bean>
<!--<!–级联属性–>
<bean id="person2" class="com.mashibing.bean.Person">
<property name="address" ref="address"></property>
<property name="address.province" value="北京"></property>
</bean>-->
<bean id="address" class="com.mashibing.bean.Address">
<property name="province" value="jiangxisheng"></property>
<property name="city" value="fuzhoushi"></property>
<property name="town" value="nanchengxian"></property>
</bean>
<bean id="book2" class="com.mashibing.bean.Book">
<property name="name" value="JVM"></property>
<property name="author" value="马士兵"></property>
<property name="price" value="1200"></property>
</bean>
<!--util名称空间创建集合类型的bean-->
<util:map id="myMap">
<entry key="key1" value="value1"></entry>
<entry key="key2" value-ref="book2"></entry>
<entry key="key3">
<bean class="com.mashibing.bean.Book">
<property name="name" value="西游记"></property>
<property name="author" value="吴承恩"></property>
<property name="price" value="100"></property>
</bean>
</entry>
</util:map>
</beans>
applicationContext.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="qiangShan"></property>
<property name="age" value="21"></property>
<property name="gender" value="nan"></property>
</bean>
<!--parent:指定bean的配置信息继承于哪个bean-->
<bean id="person1" class="com.mashibing.bean.Person" parent="person">
<property name="name" value="lisi"></property>
</bean>
</beans>
如果想实现Java文件的抽象类,不需要将当前bean实例化的话,可以使用abstract属性
applicationContext.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" abstract="true">
<property name="id" value="1"></property>
<property name="name" value="qiangShan"></property>
<property name="age" value="21"></property>
<property name="gender" value="nan"></property>
</bean>
<!--parent:指定bean的配置信息继承于哪个bean-->
<bean id="person1" class="com.mashibing.bean.Person" parent="person">
<property name="name" value="lisi"></property>
</bean>
</beans>
7、bean对象创建的依赖关系
bean对象在创建的时候是按照bean在配置文件的顺序决定的,也可以使用depend-on标签来决定顺序
applicationContext.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" depends-on="address,book">
<property name="id" value="1"></property>
<property name="name" value="单强"></property>
<property name="age" value="32"></property>
<property name="gender" value="男"></property>
<property name="address" ref="address"></property>
<property name="books">
<list>
<ref bean="book"></ref>
</list>
</property>
</bean>
<bean id="address" class="com.mashibing.bean.Address">
<property name="province" value="江西省"></property>
<property name="city" value="抚州市"></property>
<property name="town" value="南城县"></property>
</bean>
<bean id="book" class="com.mashibing.bean.Book">
<property name="name" value="西游记"></property>
<property name="author" value="吴承恩"></property>
<property name="price" value="1000"></property>
</bean>
</beans>
8、bean的作用域控制,是否是单例
applicationContext1.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" scope="singleton"></bean>
</beans>
9、利用工厂模式创建bean对象
在之前的案例中,所有bean对象的创建都是通过反射得到对应的bean实例,其实在spring中还包含另外一种创建bean实例的方式,就是通过工厂模式进行对象的创建
在利用工厂模式创建bean实例的时候有两种方式,分别是静态工厂和实例工厂。
静态工厂:工厂本身不需要创建对象,但是可以通过静态方法调用,对象=工厂类.静态工厂方法名();
实例工厂:工厂本身需要创建对象,工厂类 工厂对象=new 工厂类;工厂对象.get对象名();
PersonStaticFactory.java
package com.mashibing.factory;
import com.mashibing.bean.Person;
public class PersonStaticFactory {
public static Person getPerson(String name){
Person person=new Person();
person.setId(1);
person.setName(name);
return person;
}
}
ioc.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">
<!--
静态工厂的使用:
class:指定静态工厂类
factory-method:指定哪个方法是工厂方法
-->
<bean id="person" class="com.mashibing.factory.PersonStaticFactory" factory-method="getPerson">
<constructor-arg value="lisi"></constructor-arg>
</bean>
</beans>
PersonInstanceFactory.java
package com.mashibing.factory;
import com.mashibing.bean.Person;
public class PersonInstanceFactory {
public Person getPerson(String name){
Person person=new Person();
person.setId(1);
person.setName(name);
return person;
}
}
<!--实例工厂使用-->
<!--创建实例工厂类-->
<bean id="personInstanceFactory" class="com.mashibing.factory.PersonInstanceFactory"></bean>
<!--
factory-bean:指定使用哪个工厂实例
factory-method:指定使用哪个工厂实例的方法
-->
<bean id="person1" class="com.mashibing.bean.Person" factory-bean="personInstanceFactory" factory-method="getPerson">
<constructor-arg value="wangwu"></constructor-arg>
</bean>
10、继承FactoryBean来创建对象
FactoryBean是Spring规定的一个接口,当前接口的实现类,Spring都会将其作为一个工厂,但是在ioc容器启动的时候不会创建实例,只有在使用的时候才会创建对象
package com.mashibing.factory;
import com.mashibing.bean.Person;
import org.springframework.beans.factory.FactoryBean;
/**
* 实现了FactoryBean接口的类是Spring中可以识别的工厂类,spring会自动调用工厂方法创建实例
*/
public class MyFactoryBean implements FactoryBean<Person> {
/**
* 工厂方法,返回需要创建的对象
* @return
* @throws Exception
*/
@Override
public Person getObject() throws Exception {
Person person=new Person();
person.setName("maliu");
return person;
}
/**
* 返回创建对象的类型,spring会自动调用该方法返回对象的类型
* @return
*/
@Override
public Class<?> getObjectType() {
return Person.class;
}
/**
* 创建的对象是否是单例对象
* @return
*/
@Override
public boolean isSingleton() {
return false;
}
}
ioc.xml
<bean id="myFactoryBean" class="com.mashibing.factory.MyFactoryBean">
</bean>