“@Value 用法”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
第1行: 第1行:
1
+
UserService.java<syntaxhighlight lang="java">
 +
package io.github.jihch.service;
 +
 
 +
import org.springframework.beans.factory.annotation.Value;
 +
import org.springframework.stereotype.Component;
 +
 
 +
@Component
 +
public class UserService {
 +
 
 +
@Value("${name}")
 +
private String test;
 +
 +
public void test() {
 +
System.out.println(test);
 +
}
 +
 +
}
 +
</syntaxhighlight>AppConfig.java<syntaxhighlight lang="java">
 +
package io.github.jihch;
 +
 
 +
import org.springframework.context.annotation.ComponentScan;
 +
import org.springframework.context.annotation.PropertySource;
 +
 
 +
@ComponentScan("io.github.jihch")
 +
@PropertySource("classpath:spring.properties")
 +
public class AppConfig {
 +
 
 +
}
 +
</syntaxhighlight>

2024年7月23日 (二) 12:34的版本

UserService.java

package io.github.jihch.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class UserService {

	@Value("${name}")
	private String test;
	
	public void test() {
		System.out.println(test);
	}
	
}

AppConfig.java

package io.github.jihch;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

@ComponentScan("io.github.jihch")
@PropertySource("classpath:spring.properties")
public class AppConfig {

}