spring 创建三种创建对象的方法
User类
x
public class User {
private String name;
private Integer age;
private TestObj testObj;
public TestObj getTestObj(){
return new TestObj();
}
}
getTestObj类
public class TestObj {
private String Test;
}
xml配置
xxxxxxxxxx
<!--使用默认方法构建-->
<bean id="user" class="com.test.User"/>
<!--使用工厂内的普通方法创建-->
<bean id="testObj" factory-bean="user" factory-method="getTestObj"/>
<!--通过静态方法-->
<bean id="staticGetTestObj" class="com.chexd.User" factory-method="getStaticTestObj"/>
@Test(比如用第三方的jar包 放到spring工厂内)
x
ApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml" );
TestObj testObj = context.getBean( "testObj", TestObj.class );
System.out.println(testObj);
TestObj testStaticObj = context.getBean( "staticGetTestObj", TestObj.class );
System.out.println(testStaticObj);