动手试试呗:1.创建一个Tiger类
package com.rll.vo;
public class Tiger {
private String name;
private Integer age;
@Override
public String toString() {
return "Tiger{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
2.新建一个config.properties文件
3.要用中文的话就要设置file-->settings-->
4.完善config.properties文件
5.在Tiger类对象上加注解@Component("tiger")
- 在每个属性上加上@Value("${tiger.属性名}")
6.然后在SpringConfig.java里面的类名上面加上@PropertySource(value="文件名")
这里要找注解,就得扫描组件
@ComponentScan(basePackages = "com.rll.vo")
so:
package com.rll.config;
import com.rll.vo.Student;
import org.springframework.context.annotation.*;
@Configuration
@ImportResource(value = "classpath:applicationContext.xml")
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.rll.vo")
public class SpringConfig {
//不指定对象的名称,默认的方法名是id
@Bean
public Student createStudent(){
Student s1= new Student();
s1.setAge(90);
s1.setName("zyy");
s1.setSex("man");
return s1;
}
@Bean(name = "ListStudent")
public Student makeStudent(){
Student s1= new Student();
s1.setAge(90);
s1.setName("lisi");
s1.setSex("man");
return s1;
}
}
测试一下呗



