/*
控制反转和依赖注入的优点:控制反转的迷失将对象全部放到xml文件中定义<bean>中的property定义属性,赋值也变得方便,,所以实现子类也变得容易,只要改动xml文件即可。控制反转使得对象使用前不用再一定要创建,只需要在容器中抓取即可如在测试类中User user=factory.getbean(); 对应属性:user.getName()等。
1。。最基本的单个bean赋值取值:在bean类中属性有对应的setget方法,可以在bean类中就有取值的方法,,在xml文件对这个bean定义的地方写上property name value 进行赋值
在测试方法中加载xml文件,并以此加载得到bean类的对象,得值也可以用这个对象调用get方法得到!
*/
bean类:People.java
public class People implements peopleIntef {
private String name;
private Integer age;
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;
}
/* (non-Javadoc)
* @see spring.peopleIntef#peo()
*/
public void peo()
{
System.out.println(this.getName()+"----"+this.getAge());
}
}
对应的接口
package Interface;
public interface peopleIntef {
void peo();
}
测试方法
public class testpeple {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");//读取bean.xml中的内容
People p=(People) ctx.getBean("person") ;
p.peo();
System.out.println("age:"+p.getAge());
System.out.println("name:"+p.getName());
}
}
src下的xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="person" class="spring.People">
<property name="name" value="xingoo"/>
<property name="age" value="12"/>
</bean>
</beans>
/*
注入其他的bean对象
正常建要被注入的bean A和他对应的接口,在要注入bean的bean B中定义接口对象(注意,定义的是接口 ,不是bean类,目的是松耦合),并在B中写这个接口对象的setget方法,可以在B的方法中通过这个接口对象调用bean A 中 的方法。
在xml中定义正常bean A 在bean B的<bean><bean>中添加property属性 <property name="save" ref="saves"/> //其中name为B的bean类的中定义时A的接口对象名
ref为正常定义bean A时的id属性的值
当然也可以在测试方法中通过B的类对象.getA对象.A方法()方法进行A类中方法的调用。
*/
save接口
public interface saveInterf {
void dsave();
}
save的bean类
package spring;
import Interface.saveInterf;
public class save implements saveInterf {
/* (non-Javadoc)
* @see spring.saveInterf#dsave()
*/
public void dsave()
{
System.out.println("I am save");
}
}
people的bean类
public class People implements peopleIntef {
private String name;
private Integer age;
private saveInterf save;
public saveInterf getSave() {
return save;
}
public void setSave(saveInterf save) {
this.save = save;
}
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;
}
/* (non-Javadoc)
* @see spring.peopleIntef#peo()
*/
public void peo()
{
System.out.println(this.getName()+"----"+this.getAge());
save.dsave();
}
}
people的接口
package Interface;
public interface peopleIntef {
void peo();
}
测试方法
public class testpeple {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");//读取bean.xml中的内容
People p=(People) ctx.getBean("person") ;
/* p.peo();
System.out.println("age:"+p.getAge());
System.out.println("name:"+p.getName());*/
p.getSave().dsave();
p.peo();
}
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="saves" class="spring.save">
</bean>
<bean id="person" class="spring.People">
<property name="name" value="xingoo"/>
<property name="age" value="12"/>
<property name="save" ref="saves"/>
</bean>
</beans>