import lombok.Data;
@Data
public class People {
private String name;
private Integer age;
private String eat;
}
<?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 id="people" class="People">
<property name="name" value="张三"/>
<property name="age" value="18"/>
<property name="eat" value="吃饭"/>
</bean>
</beans>
注:无参构造方法如果不存在,将抛出异常BeanCreationException
package domain;
import lombok.Data;
@Data
public class People {
private String name;
private Integer age;
private String eat;
}
package factory;
import domain.People;
public class PeopleFactory {
public static People getPeople() {
return new People();
}
}
<?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 id="people" class="factory.PeopleFactory" factory-method="getPeople">
<property name="name" value="张三"/>
<property name="age" value="18"/>
<property name="eat" value="吃饭"/>
</bean>
</beans>
package domain;
import lombok.Data;
@Data
public class People {
private String name;
private Integer age;
private String eat;
}
package factory;
import domain.People;
public class PeopleFactory {
public People getPeople() {
return new People();
}
}
<?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 id="PeopleFactory" class="factory.PeopleFactory"/>
<bean id="people" factory-bean="PeopleFactory" factory-method="getPeople">
<property name="name" value="张三"/>
<property name="age" value="18"/>
<property name="eat" value="吃饭"/>
</bean>
</beans>
package domain;
import lombok.Data;
@Data
public class People {
private String name;
private Integer age;
private String eat;
}
package factory;
import domain.People;
import org.springframework.beans.factory.FactoryBean;
public class PeopleFactoryBean implements FactoryBean<People> {
@Override
public People getObject() throws Exception {
return new People();
}
@Override
public Class<?> getObjectType() {
return People.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
<?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 id="people" class="factory.PeopleFactoryBean" />
</beans>