目录
前言
一、什么是javaConfig?
二、使用XML文件配置对象到容器
1.Maven工程目录
2.main->java下创建对象类
3.main->resources下创建容器配置文件beans.xml
4.test->java下创建测试类MyTest以及测试方法test01
三、JavaConfig配置容器
1.main->java下创建JavaConfg类SpringConfig,类上面添加注解
2.test->java->MyTest下创建测试方法test02
3.根据别名获取对象
四、@ImportResource
五、@PropertyResource
总结
前言
Spring使用XML类型文件作为容器配置文件,在Spring3.0后加入了JavaConfig,使用java类做配置文件使用。
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是javaConfig?
JavaConfig是Springboot中用到的技术,该技术是用来解决因为Spring,SpringMVC在使用中需要对各种对象进行配置才能使用所带来的繁琐问题。是对XML配置文件进行替代的一种方式,关键就是使用java类配置对象,主要会用到下面两个注解。
优点:
1.可以使用面向对象的方式,一个配置类可以继承配置类,可以重写方法,提高配置文件的复用率
2.避免繁琐的xml配置
使用两个注解实现:
1)@Configuration: 放在一个类上面,声明该类用来充当配置文件使用
2) @Bean: 声明对象,对该对象放到容器中
二、使用XML文件配置对象到容器
1.Maven工程目录
2.main->java下创建对象类
学生类Student:
public class Student {
private String name;
private Integer age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
", sex='" + sex + ''' +
'}';
}
}
3.main->resources下创建容器配置文件beans.xml
beans.xml中配置Student对象:
4.test->java下创建测试类MyTest以及测试方法test01
public class MyTest {
@Test
public void test01(){
String config = "beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
Student student = (Student) ctx.getBean("myStudent");
System.out.println(student);
}
}
三、JavaConfig配置容器
1.main->java下创建JavaConfg类SpringConfig,类上面添加注解
@Configuration
public class SpringConfig {
@Bean
public Student createStudent(){
Student student = new Student();
student.setName("lina");
student.setAge(20);
student.setSex("女");
return student;
}
}
2.test->java->MyTest下创建测试方法test02
1.main->java下创建JavaConfg类SpringConfig,类上面添加注解
@Configuration
public class SpringConfig {
@Bean
public Student createStudent(){
Student student = new Student();
student.setName("lina");
student.setAge(20);
student.setSex("女");
return student;
}
}
2.test->java->MyTest下创建测试方法test02
1) 这里Spring容器对象是基于注解的,应该用 AnnotationConfigApplicationContext()创建;
2) 这里Spring容器获取Student对象时,不设置别名的情况下默认就是方法名
@Test
public void test02(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ctx.getBean("createStudent");
System.out.println("使用JavaConfig创建的对象:"+student);
}
3.根据别名获取对象
1) 根据别名获取对象,只需要在JavaConfig类配置注解@Bean时设置name值即可,如下:
@Bean(name = "lisiStudent")
public Student makeStudent(){
Student student1 = new Student();
student1.setName("lisi");
student1.setAge(20);
student1.setSex("男");
return student1;
}
2) 创建测试方法test03()
@Test
public void test03(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ctx.getBean("lisiStudent");
System.out.println("使用JavaConfig创建的对象:"+student);
}
四、@ImportResource
1)@ImportResource注解用来导入其他的xml文件,提高配置文件的复用率,类似于使用xml配置文件时利用
2)创建applicationContext.xml配置文件并对Cat类进行赋值
vo包下创建Cat类:
public class Cat {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
resources下创建applicationContext.xml:
3)在注解的value中需要指定需要添加的配置文件的类路径
@Configuration
@ImportResource(value = "classpath:applicationContext")
public class SpringConfig {
}
4)测试结果
@Test
public void test04(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Cat cat = (Cat) ctx.getBean("MyCat");
System.out.println("使用JavaConfig创建的对象:"+cat);
}
五、@PropertyResource
1)@PropertyResource注解用来导入属性文件,例如使用jdbc时用到的属性配置文件。
2)步骤:
1.在resources目录下创建属性配置文件
2.在@Propertyresource中指定resources文件的位置
3.使用@Value(value="${key}")获取属性值
3)演示:创建老虎类,并使用外部属性配置文件进行赋值
resources下创建tiger.properties属性配置文件
tiger.name=东北虎 tiger.age=2
创建Tiger类并利用@Value注解对name、age两个属性赋值
这里使用@Component注解代替xml文件进行Tiger类的注册,功能和
一共两种方式,第一种是在xml文件中使用标签
@Component("tiger")
public class Tiger {
@Value(value = "${tiger.name}")
private String name;
@Value(value = "${tiger.age}")
private Integer age;
@Override
public String toString() {
return "Tiger{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
使用@PropertySource()注解导入Tiger类的外部属性配置文件
@Configuration
@ImportResource(value = "classpath:applicationContext.xml")
@PropertySource(value = "classpath:tiger.properties")
@ComponentScan(basePackages = "com.bjnode.vo")
public class SpringConfig {
......
}
4)测试方法
@Test
public void test05(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Tiger tiger = (Tiger) ctx.getBean("tiger");
System.out.println("使用JavaConfig创建的对象:"+tiger);
}
总结
上述内容其实都只有一个目的,就是把要用到的对象存储到容器中方便管理。我们可以从两个角度上来梳理,首先是容器,因为只有造好了容器,才能利用它为我们存东西,也就是关于容器的创建问题;其次,就是如何造好我们的对象,也就是关于对象属性赋值的问题。关于容器的创造,有两种方式,一种是使用xml配置文件,ClassPathXmlApplicationContext(”xml配置文件“)创建容器;一种是使用JavaConfig类,AnnotationConfigApplicationContext(类名.class)创建容器。容器创建好了之后就是对象属性的赋值。关于赋值,有三种方式,第一种是xml配置文件中使用



