使用Spring的IOC来创建对象一共有3中方式:
1)通过无参构造的方式来创建
Hello.java代码:
package com.myspring.bean;
public class Hello {
private String name;
public Hello() {
System.out.println("hello 被创建");
}
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.println("hello," + name);
}
}
beans.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean就是java对象 , 由spring容器创建和管理 -->
<bean name="hello" class="com.myspring.bean.Hello">
<property name="name" value="张三" />
</bean>
</beans>
测试类Test代码:
package com.myspring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.myspring.bean.Hello;
public class Test {
public static void main(String[] args) {
//解析beans.xml文件,生成管理相应的bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") ;
Hello hello = (Hello) context.getBean("hello") ;
hello.show();
}
}
运行可以看到控制台打印信息如下:
2)有参构造
Hello.java代码:
package com.myspring.bean;
public class Hello {
private String name;
public Hello(String name) {
super();
System.out.println("有参构造name:"+name);
this.name = name;
}
public void show() {
System.out.println("hello," + name);
}
}
beans.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean就是java对象 , 由spring容器创建和管理 -->
<bean name="hello" class="com.myspring.bean.Hello">
<constructor-arg index="0" value="李四" />
</bean>
</beans>
其中index指构造方法参数下标从0开始,当有多个时,配置如下:
<constructor-arg index="0" value="李四" />
<constructor-arg index="1" value="王五" />
第二种是根据参数名称,指定name,如图:
第三种:根据参数类型设置,
测试类代码Test不变,测试结果控制台打印如下:
3)通过工厂方法
分为两种:
第一种是静态工厂
HelloFactory代码:
package com.myspring.factory;
import com.myspring.bean.Hello;
/**
* 静态工厂类
*/
public class HelloFactory {
public static Hello newInstance(String name) {
return new Hello(name) ;
}
}
beans.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean就是java对象 , 由spring容器创建和管理 -->
<bean id="hello" class="com.myspring.factory.HelloFactory"
factory-method="newInstance">
<constructor-arg index="0" value="李四" />
</bean>
</beans>
第二种是动态工厂
动态工厂类代码:
package com.myspring.factory;
import com.myspring.bean.Hello;
/**
* 动态工厂类
*/
public class HelloDynamicFactory {
public Hello newInstance(String name) {
return new Hello(name) ;
}
}
注意这个代码的newInstance()方法,不是static的,否则运行程序会报错
beans.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean就是java对象 , 由spring容器创建和管理 -->
<bean id="helloFactory" class="com.myspring.factory.HelloDynamicFactory" />
<bean id="hello" factory-bean="helloFactory" factory-method="newInstance">
<constructor-arg index="0" value="李四" />
</bean>
</beans>
测试代码:
package com.myspring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.myspring.bean.Hello;
public class Test {
public static void main(String[] args) {
//解析beans.xml文件,生成管理相应的bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") ;
Hello hello = (Hello) context.getBean("hello") ;
hello.show();
}
}
测试结果: