@component表明这个类将被ioc容器扫描装配bean
@value指定具体属性的值
@componentscan扫描当前包和其子包或者指定扫描包@ComponentScan("com.hengrui.entity")
package com.hengrui.entity.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("user")
public class User {
@Value("324")
private Integer id;
@Value("lisi")
private String name;
@Value("note_2")
private String note;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", note=" + note + "]";
}
}
package com.hengrui.entity.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class AppConfig {
}
package com.hengrui;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.hengrui.entity.config.AppConfig;
import com.hengrui.entity.config.User;
/**
* 读取配置文件
* @author Administrator
*
*/
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
User user = context.getBean(User.class);
log.info(user.getId()+"");
}
}