我无法快速找到解决方案,我决定亲自去做并分享。
Spring,从getter其他对象中填充对象的setter
Standart基于Java的代码:
public class Mediator{
public static Contact getFullContact(){
Contact contact = new Contact();
RandomData randomData = new RandomData();
contact.setName(randomData.getName());
contact.setPhone(randomData.getPhone());
contact.setEmail(randomData.getEmail());
return contact;
}
}
public class Test {
public static void main(String[] args) {
for(int i=0; i<5; i++) {
Contact contact = Mediator.getFullContact();
System.out.println(contact + " / " + contact.getName() + " / " + contact.getPhone() + " / " + contact.getEmail());
}
}
}
基于弹簧的变体:
pom.xml的
CGLIB用于扩展JAVA类并在运行时实现接口。
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.6</version>
</dependency>
Spring 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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="contact" class="yourPackage.contacts.Contact" scope="prototype"></bean>
<bean id="randomData" class="yourPackage.contacts.RandomData" scope="prototype"></bean>
<bean id="conveyor" class="yourPackage.contacts.MediatorConveyor">
<lookup-method name="createContact" bean="contact"/> <!-- CGLIB is substitute dynamicaly-created Contact class -->
<lookup-method name="createRandomData" bean="randomData"/> <!-- CGLIB is substitute dynamicaly-created RandomData class -->
</bean>
</beans>
测试类:
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
MediatorConveyor conveyor = (MediatorConveyor)context.getBean("conveyor");
for(int i=0; i<5; i++) {
Contact contact = conveyor.getContact();
System.out.println(contact + " / " + contact.getName() + " / " + contact.getPhone() + " / " + contact.getEmail());
}
}
}
申请表:
MediatorConveyor类:
public abstract class MediatorConveyor{
protected abstract Contact createContact();
protected abstract RandomData createRandomData();
public Contact getContact() {
Contact contact = createContact();
RandomData randomData = createRandomData();
contact.setName(randomData.getName());
contact.setPhone(randomData.getPhone());
contact.setEmail(randomData.getEmail());
return contact;
}
}
联系班级:
public class Contact {
private String name = null;
private String phone = null;
private String email = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
RandomData类:
public class RandomData {
private List<String> alphabetS = new ArrayList<>(Arrays.asList("b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,x,z".split(",")));
private List<String> alphabetG = new ArrayList<>(Arrays.asList("a,e,i,o,u,y".split(",")));
private Random random = new Random();
private String name = "";
private String phone = "";
RandomData(){
createName();
createNumberPhone();
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return name.toLowerCase()+"@mail.ru";
}
private void createName() {
int countsymbol = 0;
while(countsymbol < 5) {
countsymbol = random.nextInt(10);
}
int gls = random.nextInt(2);
for(int i=0; i<countsymbol; i++) {
List<String> list = null;
if(gls == 0) {list = alphabetS; gls=1;}
else if(gls == 1) {list = alphabetG; gls=0;}
if (i==0) {name += list.get(random.nextInt(list.size())).toUpperCase();}
name += list.get(random.nextInt(list.size()));
}
}
private void createNumberPhone() {
for(int i=0; i<10; i++) {
if(i==3 || i==6 || i==8) {phone += "-";}
phone += random.nextInt(10);
}
}
}