其中@Configuration相当于xml配置文件。
来写个例子试验一下呗,先创建一个空的项目,选maven但不选模板,然后看看pom.xml啥亚子噻。
4.0.0 org.example 001-springboot-pre1.0-SNAPSHOT org.springframework spring-context5.3.1 junit junit4.12 maven-compiler-plugin 3.8.0 1.8 1.8 UTF-8
再在创建的模块下面创建一个类com.rll.vo.Student
package com.rll.vo;
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 + ''' +
'}';
}
}
创建beans.xml
创建一个测试类:
package com.rll;
import com.rll.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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这种方式用一个类来代替这个beans.xml
原本文件目录结构:
用这种方式,首先你得先有自定义的类来代替这个beans.xml配置文件的作用,类上加@Configuration注解,之后还要使用@Bean。
1、创建类config.SpringConfig
package com.rll.config;
import com.rll.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfig {
@Bean
public Student createStudent(){
Student s1= new Student();
s1.setAge(90);
s1.setName("zyy");
s1.setSex("man");
return s1;
}
}
测试一哈呗:
package com.rll;
import com.rll.config.SpringConfig;
import com.rll.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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);
}
@Test
public void test02(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student=(Student)ctx.getBean("createStudent");
System.out.println("使用JavaConfig来创建的bean对象:"+student);
}
}
执行test02的方法结果:
@Bean,不指定对象的名称,默认是方法名是 id
* 指定对象在容器中的名称(等价于:指定的id属性) *@Bean的name属性,指定对象的名称(id)
package com.rll.config;
import com.rll.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
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;
}
}
package com.rll;
import com.rll.config.SpringConfig;
import com.rll.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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);
}
@Test
public void test02(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student=(Student)ctx.getBean("createStudent");
System.out.println("使用JavaConfig来创建的bean对象:"+student);
}
@Test
public void test03(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student=(Student)ctx.getBean("ListStudent");
System.out.println("使用JavaConfig来创建的bean对象:"+student);
}
}
运行test03结果:
现在我们来演示一下:
1.新建一个Cat类;
package com.rll.vo;
public class Cat {
private String catId;
private String name;
private Integer age;
@Override
public String toString() {
return "Cat{" +
"catId='" + catId + ''' +
", name='" + name + ''' +
", age=" + age +
'}';
}
public String getCatId() {
return catId;
}
public void setCatId(String catId) {
this.catId = catId;
}
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;
}
}
2.新建一个applicationContext.xml文件
3.注入配置文件
4.写test04方法并运行



