初识Spring框架(一)
2021-01-03 19:31
标签:enc 它的 基础设施 top text 刷新 发送 要求 定义 spring中有两种类型的bean, 一种是普通bean, 另一种是工厂bean, 即 FactoryBean. 初识Spring框架(一) 标签:enc 它的 基础设施 top text 刷新 发送 要求 定义 原文地址:https://www.cnblogs.com/binwenhome/p/12986888.html框架简介
Spring概述
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>5.0.8.RELEASEversion>
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();
}
}
Spring配置介绍
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="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>
FactoryBean
public class Car {
private String brand;
private Double price;
}
public class MyFactory implements FactoryBean
bean id="factory" class="factory.MyFactory">bean>
Object bean = cac.getBean("factory");
System.out.println(bean);