package com.liyang.entity;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class People implements Runnable {
protected boolean isStop ;
@Override
public void run() {
while(! isStop){
nextYear() ;
try {
slepp() ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected void slepp() throws InterruptedException {
Thread.sleep(500) ;
}
protected abstract void nextYear() ;
}
package com.liyang.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class Man extends People {
private String name ;
private String sex ;
private int age ;
public String getName() {
return name;
}
@Value("#{liyang['manname']}")
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
@Value("#{liyang.mansex}")
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
@Value("#{liyang['age']}")
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Man [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
public Man() {
// TODO Auto-generated constructor stub
}
@Override
protected void nextYear() {
// TODO Auto-generated method stub
System.out.println("man=" + name + ",age" + age++);
}
}
package com.liyang.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class Woman extends People {
private String name ;
private String sex ;
private int age ;
public String getName() {
return name;
}
@Value("#{liyang['womanname']}")
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
@Value("#{liyang.womansex}")
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
@Value("#{liyang['age']}")
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Man [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
public Woman() {
// TODO Auto-generated constructor stub
}
@Override
protected void nextYear() {
// TODO Auto-generated method stub
System.out.println("woman=" + name + ",age" + age++);
}
}
package com.liyang.task;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.liyang.entity.Man;
import com.liyang.entity.People;
import com.liyang.entity.Woman;
@Service
public class TaskExecutor {
private String info = null ;
@Autowired
protected BeanFactory beanFactory;
public String getInfo() {
return info;
}
@Value("#{liyang.info}")
public void setInfo(String info) {
this.info = info;
}
@PostConstruct
private void init(){
People p = beanFactory.getBean("man" , Man.class) ;
new Thread(p).start() ;
p = beanFactory.getBean("woman" , Woman.class) ;
new Thread(p).start() ;
}
}
package com.liyang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringContainer {
@Autowired
public static ApplicationContext context ;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext(new String[]{"applicationContest-base.xml"}) ;
}
}