标签:enc 它的 基础设施 top text 刷新 发送 要求 定义
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.8.RELEASE</version> </dependency>
public class Person { private Integer id; private String name; //... }
<bean id="person" class="spring.bean.Person"> <property name="id" value="111"></property> <property name="name" value="zhangsan"></property> </bean>
public class TestBySpring { public static void main(String[] args) { //初始化容器 ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("applicationContext.xml"); //通过getBean()获取对象 Person person = cac.getBean("person", Person.class); System.out.println(person); cac.close(); } }
<bean id="person" class="Person" scope="singleton"> <property name="id" value="222"></property> <property name="name" value="李四"></property> </bean>
<bean id="person3" class="top.binwenhome.spring.bean.Person"> <constructor-arg value="333"></constructor-arg> <constructor-arg value="wangwu"></constructor-arg> </bean>
<bean id="person4" class="top.binwenhome.spring.bean.Person"> <constructor-arg name="name" value="zhaoliu"></constructor-arg> </bean>
<bean id="person5" class="top.binwenhome.spring.bean.Person" p:id="444" p:name="xiaoming"> </bean> IDEA中的名称空间是可以自动导入的.
<bean id="teacher1" class="bean.Teacher"> <property name="tid" value="1"></property> <property name="tname" value="xiaoming"></property> </bean> <bean id="student1" class="bean.Student"> <property name="id" value="11"></property> <property name="name" value="xiaohong"></property> <property name="age" value="18"></property> <property name="sex" value="female"></property> <property name="teacher" ref="teacher1"></property> </bean>
<bean id="teacher1" class="bean.Teacher"> <property name="tid" value="1"></property> <property name="tname" value="xiaoming"></property> </bean> <bean id="student1" class="bean.Student"> <property name="id" value="11"></property> <property name="name" value="xiaohong"></property> <property name="age" value="18"></property> <property name="sex" value="female"></property> <property name="teacher" ref="teacher1"></property> <property name="teacher.tid" value="2"></property> </bean> 这样, tid就从1变成了2
<bean id="student1" class="bean.Student"> <property name="id" value="11"></property> <property name="name" value="xiaohong"></property> <property name="age" value="18"></property> <property name="sex" value="female"></property> <property name="teacher"> <bean class="bean.Teacher" p:tid="3" p:tname="haha" ></bean> </property> </bean>
<bean id="teacher" class="bean.Teacher"> <property name="tid" value="102"></property> <property name="tname" value="xiaoming"></property> <property name="grades"> <array> <value>一年级</value> <value>二年级</value> </array> </property> <property name="persons"> <list> <ref bean="person" /> <ref bean="person2" /> <ref bean="person3" /> </list> </property> </bean>
<bean id="teacher" class="bean.Teacher"> <property name="tid" value="102"></property> <property name="tname" value="xiaoming"></property> <property name="map"> <map> <entry> <key><value>1</value></key> <ref bean="person2"></ref> </entry> <entry> <key><value>2</value></key> <ref bean="person3"></ref> </entry> </map> </property> </bean>
<bean id="teacher" class="bean.Teacher"> <property name="tid" value="102"></property> <property name="tname" value="xiaoming"></property> <property name="persons" ref="list"></property> <property name="grades" ref="string"></property> </bean> <util:list id="string"> <value>一年级</value> <value>二年级</value> <value>三年级</value> </util:list> <util:list id="list"> <ref bean="person" /> <ref bean="person2" /> <ref bean="person3" /> </util:list>
spring中有两种类型的bean, 一种是普通bean, 另一种是工厂bean, 即 FactoryBean.
public class Car { private String brand; private Double price; }
public class MyFactory implements FactoryBean<Car> { @Override public Car getObject() throws Exception { Car car = new Car("baoma", 3000000D); return car; } @Override public Class<?> getObjectType() { return Car.class; } @Override public boolean isSingleton() { return false; } }
<!-- 虽然这里写的是工厂的全类名, 但实际返回的是工厂创建的对象: getObject()返回的对象 --> <bean id="factory" class="factory.MyFactory"></bean>
Object bean = cac.getBean("factory");
System.out.println(bean);
标签:enc 它的 基础设施 top text 刷新 发送 要求 定义
原文地址:https://www.cnblogs.com/binwenhome/p/12986888.html