1.通过set方法设值注入
设置值的时候用set,获取值的时候用get
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("========");
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("--------");
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
<bean id="myStudent" class="com.di01.Student">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
</bean>
@Test
public void test01() {
// 加载Spring配置文件,创建Spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/di01/applicationContext.xml");
// 从容器中获取指定的bean对象
Student student = (Student) ac.getBean("myStudent");
System.out.println(student);
}
导入头文件,可以走windows-preference-xmlfiles-editor-templates-add添加,记得复制原码
<bean id="myStudent" class="com.di01.Student">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
<property name="school" ref="mySchool"></property>
</bean>
<bean id="mySchool" class="com.di01.School">
<property name="sname" value="林远镖局"></property>
</bean>
2.构造注入(不需要调无参构造器,但是最好还是带上)
public class Student {
private String name;
private int age;
private School school;// 域属性
public Student() {
System.out.println("---------"); // 无参构造器
}
public Student(String name, int age, School school) {
super(); // 有参构造器
this.name = name;
this.age = age;
this.school = school;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
}
}
<bean id="myStudent" class="com.di02.Student"> // 构造注入的方法,使用index,name,或者直接value只要保证顺序和构造器的一致即可,选择合适的就行
<constructor-arg index="0" value="linda"></constructor-arg>
<constructor-arg index="1" value="20"></constructor-arg>
<constructor-arg index="2" ref="mySchool"></constructor-arg>
</bean>
<bean id="mySchool" class="com.di02.School">
<property name="sname" value="林远镖局"></property>
</bean>
3.集合属性注入
private School[] schools; //每个元素都是对象引用
private List<String> myList;
private Set<String> mySet;
private Map<String, Object> myMap;
private Properties myPros;// 健和值都是字符串
<bean id="school1" class="com.di03.School">
<property name="sname" value="南京大学"></property>
</bean>
<bean id="school2" class="com.di03.School">
<property name="sname" value="东南大学"></property>
</bean>
<bean id="some" class="com.di03.Some">
<property name="schools">
<array>
<ref bean="school1" />
<ref bean="school2" />
</array>
</property>
<property name="myList">
<list>
<value>lucy</value>
<value>lily</value>
</list>
</property>
<property name="mySet">
<set>
<value>天涯</value>
<value>海角</value>
</set>
</property>
<property name="myMap"> //map是键值对输入
<map>
<entry key="av1" value="武藤兰"></entry>
<entry key="av2" value="小泽玛莉亚"></entry>
</map>
</property>
<property name="myPros">
<props>
<prop key="联系方式">深圳</prop>
<prop key="具体说明">东莞一条龙</prop>
</props>
</property>
</bean>
<property name="arrs" value="abc,def"></property>
<property name="myList" value="张三,李四" />
<property name="mySet" value="天涯,海角" /> //对于字符串可以直接写,对于键值对和数组无法使用。
4.对于域属性的自动注入
<bean id="myStudent" class="com.di05.Student" autowire="byName">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
</bean>
<bean id="school" class="com.di05.School">
<property name="sname" value="林远镖局"></property>
</bean>
<bean id="myStudent" class="com.di06.Student" autowire="byType">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
</bean>
5.使用SPEL注入
Spring EL表达式语言,SPEL以#开头,后跟一对大括号,用法: <bean id="" value="#{}"/>
public int computeAge() {
return page > 25 ? 25 : page;
}
<bean id="myPerson" class="com.di07.Person">
<property name="pname" value="lucy"></property>
<property name="page" value="#{T(java.lang.Math).random()*50}"></property>
</bean>
<bean id="myStudent" class="com.di07.Student">
<property name="name" value="#{myPerson.pname}"></property>
<property name="age" value="#{myPerson.computeAge()}"></property>
</bean>
要有get方法,可以在类中定义方法,在Bean中进行调用
6.内部匿名Bean
<bean id="myStudent" class="com.di06.Student" autowire="byType">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
<property name="school">
<bean class="com.di06.School">
<property name="sname" value="daxue"/>
</bean>
</property>
</bean>
-
同类抽象Bean
遇到相同的内容,会想到继承关系,可以使用parent属性,格式如 <bean id="" parent=""> -
异常抽象Bean
不是同类使用abstract,格式 <bean id="base" abstract="true">
9.为应用指定多个Spring配置文件(平等关系)
目录下有好几个bean文件,格式相同,比如spring-beans,spring-base,
可以在test写成di/spring-*.xml
格式不相同,把每个列出来,资源需要。
applicationContext ac=new classpathxmlapplicationContext(r1,r2);
也可放入数组中进行配置
String[] resources={r1,r2};
(包含关系)
网友评论