使用@autowire注入bean后的null值

xoefb8l8  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(269)

当使用autowire注入bean并调用getter时,我得到了null值。但是,如果我使用ctx.getbean(name,class)(请参阅application.java),那么值就在那里并且是正确的,因此我知道bean正在被创建。我只是无法在hellocontroller类中使用@autowire访问它
应用程序上下文.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());

    }

}

Hello控制器

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)
am46iovg

am46iovg1#

因为您使用的是注解,所以不应该使用xml标记,删除它。不应该在服务内部调用控制器,这是不正确的。在控制器2的注解中表示相同的东西,只保留控制器

相关问题