本例都是在局部配置文件中配置的,也可以在applicationContext.xml文件中直接进行配置,为了说明方便,这里用的局部配置方法。
(1)通过构造方法
创建一个类Cat
package
cn.java.di;
public
class
Cat {
private
String name;
private
Integer age;
private
Float weight;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
Integer getAge() {
return
age;
}
public
void
setAge(Integer age) {
this
.age = age;
}
public
Float getWeight() {
return
weight;
}
public
void
setWeight(Float weight) {
this
.weight = weight;
}
public
Cat() {
System.out.println(
"无参构造方法"
);
}
public
Cat(String name, Integer age, Float weight) {
super
();
this
.name = name;
this
.age = age;
this
.weight = weight;
System.out.println(
"有参构造方法"
);
}
@Override
public
String toString() {
return
"Cat [name="
+ name +
", age="
+ age +
", weight="
+ weight +
"]"
;
}
}
创建一个局部配置文件:
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"https://www.springframework.org/schema/beans"
xmlns:xsi=
"https://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="https://www.springframework.org/schema/beans
https:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class
=
"cn.java.di.Cat"
id=
"cat"
>
<constructor-arg index=
"0"
type=
"java.lang.String"
value=
"tom"
></constructor-arg>
<constructor-arg index=
"1"
type=
"java.lang.Integer"
value=
"10"
></constructor-arg>
<constructor-arg index=
"2"
type=
"java.lang.Float"
value=
"2.5"
></constructor-arg>
</bean>
</beans>
然后在applicationContext.xml文件中将其关联:
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"https://www.springframework.org/schema/beans"
xmlns:xsi=
"https://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="https://www.springframework.org/schema/beans
https:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean:将一个类的对象创建过程交给spring容器
class
:指定类的具体路径
id:唯一标识符
scope:用来控制spring容器产生对象的方式,可以为单例也可以为多例。
常用的值为singleton(单例),prototype(多例),默认的是单例。
init-method:表示初始化方法,只写初始化方法的名字,不用加上();
-->
<
import
resource=
"cn/java/di/Cat.xml"
></
import
>
</beans>
创建一个测试类:
package
cn.java.ioc1;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
cn.java.di.Cat;
import
cn.java.initAndDistroy.Ji;
import
cn.java.lazy.Duck;
import
cn.java.singleton2.Dog;
public
class
Window {
public
static
void
main(String[] args) {
//启动框架(context代表spring容器)
ApplicationContext context=
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
//只写这句也可以调用YellowMouseWolf类中的无参构造方法
System.out.println(
"-----------------------"
);
//获取spring容器中创建的对象(通过id值获取)
Cat cat=(Cat)context.getBean(
"cat"
);
}
}
(2)通过get/set方法赋值
这时只需要修改:
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"https://www.springframework.org/schema/beans"
xmlns:xsi=
"https://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="https://www.springframework.org/schema/beans
https:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class
=
"cn.java.di.Cat"
id=
"cat"
>
<property name=
"name"
value=
"jack"
></property>
<property name=
"age"
value=
"20"
></property>
<property name=
"weight"
value=
"5.2"
></property>
</bean>
</beans>