Spring IOC容器
Posted 禾野牧村
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring IOC容器相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.llxazy</groupId> <artifactId>spring_demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.3.RELEASE</version> </dependency> </dependencies> </project>
2、编写实体类
Person.java
package com.llxazy.bean; public class Person { private int id; private String name; private int age; private String gender; //此处省略构造方法、get、set方法、toString方法 }
3、注册对象
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"> <!--注册一个对象,spring会自动创建这个对象--> <!-- 一个bean标签就表示一个对象 id:这个对象的唯一标识 class:注册对象的完全限定名 --> <bean id="person" class="com.llxazy.bean.Person"> <!--使用property标签给对象的属性赋值 name:表示属性的名称 value:表示属性的值 --> <property name="id" value="1001"></property> <property name="name" value="李四"></property> <property name="age" value="28"></property> <property name="gender" value="男"></property> </bean> </beans>
4、测试
SpringTest.java
import com.llxazy.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); } }
总结:
1、ApplicationContext就是IOC容器的接口,可以通过此对象获取容器中创建的对象;
2、对象在Spring容器中默认是在创建完成的时候就已经创建完成,不是需要用的时候才创建,此种情况满足的是单例模式;
3、对象在IOC容器中存储的时候默认都是单例的,如果需要多例需要修改属性;
4、创建对象给属性赋值的时候是通过setter方法实现的;
5、对象的属性是由setter/getter方法决定的,而不是定义的成员属性。
一、spring对象的获取及属性赋值方式:
1、
<!--给person类添加构造方法--> <bean id="person2" class="com.llxazy.bean.Person"> <constructor-arg name="id" value="1"></constructor-arg> <constructor-arg name="name" value="lisi"></constructor-arg> <constructor-arg name="age" value="20"></constructor-arg> <constructor-arg name="gender" value="女"></constructor-arg> </bean> <!--在使用构造器赋值的时候可以省略name属性,但是此时就要求必须严格按照构造器参数的顺序来填写了--> <bean id="person3" class="com.llxazy.bean.Person"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="lisi"></constructor-arg> <constructor-arg value="20"></constructor-arg> <constructor-arg value="女"></constructor-arg> </bean> <!--如果想不按照顺序来添加参数值,那么可以搭配index属性来使用--> <bean id="person4" class="com.llxazy.bean.Person"> <constructor-arg value="lisi" index="1"></constructor-arg> <constructor-arg value="1" index="0"></constructor-arg> <constructor-arg value="女" index="3"></constructor-arg> <constructor-arg value="20" index="2"></constructor-arg> </bean> <!--当有多个参数个数相同,不同类型的构造器的时候,可以通过type来强制类型--> 将person的age类型设置为Integer类型 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"); } <bean id="person5" class="com.llxazy.bean.Person"> <constructor-arg value="1"></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.llxazy.bean.Person"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="lisi"></constructor-arg> <constructor-arg value="20" type="int" index="2"></constructor-arg> </bean>
4、通过命名空间为bean赋值,简化配置文件中属性声明的写法
导入命名空间
<?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">
添加配置
<bean id="person6" class="com.mashibing.bean.Person" p:id="3" p:name="wangwu" p:age="22" p:gender="男"></bean>
5、为复杂类型进行赋值操作
给复杂类型赋值,如集合、数组、其他对象等。
实体类:
public class Person { private int id; private String name="李四"; private int 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; //此处省略构造方法、get、set方法、toString方法 } public class Book { private String name; private String author; private double price; //此处省略构造方法、get、set方法、toString方法 } public class Address { private String province; private String city; private String town; //此处省略构造方法、get、set方法、toString方法 }
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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd" > <!--给复杂类型的赋值都在property标签内进行--> <bean id="person" class="com.llxazy.bean.Person"> <property name="name"> <!--赋空值--> <null></null> </property> <!--通过ref引用其他对象,引用外部bean--> <property name="address" ref="address"></property> <!--引用内部bean--> <!-- <property name="address"> <bean class="com.llxazy.bean.Address"> <property name="province" value="北京"></property> <property name="city" value="北京"></property> <property name="town" value="西城区"></property> </bean> </property>--> <!--为list赋值--> <property name="books"> <list> <!--内部bean--> <bean id="book1" class="com.llxazy.bean.Book"> <property name="name" value="水浒传"></property> <property name="author" value="施耐庵"></property> <property name="price" value="100"></property> </bean> <!--外部bean--> <ref bean="book2"></ref> </list> </property> <!--给map赋值--> <property name="maps" ref="myMap"></property> <!--给property赋值--> <property name="properties"> <props> <prop key="aaa">aaa</prop> <prop key="bbb">222</prop> </props> </property> <!--给数组赋值--> <property name="hobbies"> <array> <value>book</value> <value>movie</value> <value>game</value> </array> </property> <!--给set赋值--> <property name="sets"> <set> <value>111</value> <value>222</value> <value>222</value> </set> </property> </bean> <bean id="address" class="com.llxazy.bean.Address"> <property name="province" value="河北"></property> <property name="city" value="邯郸"></property> <property name="town" value="武安"></property> </bean> <bean id="book2" class="com.llxazy.bean.Book"> <property name="name" value="西游记"></property> <property name="author" value="吴承恩"></property> <property name="price" value="200"></property> </bean> <!--级联属性--> <bean id="person2" class="com.llxazy.bean.Person"> <property name="address" ref="address"></property> <property name="address.province" value="北京"></property> </bean> <!--util名称空间创建集合类型的bean--> <util:map id="myMap"> <entry key="key1" value="value1"></entry> <entry key="key2" value-ref="book2"></entry> <entry key="key03"> <bean class="com.llxazy.bean.Book"> <property name="name" value="西游记" ></property> <property name="author" value="吴承恩" ></property> <property name="price" value="100" ></property> </bean> </entry> </util:map> </beans>
6、继承关系bean的配置
ioc.xml
<bean id="person" class="com.llxazy.bean.Person"> <property name="id" value="1"></property> <property name="name" value="zhangsan"></property> <property name="age" value="21"></property> <property name="gender" value="男"></property> </bean> <!--parent:指定bean的配置信息继承于哪个bean--> <bean id="person2" class="com.llxazy.bean.Person" parent="person"> <property name="name" value="lisi"></property>
</bean>
如果想实现Java文件的抽象类,不需要将当前bean实例化的话,可以使用abstract属性
<bean id="person" class="com.llxazy.bean.Person" abstract="true"> <property name="id" value="1"></property> <property name="name" value="zhangsan"></property> <property name="age" value="21"></property> <property name="gender" value="男"></property> </bean> <!--parent:指定bean的配置信息继承于哪个bean--> <bean id="person2" class="com.llxazy.bean.Person" parent="person">以上是关于Spring IOC容器的主要内容,如果未能解决你的问题,请参考以下文章