使用Java的方式配置Spring
- Component注解将实体类注入到Spring容器中、Value注解进行属性注入
@Component
public class student {
@Value("zhangsan")
private String name;
@Value("111111111")
private String num;
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", num='" + num + '\'' +
'}';
}
}
- Configuration注解代表该类是一个配置类,相当于bean.xml
@Configuration
public class config {
@Bean
public student getstudent(){
return new student();
}
}
- 测试类
public class mytest {
@Test
public void test(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(config.class);
student student = context.getBean( com.practice.dao.student.class);
System.out.println(student);
}
}