在spring framework的项目需在一个Bean引用另外一个Bean,且另外一个Bean需要新生成,所以在这里ApplicationContextAware接口就能启重要作用,因为这个有如下方法
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.ctx=ctx;
}
这样可以让具体Bean来访问applicationcontext,从通过可以访问到系统中另外一个Bean且需要生新的Bean,在spring中可以引用非单例Bean,引用方法很单,通过@Scope("prototype")这个来的,以下是我进行的测试,完全没有问题,主要是通过java config类来实现
1.先准一个app.properties文件,有如下内容
students=王天天,张小明,tom,jeffery,张天宠
courses=java,c++,c#,计算机基础
2.其它相关类如下,具体的代码就不说明了,相信可以看明白
package com.edu05;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Student {
private String name;
@Autowired
private ArrayList<Course> courses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void getCourses() {
//System.out.println("course count "+this.courses.size());
for(Course course : courses) {
System.out.println(this.name+" 选择了 "+course.getName()+" 课程 !");
}
}
}
package com.edu05;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class Course {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.edu05;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ChoseService {
@Autowired
private ArrayList<Student> sts;
public void getCoures() {
for(Student st : sts) {
st.getCourses();
}
}
}
package com.edu05;
import java.util.ArrayList;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@ComponentScan
@PropertySource("app.properties")
public class AppConfig implements ApplicationContextAware {
private ApplicationContext ctx;
@Autowired
private Environment env;
@Bean
public ArrayList<Course> initCourse(){
ArrayList<Course> crs=new ArrayList<Course>();
for(String csString: env.getProperty("courses").split(",")) {
Course cs=ctx.getBean(Course.class);
cs.setName(csString);
crs.add(cs);
}
return crs;
}
@Bean
public ArrayList<Student> initStudents(){
ArrayList<Student> sts=new ArrayList<Student>();
for(String stString: env.getProperty("students").split(",")) {
Student st=(Student)ctx.getBean(Student.class);
st.setName(stString);
sts.add(st);
}
return sts;
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.ctx=ctx;
}
}
package com.edu05;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Edu05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);
ChoseService cs=ctx.getBean(ChoseService.class);
cs.getCoures();
//Student st=ctx.getBean(Student.class);
}
}
3.运行Edu05会有如下结果