目录
一:Xml 和 JavaConfig
1. JavaConfig
(1)为什么要使用 Spring Boot?
(2)Xml 和 JavaConfig
(3)什么是 JavaConfig?
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>com.zl</groupId>
<artifactId>study-springboot-001</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<!--引入spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!--引入junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</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>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
Student类
package com.zl.springboot.pojo;
public class Student {
private String name;
private Integer age;
private String sex;
public Student() {
}
public Student(String name, Integer age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + 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;
}
}
第一种:使用传统的XML配置文件的方式管理Bean
spring.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="studentBean" class="com.zl.springboot.pojo.Student">
<property name="name" value="张三"/>
<property name="age" value="18" />
<property name="sex" value="男"/>
</bean>
</beans>
单元测试:
package com.zl.springboot.test;
import com.zl.springboot.pojo.Student;
import javafx.application.Application;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringBootTest {
@Test
public void test01(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student studentBean = applicationContext.getBean("studentBean", Student.class);
System.out.println(studentBean);
}
}
第二种:使用JavaConfig来管理Bean对象
创建一个Java类,在类上引入@Configuration注解,在方法上引入@Bean注解
package com.zl.springboot.config;
import com.zl.springboot.pojo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 当前类作为配置文件使用
public class SpringConfig {
@Bean // 不指定默认id是方法名
// @Bean("myStudent") // 也可以自己指定id
public Student createStudent(){
Student student = new Student();
student.setName("李四");
student.setAge(19);
student.setSex("女");
return student; // 返回这个对象
}
}
单元测试:
@Test
public void test02(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
// 使用默认的,id就是方法名
Student student = applicationContext.getBean("createStudent", Student.class);
// 使用自己指定的id
Student student = applicationContext.getBean("myStudent", Student.class); /
System.out.println(student);
}
2. @ImportResource注解
<!--导入其它配置文件-->
<import resources="其他配置文件"/>
@ImportResource源码:有两个数组参数,value和locations,互为别名,效果是等价的
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.core.annotation.AliasFor;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
public @interface ImportResource {
@AliasFor("locations")
String[] value() default {};
@AliasFor("value")
String[] locations() default {};
Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;
}
Cat类
package com.zl.springboot.pojo;
/**
* @Author:朗朗乾坤
* @Package:com.zl.springboot.pojo
* @Project:spring-boot
* @Date:2023/3/2 18:54
*/
public class Cat {
private String cardId;
private String name;
private Integer age;
public Cat() {
}
public Cat(String cardId, String name, Integer age) {
this.cardId = cardId;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"cardId='" + cardId + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
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;
}
}
catbean.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="studentBean" class="com.zl.springboot.pojo.Cat">
<property name="cardId" value="1111" />
<property name="name" value="大红" />
<property name="age" value="8" />
</bean>
</beans>
把catbean.xml核心配置,通过@ImportResource注解引入到SpringConfig类当中
单元测试:
@Test
public void test03(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Cat catBean = applicationContext.getBean("catBean", Cat.class);
System.out.println(catBean);
}
3. @PropertyResource注解
<!--引入外部配置文件-->
<properties resource="jdbc.properties"/>
config.properties属性配置文件
tiger.name=东北虎
tiger.age=3
Tiger类:使用注解式开发
package com.bjpowernode.vo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("tiger") // 交给Spring容器管理
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 +
'}';
}
}
在SpringConfig类当中引入包扫描和使用@PropertyResource注解引入外部属性文件
单元测试:
@Test
public void test05(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Tiger tiger = applicationContext.getBean("tiger", Tiger.class);
System.out.println(tiger);
}
如果此时出现了中文乱码,进行以下设置:
其实以下注解就等同与在xml配置中配置以下信息
@Configuration // 代表xml配置
@ImportResource(value = "classpath:catbean.xml") // 导入其它xml配置
@PropertySource(value = "classpath:config.properties") // 引入外部属性配置文件
@ComponentScan(basePackages = "com.zl.springboot.pojo") // 扫描包,带有@Component注解的就可以被创建出来
与上面是等价的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:catbean.xml" />
<context:property-placeholder location="classpath:config.properties" />
<context:component-scan base-package="com.zl.springboot.pojo" />
</beans>