动手试试呗: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:表示当前类是作为配置文件使用的,就是用来配置容器的
* 位置:在类的上面
*
* SpringConfig这个类就相当于beans.xml
*
* */
@Configuration
@ImportResource(value = "classpath:applicationContext.xml")
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.rll.vo")
public class SpringConfig {
/*
*创建方法,方法的返回值是对象。在方法的上面加入@Bean
*方法的返回值对象就注入到容器中
*
* @Bean:把对象注入到spring容器中
* 作用相当于<bean>
* 位置:方法的上面
*
* 说明:@Bean,不指定对象的名称,默认是方法名是 id
* */
//不指定对象的名称,默认的方法名是id
@Bean
public Student createStudent(){
Student s1= new Student();
s1.setAge(90);
s1.setName("zyy");
s1.setSex("man");
return s1;
}
/*
* 指定对象在容器中的名称(等价于:指定<bean>的id属性)
*@Bean的name属性,指定对象的名称(id)
* */
@Bean(name = "ListStudent")
public Student makeStudent(){
Student s1= new Student();
s1.setAge(90);
s1.setName("lisi");
s1.setSex("man");
return s1;
}
}
测试一下呗