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”)注释。