簡體   English   中英

使用@Autowire 注入 bean 后的 Null 值

[英]Null value after injecting bean using @Autowire

使用 autowire 注入 bean 並調用 getter 時,我得到 null 值。 但是,如果我使用 ctx.getbean(name, class)(請參閱 application.java)來執行此操作,則該值存在且正確,因此我知道正在創建 bean。 我只是無法在 HelloController class 中使用 @Autowire 訪問它

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"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  

<bean id="lion" class= "com.example.springboot.HelloController">
<property name="name" value="John" />
 </bean>
 


 
</beans>  

應用.java

package com.example.springboot;

import java.util.Arrays;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootApplication
@ComponentScan(basePackages={"com.example" } )
public class Application {
    
    public static void main(String[] args) {
       
        //ApplicationContext ctx = SpringApplication.run(Application.class, args);
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        SpringApplication.run(Application.class, args);
        
        
        
        
         String[] beanNames = ctx.getBeanDefinitionNames();
         for (String beanName : beanNames) {
                System.out.println("bean names from xml " + beanName);
                
            }

         testclass x = new  testclass();
    System.out.println(" in here  " + x.getlion());
    
        
         
         
         
         
    }
    
}

你好控制器

package com.example.springboot;

import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//convert to a bean or try autowrining ???

@RestController
@Component
public class HelloController {
    
    String x;
    
    String name;
    

    @Autowired 
    private ObjectMapper mapper;

    
    public HelloController(){
        
    }

    public void setname(String name) {
        this.name=name;
        
    }
    
    public String getname() {
        return this.name;
    }
    
    @RequestMapping("/")
    public String index() {
    
    }
    
        //http://127.0.0.1:8080/hello?name=stephen
        @RequestMapping( value= "/hello" , method= RequestMethod.GET)
        public String hello(@RequestParam("name") String name){

            return "hello " + name;
        }
        

    
    
    @GetMapping("/welcome")
    public Map<String, String> welcome(String name ) {
        HashMap<String, String> map = new HashMap<>();
        return map;
    }
    
    
    
    @GetMapping("/mapper")
    public ObjectNode mapper(String name ) {
        ObjectNode objectNode = mapper.createObjectNode();
        objectNode.put("key", "value");
        objectNode.put("foo", "bar");
        objectNode.put("number", 42);
            return objectNode;
    }
    

    
}

測試類

package com.example.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class testclass {

    
    @Autowired 
    private  HelloController lion;


    
    public  testclass(){
    
    }
    
    public String getlion() {
        return lion.getname();
    }

}

在 application.java 中調用 getter 的錯誤消息

Exception in thread "main" java.lang.NullPointerException
    at com.example.springboot.testclass.getlion(testclass.java:20)
    at com.example.springboot.Application.main(Application.java:41)

因為你使用注解,你不應該使用 xml 標記,刪除它。你不應該在服務內部調用 controller,這是不正確的。 在您的 controller 2 個注釋中表示相同的內容,只留下 controller

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM