Spring Boot应用程序属性 @Value

huangapple go评论71阅读模式
英文:

Spring Boot application properties @Value

问题

@SpringBootApplication
public class NhlTeamsApplication {

	public static void main(String[] args) {
		SpringApplication.run(NhlTeamsApplication.class, args);
	}

}

// 我的控制器
public class NhlTeamsController {

    @Autowired
    private NhlTeamsService nhlTeamsService;

    @RequestMapping(method = RequestMethod.GET, value = "/requestAllTeams")
    public @ResponseBody String requestAllTeams() {
        return nhlTeamsService.allTeamsService();

    }
}

// 我的接口
@Component
public interface NhlTeamsService {

    String allTeamsService();
}

// 实现类
@Service
@Data
@Configuration
@PropertySource("classpath:application.properties")
public class NhlTeamsServiceImpl implements NhlTeamsService{

    @Value("${nhl.endpoint}")
    private String endpoint;

    @Override
    public String allTeamsService() {

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(getEndpoint(), String.class);
    }
}

// application.properties
nhl.endpoint=https://statsapi.web.nhl.com/api/v1/teams

// JUnitTest
@Test
public void nhlAllTeamsTest(){

    NhlTeamsServiceImpl nhlTeamsService = new NhlTeamsServiceImpl();
    System.out.println(nhlTeamsService.allTeamsService());

}
英文:

I'm trying to create a simple Spring Boot Application, creating a GET using resttemplate.getForObject and instead of hard coding the url within the parameter, I want to create a string and leverage @Value to call the url from application.properties. I've checked videos and the board (https://stackoverflow.com/questions/53482633/value-in-springboot-returns-null) and every time I run my JUnit test the endpoint is null. I was wondering if someone can take a look at my code and point out what am I doing incorrectly, why is @Value always null and what can I do to fix this?

@SpringBootApplication
public class NhlTeamsApplication {
public static void main(String[] args) {
SpringApplication.run(NhlTeamsApplication.class, args);
}
}
//my controller
public class NhlTeamsController {
@Autowired
private NhlTeamsService nhlTeamsService;
@RequestMapping(method = RequestMethod.GET, value = "/requestAllTeams")
public @ResponseBody String requestAllTeams() {
return nhlTeamsService.allTeamsService();
}
}
//my interface
@Component
public interface NhlTeamsService {
String allTeamsService();
}
//implementation
@Service
@Data
@Configuration
@PropertySource("classpath:application.properties")
public class NhlTeamsServiceImpl implements NhlTeamsService{
@Value("${nhl.endpoint}")
private String endpoint;
@Override
public String allTeamsService() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject(getEndpoint(), String.class);
}
}
//application.properties
nhl.endpoint=https://statsapi.web.nhl.com/api/v1/teams
//JUnitTest
@Test
public void nhlAllTeamsTest(){
NhlTeamsServiceImpl nhlTeamsService = new NhlTeamsServiceImpl();
System.out.println(nhlTeamsService.allTeamsService());
}

Am I missing any annotations? Any insight is greatly appreciated.

答案1

得分: 1

当您运行测试时,会创建一个新的NhlTeamsServiceImpl对象。但在Spring中,内部有一个Spring容器,在运行时Spring必须使用所有变量进行初始化,并进行存储。

因此,当您创建一个新对象时,它会初始化Spring容器,并且在其中不必初始化所有必需的内容,如@Value变量。

所以在测试用例类中进行以下修改,

class Junit {
    
    @Autowired
    public NhlTeamsServiceImpl nhlTeamsServiceImpl;

    // JUnitTest
    @Test
    public void nhlAllTeamsTest() {
        // NhlTeamsServiceImpl nhlTeamsService = new NhlTeamsServiceImpl();
        System.out.println(nhlTeamsService.allTeamsService());

    }
}

这样它将起作用。

在下面找到可工作的截图。
Spring Boot应用程序属性 @Value

同样的工作代码在这里添加 https://github.com/MaheshMore4321/RunTestCases

英文:

When you running tests that time you create a new object of NhlTeamsServiceImpl.
But in spring have spring container inside that at run time spring had to initialize that object with all variable & stored.

So when you create a new object it will initialize of spring container & in that it doesn't have to initialize all required thing as @value variable

so do one thing in test case class modified as follows,

class junit{
@Autowired
public NhlTeamsServiceImpl nhlTeamsServiceImpl;
//JUnitTest
@Test
public void nhlAllTeamsTest(){
//NhlTeamsServiceImpl nhlTeamsService = new NhlTeamsServiceImpl();
System.out.println(nhlTeamsService.allTeamsService());
}
}

So it will work.

Find below the working snap.
Spring Boot应用程序属性 @Value

& same working code add here https://github.com/MaheshMore4321/RunTestCases

huangapple
  • 本文由 发表于 2020年10月9日 04:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64270408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定