Ioc和DI介绍
HELLO WORLD程序
getBean方法的三种签名
解决方案:
项目文件:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="helloworld" class="helloworld.helloworld">
<property name="username" value="洲洲"></property>
</bean>
<bean id="helloworld1" class="helloworld.helloworld">
<property name="username" value="苏洲"></property>
</bean>
<bean id="helloworld2" class="helloworld.helloworld">
<property name="username" value="洲"></property>
</bean>
</beans>
helloworld类
package helloworld;
public class helloworld {
private String username;
public helloworld(){
System.out.println("所谓的焦虑就是书读的太少,而想得又特别多");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void sayhello(){
System.out.println("世界你好,我的名字是"+username);
}
}
test测试类
package helloworld;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
public class test {
@Test
public void testputong(){
helloworld world=new helloworld();
world.setUsername("洲洲");
world.sayhello();
}
@Test
public void testspringIoc(){
//从Classpath寻找指定路径
Resource resource=new ClassPathResource("applicationContext.xml");
//根据资源文件创建spring容器
BeanFactory factory=new XmlBeanFactory(resource);
//从spring容器中获取指定的bean对象
helloworld world=(helloworld) factory.getBean("helloworld");
world.sayhello();
}
@Test
//手动模拟配置xml,底层必须弄懂,第一个测试
public void testauto() throws Exception {
//第一步:得到类的全限定名
Class clz=Class.forName("helloworld.helloworld");
//创建对象,获取到无参数构造器;
clz.newInstance();
}
@Test
//手动模拟配置xml
public void testauto1() throws Exception {
//第一步:得到类的全限定名
Class clz=Class.forName("helloworld.helloworld");
//获得构造器
Constructor con=clz.getConstructor();
//设置可访问的
con.setAccessible(true);
//创建对象
Object obj=con.newInstance();
//使用内省机制赋值
BeanInfo info= Introspector.getBeanInfo(clz,Object.class);
PropertyDescriptor []pds=info.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
if("username".equals(pd.getName())){
//获得该属性的写入方法,既setter
pd.getWriteMethod().invoke(obj,"洲洲");
break;
}
}
helloworld world=(helloworld) obj;
world.sayhello();
}
//获取getBean的三种写入方式
@Test
public void testbean(){
//从Classpath获取指定路径
Resource resource=new ClassPathResource("applicationContext.xml");
//根据资源文件创建spring容器
BeanFactory factory=new XmlBeanFactory(resource);
//从spring容器中获取bean容器
//第一种方式:根据对象的类型来获取
// helloworld world=factory.getBean(helloworld.class);
// world.sayhello();
//第二种方式:根据bean的id或name来获取
helloworld world=null;
// world=(helloworld) factory.getBean("helloworld1");
// world.sayhello();
//第三种方式:同时使用bean的id或者name结合对象的类型使用
world=(helloworld) factory.getBean("helloworld2",helloworld.class);
world.sayhello();
}
}
结果为: