在做spring、springmvc项目的时候需要写大量的配置文件,比较麻烦,在大型的项目,对象较多,写的配置文件较多,容易出错,还需要继承第三方的框架,需要写配置文件,比如说写一个mybatis,需要在spring的配置文件中写mybatis集成的对象,做每个对象的配置才能用。Spring也发现了这个问题,spring想改变这种格局,少点写配置,直接去做项目功能,所以打造除了springboot这个框架,解决了spring、sporingmvc的痛点,几乎很少写配置文件,他可以把大多数的框架和第三方资源都已经写好了配置,直接在我们项目中来使用。
用springboot可以直接实现spring、springmvc那些功能,而不用在写配置文件了,极大的 提高了开发效率,减轻了我们使用配置文件的难度,springboot相当于一个更加快速的spring+springmvc
目录:
(1)通过配置文件创建对象:
(2)使用JavaConfig配置容器:配置Bean
(3)@ImportResource的使用:读入xml配置文件
(4)@PropertyResource读入配置文件
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.bjpowernode</groupId>
<artifactId>001-springboot-pre</artifactId>
<version>1.0.0</version>
<dependencies>
<!--spring依赖-->
<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>
</project>
(1)通过配置文件创建对象:
创建spring配置文件
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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--声明bean对象,并初始化-->
<bean id="myStudent" class="com.bjpowernode.vo.Student">
<property name="name" value="李四"/>
<property name="age" value="20"/>
<property name="sex" value="女"/>
</bean>
</beans>
Student类:
package com.bjpowernode.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 + '\'' +
'}';
}
}
测试类:
MyTest:
package com.bjpowernode;
import com.bjpowernode.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
/*
* 使用xml作为容器配置文件
* */
@Test
public void test01(){
String config="beans.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(config);//获取工厂
Student student = (Student) ctx.getBean("myStudent");
System.out.println("容器中的对象:"+student);
}
}
(2)使用JavaConfig配置容器
创建配置Bean Java类:SpringConfig:
package com.bjpowernode.config;
import com.bjpowernode.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //声明注解,配置Bean,代替spring配置文件,就是用来配置容器的
public class SpringConfig {
/*
* 创建方法,方法的返回值是对象,在方法的上面加入@Bean
* 方法的返回值对象就注入到容器中
*
* @Bean:把对象注入到spring容器中,作用相当于<bean>标签
* 说明:@Bean,不指定对象的名称,默认是方法名是id
* */
@Bean
public Student createStudent(){
Student s1=new Student();
s1.setName("张三");
s1.setAge(26);
s1.setSex("男");
return s1;
}
/*
* 指定对象在容器中的名称(指定<bean>的id属性)
* @Bean的Name属性,指定对象的名称(id)
* */
@Bean(name = "lisiStudent")
public Student makeStudent(){
Student s2=new Student();
s2.setName("李四");
s2.setAge(20);
s2.setSex("男");
return s2;
}
}
MyTest:
package com.bjpowernode;
import com.bjpowernode.config.SpringConfig;
import com.bjpowernode.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 {
/*
* 使用xml作为容器配置文件
* */
@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");//方法名字作为默认的id
System.out.println("使用JavaConfig创建bean对象:"+student);
}
//给对象取名字:@Bean(name = "lisiStudent")
@Test
public void test03(){
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);//获得工厂
Student student = (Student) ctx.getBean("lisiStudent");//方法名字作为默认的id
System.out.println("使用JavaConfig创建bean对象:"+student);
}
}
test02:
test03:
(3)@ImportResource的使用:读入xml配置文件
Cat类:通过applicationContext.xml配置
package com.bjpowernode.vo;
public class Cat {
private String cardId;
private String name;
private Integer 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;
}
@Override
public String toString() {
return "Cat{" +
"cardId='" + cardId + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建一个需要导入的配置文件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.bjpowernode.vo.Cat">
<property name="name" value="tom猫"/>
<property name="age" value="2"/>
<property name="cardId" value="uw521651451"/>
</bean>
</beans>
配置Bean中导入:配置文件:
SpringConfig:
package com.bjpowernode.config;
import com.bjpowernode.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration //声明注解,配置Bean,代替spring配置文件,就是用来配置容器的
@ImportResource(value = "classpath:applicationContext.xml") //通过注解引入applicationContext配置文件
//@ImportResource(value = {"classpath:applicationContext.xml","classpath:beans.xml"}) //通过{} 可以导入多个配置文件
public class SpringConfig {
/*
* 创建方法,方法的返回值是对象,在方法的上面加入@Bean
* 方法的返回值对象就注入到容器中
*
* @Bean:把对象注入到spring容器中,作用相当于<bean>标签
* 说明:@Bean,不指定对象的名称,默认是方法名是id
* */
@Bean
public Student createStudent(){
Student s1=new Student();
s1.setName("张三");
s1.setAge(26);
s1.setSex("男");
return s1;
}
/*
* 指定对象在容器中的名称(指定<bean>的id属性)
* @Bean的Name属性,指定对象的名称(id)
* */
@Bean(name = "lisiStudent")
public Student makeStudent(){
Student s2=new Student();
s2.setName("李四");
s2.setAge(20);
s2.setSex("男");
return s2;
}
}
测试类测试MyTest:
//测试注解@ImportResource 导入xml配置文件
@Test
public void test04(){
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
Cat cat = (Cat) ctx.getBean("myCat");
System.out.println("cat==="+cat);
}
输出结果证明applicationContest导入成功
(4)@PropertyResource读入配置文件
Tiger:属性值来自属性配置文件
package com.bjpowernode.vo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("tiger") //注解创建Tiger对象,就不用写<bean>标签创建了
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 +
'}';
}
}
创建config.properties配置文件:
tiger.name=东北虎 tiger.age=3
在配置Bean中加入注解:SpringConfig:@PropertySource @ComponentScan
package com.bjpowernode.config;
import com.bjpowernode.vo.Student;
import org.springframework.context.annotation.*;
@Configuration //声明注解,配置Bean,代替spring配置文件,就是用来配置容器的
@ImportResource(value = "classpath:applicationContext.xml") //通过注解引入applicationContext配置文件
//@ImportResource(value = {"classpath:applicationContext.xml","classpath:beans.xml"}) //通过{} 可以导入多个配置文件
@PropertySource(value = "classpath:config.properties") //注解读取到properties配置文件
@ComponentScan(basePackages = "com.bjpowernode.vo") //老虎对象使用注解创建的,告诉容器去哪找这个对象上面的注解,使用包扫描
public class SpringConfig {
/*
* 创建方法,方法的返回值是对象,在方法的上面加入@Bean
* 方法的返回值对象就注入到容器中
*
* @Bean:把对象注入到spring容器中,作用相当于<bean>标签
* 说明:@Bean,不指定对象的名称,默认是方法名是id
* */
@Bean
public Student createStudent(){
Student s1=new Student();
s1.setName("张三");
s1.setAge(26);
s1.setSex("男");
return s1;
}
/*
* 指定对象在容器中的名称(指定<bean>的id属性)
* @Bean的Name属性,指定对象的名称(id)
* */
@Bean(name = "lisiStudent")
public Student makeStudent(){
Student s2=new Student();
s2.setName("李四");
s2.setAge(20);
s2.setSex("男");
return s2;
}
}
测试类:MyTest:
//测试@PropertySource注解,引入配置文件的使用 使用@Component创建对象
@Test
public void test05(){
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
Tiger tiger = (Tiger) ctx.getBean("tiger");
System.out.println("tiger==="+tiger);
}
说明:@Configuration就相当于xml,在类上面写的注解就相当于在原来的Spring配置文件中写的: