其中@Configuration相当于xml配置文件。
来写个例子试验一下呗,先创建一个空的项目,选maven但不选模板,然后看看pom.xml啥亚子噻。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>001-springboot-pre</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--编译插件-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!--插件的版本-->
<version>3.8.0</version>
<!--编译级别-->
<configuration>
<source>1.8</source>
<target>1.8</target>
<!--编码格式-->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
再在创建的模块下面创建一个类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:表示当前类是作为配置文件使用的,就是用来配置容器的
* 位置:在类的上面
*
* SpringConfig这个类就相当于beans.xml
*
* */
@Configuration
public class SpringConfig {
/*
*创建方法,方法的返回值是对象。在方法的上面加入@Bean
*方法的返回值对象就注入到容器中
*
* @Bean:把对象注入到spring容器中
* 作用相当于<bean>
* 位置:方法的上面
* */
@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);
}
/*
* 使用javaConfig
* */
@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
* 指定对象在容器中的名称(等价于:指定<bean>的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:表示当前类是作为配置文件使用的,就是用来配置容器的
* 位置:在类的上面
*
* SpringConfig这个类就相当于beans.xml
*
* */
@Configuration
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;
}
}
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);
}
/*
* 使用javaConfig
* */
@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文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myCat" class="com.rll.vo.Cat">
<property name="name" value="tomcat"/>
<property name="age" value="2"/>
<property name="catId" value="333"/>
</bean>
</beans>
3.注入配置文件
4.写test04方法并运行