Spring的主要思想是控制反轉(IOC)和面向切面編程(AOP)
1.控制反轉:對象的交由Spring容器來管理:對象的創建、屬性注入。
2.bean對象創建的三種方式:通過構造方法、通過靜態方法、通過實例方法。
<bean id="example1" class="com.Example"/><!--通過構造器-->
<bean id="example2" class="com.Example" factory-method="createInstance"/><!--通過靜態方法-->
<bean id="example3" factory-bean="example1" factory-method="createObject" /><!--通過實例方法-->
main方法:
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
System.out.println(context.getBean("example1"));
System.out.println(context.getBean("example2"));
System.out.println(context.getBean("example3"));
}
}
3.屬性的注入:通過構造器,可以使用constructor的index屬性、name屬性、type屬性來和bean對象中的屬性相匹配。
<bean id="student" class="com.Student">
<constructor-arg index="0" value="zhangsan"/>
<constructor-arg type="int" value="20"/>
<constructor-arg index="2" value="M"/>
<constructor-arg name="teacher" ref="tea"/>
</bean>
<bean id="tea" class="com.Teacher">
<constructor-arg name="name" value="李老師" />
<constructor-arg type="int" value="20" />
</bean>
Student類的屬性:
private String name;
private int age;
private char sex;
private Teacher teacher;
Teacher對象的屬性:
private String name;
private int age;
main方法:
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student stu=(Student)context.getBean("student");
System.out.println(stu);
}
}
運行結果:
Student{name='zhangsan', age=20, sex=M, teacher=Teacher{name='李老師', age=20}}
通過set方法注入:
<bean id="student" class="com.Student">
<property name="name" value="張三" />
<property name="age" value="20"/>
<property name="sex" value="F"/>
<property name="teacher" ref="tea" />
</bean>
<bean id="tea" class="com.Teacher">
<property name="name" value="王老師"/>
<property name="age" value="35" />
</bean>
main方法保持不變,運行結果:
Student{name='張三', age=20, sex=F, teacher=Teacher{name='王老師', age=35}}
集合類型的裝配:
bean對象的屬性:
private Properties properties;
private List list;
private Map map;
private Set set;
屬性值的注入:
<bean id="arr" class="com.Array"><!--創建對象-->
<property name="properties"><!--對第一個屬性值注入-->
<props>
<prop key="name">張三</prop>
<prop key="age">20</prop>
</props>
</property>
<property name="list">
<list>
<value>zhangsan</value>
<value>20</value>
<ref bean="student"/>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="zhangsan" />
<entry key="age" value="20"/>
</map>
</property>
<property name="set">
<set>
<value>123</value>
<value>this is a string</value>
<ref bean="student"/>
</set>
</property>
</bean>
main方法中幾種數據結構的遍歷:
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Array array = (Array) context.getBean("arr");
Properties pro = array.getProperties();
Set<Map.Entry<Object, Object>> set = pro.entrySet();
for (Map.Entry<Object, Object> s : set) {
System.out.println(s.getKey() + "--------" + s.getValue());
}
System.out.println("-----------------------------------------------------------");
List list = array.getList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println(obj);
}
System.out.println("-----------------------------------------------------------");
Map map = array.getMap();
Set set1 = map.keySet();
Iterator iterator1 = set1.iterator();
while (iterator1.hasNext()) {
String key = (String) iterator1.next();
System.out.println(key + "------------" + map.get(key));
}
System.out.println("-----------------------------------------------------------");
Set set2 = array.getSet();
Iterator iter = set2.iterator();
while (iter.hasNext())
{
System.out.println(iter.next());
}
}
}
運行結果:
age--------20
name--------張三
-----------------------------------------------------------
zhangsan
20
Student{name='張三', age=20, sex=F, teacher=Teacher{name='王老師', age=35}}
-----------------------------------------------------------
name------------zhangsan
age------------20
-----------------------------------------------------------
123
this is a string
Student{name='張三', age=20, sex=F, teacher=Teacher{name='王老師', age=35}}
配置文件中bean標籤的屬性:
scope:可以在配置文件的bean標籤中加scope,也可在bean類上面加@Scope(“singleton”)註釋。