1 什么是 JavaConfig
JavaConfig: 是 Spring 提供的使用 java 类配置容器。 配置 Spring IOC 容器的纯 Java 方法。
优点:
1.可以使用面像对象的方式, 一个配置类可以继承配置类,可以重写方法
2.避免繁琐的 xml 配置
2 Xml 配置容器
创建001-pre-boot 项目
pom.xml
<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.5.1</version>
<!-- 编译级别 -->
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- 编码格式 -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
创建数据类 Student
public class Student{
private Integer id;
private String name;
private Integer age;
}
resources 目录下创建 Spring 的配置文件 applicationContext.xml
<bean id="myStudent" class="com.bjpowernode.vo.Student">
<property name="id" value="1001" />
<property name="name" value="李强国" />
<property name="age" value="20" />
</bean>
单元测试:
@Test
public void test01(){
String config="applicationContext.xml";
ApplicationContext ctx=new
ClassPathXmlApplicationContext(config);
Student student = (Student) ctx.getBean("myStudent");
System.out.println("student="+student);
}
3 JavaConfig 配置容器
JavaConfig 主要使用的注解
@Configuration:放在类的上面, 这个类相当于 xml 配置文件,可以在其中声明 bean
@Bean:放在方法的上面, 方法的返回值是对象类型, 这个对象注入到 spring ioc 容器
创建配置类(等同于 xml 配置文件)
@Configuration
public class SpringConfig {
@Bean
public Student createStudent(){
Student student = new Student();
student.setId(1002);
student.setName("周仓");
student.setAge(29);
return student;
}
@Bean(name = "myStudent2")
public Student makeStudent(){
Student student = new Student();
student.setId(1003);
student.setName("诸葛亮");
student.setAge(30);
return student;
}
@Bean
public Date myDate(){
return new Date();
}
}
}
测试方法:
@Test
public void test02(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ctx.getBean("createStudent");
System.out.println("student==="+student);
}
@Test
public void test03(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ctx.getBean("myStudent2");
System.out.println("myStudent2==="+student);
Object myDate = ctx.getBean("myDate");
System.out.println("myDate = " + myDate);
}
}
4 @ImportResource
@ImportResource 是导入 xml 配置,等同于 xml 文件的 resources 创建数据类:
public class Cat {
private String cardId;
private String name;
private Integer age;
}
创建配置文件 beans.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:
http:
<bean id="myCat" class="com.bjpowernode.vo.Cat">
<property name="cardId" value="XSET29001" />
<property name="name" value="tom 猫"/>
<property name="age" value="3" />
</bean>
</beans>
创建配置类:
@Configuration
@ImportResource(value = {"classpath:beans.xml","classpath:applicationContext.xml"})
public class SystemConfig {
}
创建测试方法:
@Test
public void test04(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemConfig.class);
Cat cat = (Cat) ctx.getBean("myCat");
System.out.println("cat==="+cat);
}
5 @PropertyResource
@PropertyResource 是读取 properties 属性配置文件
在 resources 目录下创建 config.properties
tiger.name=东北老虎
tiger.age=6
创建数据类 Tiger
@Component("tiger")
public class Tiger {
@Value("${tiger.name}")
private String name
@Value("${tiger.age}")
private Integer age;
@Override
public String toString() {
return "Tiger{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}}
修改 SystemConfig 文件
@Configuration
@PropertySource(value = "classpath:config.properties")
@ComponentScan(value = "com.bjpowernode.vo")
public class SystemConfig {
}
创建测试方法
@Test
public void test05(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemConfig.class);
Tiger tiger = (Tiger) ctx.getBean("tiger");
System.out.println("Tiger==="+tiger);
}
|