Java 我应该在什么时候调用getBeansFromFiles(),以便创建BeanFactory和自动连接Bean都能正常工作?
因此,我使用Spring生成随机故事,在控制器的第73行得到一个NullPointerException,在这里我调用ApplicationContext,在函数中创建一个BeanFactory,从文本文件中获取bean(MadLib和PartOfSpeech类)。在我添加该函数之前,上下文工作得很好。怎么了 编辑:我确实知道NullPointerException是什么。我不知道为什么会发生在这里。此外,当我注释掉构造函数时,它也会起作用。我想问题可能是我试图在上下文存在之前调用它 编辑2:我尝试将对getBeansFromFiles()的调用移动到第一个GetMapping(链接)。这带走了NullPointerException,可能是因为控制器bean已经初始化,所以上下文已经存在。但是,当MadLibs被添加到列表中时,bean没有被创建,或者至少没有自动连接。有没有一个地方我可以把这个电话放在双方都能用的地方?我是否需要实现一个在正确的时间进行调用的接口Java 我应该在什么时候调用getBeansFromFiles(),以便创建BeanFactory和自动连接Bean都能正常工作?,java,spring,spring-mvc,Java,Spring,Spring Mvc,因此,我使用Spring生成随机故事,在控制器的第73行得到一个NullPointerException,在这里我调用ApplicationContext,在函数中创建一个BeanFactory,从文本文件中获取bean(MadLib和PartOfSpeech类)。在我添加该函数之前,上下文工作得很好。怎么了 编辑:我确实知道NullPointerException是什么。我不知道为什么会发生在这里。此外,当我注释掉构造函数时,它也会起作用。我想问题可能是我试图在上下文存在之前调用它 编辑2:我
package controllers;
import...
@Controller
@RequestMapping(value = "/madLib")
public class MadLibController {
@Autowired
ApplicationContext context;
public MadLibController(){
getBeansFromFiles();
}
public PartOfSpeech pos(String path){
String input;
try {
input = utility.Util.readFile(path);
} catch (IOException e) {
e.printStackTrace();
input = "v";
}
return new PartOfSpeech(input);
}
public MadLib ml(String path){
String input;
System.out.println(new File("").getAbsolutePath());
try {
input = utility.Util.readFile(path);
} catch (IOException e) {
e.printStackTrace();
input = "v#V#v";
}
return new MadLib(input);
}
public void getBeansFromFiles() {
AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
File folder = new File("../../txt/ml");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles){
System.out.println("File path: " + file.getPath());
MadLib ml = ml(file.getPath());
String beanName = ml.getId();
beanFactory.autowireBean(ml);
beanFactory.initializeBean(ml, beanName);
((MadLibList)context.getBean("madLibList")).getList().add(ml);
}
folder = new File("../../txt/pos");
listOfFiles = folder.listFiles();
for (File file : listOfFiles){
System.out.println("File path: " + file.getPath());
PartOfSpeech pos = pos(file.getPath());
String beanName = pos.getId();
beanFactory.autowireBean(pos);
beanFactory.initializeBean(pos, beanName);
}
}
/*@Bean("verb")
public PartOfSpeech verb(){
return pos("verb");
}
@Bean("hansel2")
public MadLib hansel2(){
return ml("hansel");
}
@Bean("lunch")
public MadLib lunch(){
return ml("lunch");
}*/
@GetMapping
public String links(Model model){
MadLibList list = (MadLibList)context.getBean("madLibList");
//list.getList().add(hansel2());
//list.getList().add(lunch());
model.addAttribute("madLibList", list);
return "madLibLinks";
}
@GetMapping("/{name}")
public String prompts(HttpServletRequest req, HttpServletResponse res,
Model model, @PathVariable("name") String name,
@RequestParam(value = "random", defaultValue = "false") String random){
MadLib madLib = (MadLib)context.getBean(name);
madLib.setAnswers(new ArrayList<String>(madLib.getPrompts().size()));
System.out.println("Answers: " + madLib.getAnswers());
if (random.equals("true")){
System.out.println("Prompts: " + madLib.getPrompts());
for (int i=0; i<madLib.getPrompts().size(); i++){
try {
String posBean = utility.Util.camelCase(madLib.getPrompts().get(i));
PartOfSpeech pos = (PartOfSpeech)context.getBean(posBean);
String word = pos.getWord();
System.out.println(word);
madLib.getAnswers().add(word);
} catch (Exception e) {
System.out.println("exception in randomizing answers for " + madLib.getPrompts().get(i));
System.out.println(e);
madLib.getAnswers().add("");
}
}
}
model.addAttribute("default", "");
model.addAttribute("madLib", madLib);
return "madLibPrompts";
}
@PostMapping(value = "/result")
public String result(Model model, @ModelAttribute("madLib") MadLib madLib, BindingResult result){
System.out.println(madLib.getAnswers().get(0));
//model.addAttribute(madLib);
return "madLibResult";
}
}
包控制器;
导入。。。
@控制器
@请求映射(value=“/madLib”)
公共类控制器{
@自动连线
应用上下文上下文;
公共控制器(){
getBeansFromFiles();
}
公共部分语音位置(字符串路径){
字符串输入;
试一试{
输入=utility.Util.readFile(路径);
}捕获(IOE异常){
e、 printStackTrace();
输入=“v”;
}
返回新的语音部分(输入);
}
公共MadLib ml(字符串路径){
字符串输入;
System.out.println(新文件(“”.getAbsolutePath());
试一试{
输入=utility.Util.readFile(路径);
}捕获(IOE异常){
e、 printStackTrace();
输入=“v#v#v”;
}
返回新的MadLib(输入);
}
public void getBeansFromFiles(){
AutowireCapableBeanFactory beanFactory=context.getAutowireCapableBeanFactory();
文件夹=新文件(“../../txt/ml”);
File[]listOfFiles=folder.listFiles();
对于(文件:listOfFiles){
System.out.println(“文件路径:+File.getPath());
MadLib ml=ml(file.getPath());
字符串beanName=ml.getId();
蚕豆工厂。自动蚕豆(ml);
beanFactory.初始化Bean(ml,beanName);
((MadLibList)context.getBean(“MadLibList”).getList().add(ml);
}
文件夹=新文件(“../../txt/pos”);
listOfFiles=folder.listFiles();
对于(文件:listOfFiles){
System.out.println(“文件路径:+File.getPath());
PartOfSpeech pos=pos(file.getPath());
字符串beanName=pos.getId();
autowireBean(pos);
beanFactory.initializeBean(位置,beanName);
}
}
/*@Bean(“动词”)
公共词性动词{
返回位置(“动词”);
}
@豆子(“汉塞尔2”)
公共MadLib hansel2(){
返回ml(“hansel”);
}
@豆子(“午餐”)
公共午餐{
返回ml(“午餐”);
}*/
@GetMapping
公共字符串链接(模型){
MadLibList=(MadLibList)context.getBean(“MadLibList”);
//list.getList().add(hansel2());
//list.getList().add(午餐());
model.addAttribute(“madLibList”,list);
返回“madLibLinks”;
}
@GetMapping(“/{name}”)
公共字符串提示(HttpServletRequest-req、HttpServletResponse-res、,
模型模型,@PathVariable(“名称”)字符串名称,
@RequestParam(value=“random”,defaultValue=“false”)字符串随机){
MadLib MadLib=(MadLib)context.getBean(name);
setAnswers(新的ArrayList(madLib.getPrompts().size());
System.out.println(“Answers:+madLib.getAnswers());
if(random.equals(“true”)){
System.out.println(“提示:+madLib.getPrompts());
对于(int i=0;i实现BeanDefinitionRegistryPostProcessor并使用beanFactory而不是上下文
package controllers;
import...
@Controller
@RequestMapping(value = "/madLib")
public class MadLibController implements BeanDefinitionRegistryPostProcessor {
@Autowired
ApplicationContext context;
ConfigurableListableBeanFactory beanFactory;
private BeanDefinitionRegistry registry;
public PartOfSpeech pos(String path){
String input;
try {
input = utility.Util.readFile(path);
} catch (IOException e) {
e.printStackTrace();
input = "v";
}
return new PartOfSpeech(input);
}
public MadLib ml(String path){
String input;
System.out.println(new File("").getAbsolutePath());
try {
input = utility.Util.readFile(path);
} catch (IOException e) {
e.printStackTrace();
input = "v#V#v";
}
return new MadLib(input);
}
public void getBeansFromFiles() {
File folder = new File("../../txt/ml");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles){
System.out.println("File path: " + file.getPath());
MadLib ml = ml(file.getPath());
String beanName = ml.getId();
beanFactory.registerSingleton(beanName, ml);
((MadLib)beanFactory.getBean(beanName)).setId(ml.getId());
((MadLib)beanFactory.getBean(beanName)).setTitle(ml.getTitle());
((MadLib)beanFactory.getBean(beanName)).setAnswers(ml.getAnswers());
((MadLib)beanFactory.getBean(beanName)).setPrompts(ml.getPrompts());
((MadLib)beanFactory.getBean(beanName)).setStrings(ml.getStrings());
((MadLibList)beanFactory.getBean("madLibList")).getList().add(ml);
}
folder = new File("../../txt/pos");
listOfFiles = folder.listFiles();
for (File file : listOfFiles){
System.out.println("File path: " + file.getPath());
PartOfSpeech pos = pos(file.getPath());
String beanName = pos.getId();
beanFactory.registerSingleton(beanName, pos);
((PartOfSpeech)beanFactory.getBean(beanName)).setName(pos.getName());
((PartOfSpeech)beanFactory.getBean(beanName)).setWords(pos.getWords());
}
}
@GetMapping
public String links(Model model){
MadLibList list = (MadLibList)beanFactory.getBean("madLibList");
model.addAttribute("madLibList", list);
return "madLibLinks";
}
@GetMapping("/{name}")
public String prompts(HttpServletRequest req, HttpServletResponse res,
Model model, @PathVariable("name") String name,
@RequestParam(value = "random", defaultValue = "false") String random){
MadLib madLib = (MadLib)beanFactory.getBean(name);
//System.out.println(madLib);
madLib.setAnswers(new ArrayList<String>(madLib.getPrompts().size()));
System.out.println("Answers: " + madLib.getAnswers());
if (random.equals("true")){
System.out.println("Prompts: " + madLib.getPrompts());
for (int i=0; i<madLib.getPrompts().size(); i++){
try {
String posBean = utility.Util.camelCase(madLib.getPrompts().get(i));
PartOfSpeech pos = (PartOfSpeech)beanFactory.getBean(posBean);
String word = pos.getWord();
System.out.println(word);
madLib.getAnswers().add(word);
} catch (Exception e) {
System.out.println("exception in randomizing answers for " + madLib.getPrompts().get(i));
System.out.println(e);
e.printStackTrace();
madLib.getAnswers().add("");
}
}
}
model.addAttribute("default", "");
model.addAttribute("madLib", madLib);
return "madLibPrompts";
}
@PostMapping(value = "/result")
public String result(Model model, @ModelAttribute("madLib") MadLib madLib, BindingResult result){
System.out.println(madLib.getAnswers().get(0));
//model.addAttribute(madLib);
return "madLibResult";
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
this.beanFactory = arg0;
getBeansFromFiles();
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry arg0) throws BeansException {
this.registry = arg0;
}
}
包控制器;
导入。。。
@控制器
@请求映射(value=“/madLib”)
公共类MadLibController实现BeanDefinitionRegistryPostProcessor{
@自动连线
应用上下文上下文;
可配置列表beanFactory beanFactory;
私人BeanDefinitionRegistry;
公共部分语音位置(字符串路径){
字符串输入;
试一试{
输入=utility.Util.readFile(路径);
}捕获(IOE异常){
e、 printStackTrace();
输入=“v”;
}
返回新的语音部分(输入);
}
公共MadLib ml(字符串路径){
字符串输入;
System.out.println(新文件(“”.getAbsolutePath());
试一试{
输入=utility.Util.readFile(路径);
}捕获(IOE异常){
e、 printStackTrace();
输入=“v#v#v”;
}
返回新的MadLib(输入);
}
public void getBeansFromFiles(){
文件夹=新文件(“../../txt/ml”);
File[]listOfFiles=folder.listFiles();
对于(文件:listOfFiles){
System.out.println(“文件路径:+File.getPath());
MadLib ml=ml(file.getPath());
字符串beanName=ml.getId();
beanFactory.registerSingleton(beanName,ml);
((MadLib)beanFactory.getBean(beanName)).setId(ml.getId());
((MadLib)beanFactory.getBean(beanName)).setTitle(ml.getTitle());
((MadLib)beanFactory.getBean(beanName)).setAnswers(ml.getAnswers());
((MadLib)beanFactory.getBean(beanName)).setPrompts(ml.getPrompts());
((MadLib)beanFactory.getBean(beanName)).setString(ml.getStrings());
((MadLibList)beanFactory.getBean(“MadLibList”).getList().add(ml);
}
文件夹=新文件(“../../txt/pos”);