加载配置文件
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>javase</groupId>
<artifactId>0527</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Spring 需要的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!-- 日志需要的依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
初始化(根据配置项,进行初始化,比如定义的bean进行实例化)
application.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 通过bean标签定义bean对象,Spring Bean容器是通过id来进行管理的 -->
<!-- id相当于bean的名称,Spring可以通过id找到bean对象, 如果没有提供id 会生成自动id
默认是单例模式,只会实例化一个对象 -->
<!-- 通过无参的构造方法,创建一个对象,如果该类型没有无参的构造方法就会报错 -->
<bean id="Josvin" class="java.lang.String"></bean>
<bean class="java.lang.String"><!-- 在这里就会生成一个id 为string 的字符串对象-->
<!---->
</bean>
</beans>
使用(根据容器获取到Bean对象)
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @ClassName Main
* @Description :TODO
* @Author Josvin
* @Date 2021/05/27/19:39
*/
public class Main {
public static void main(String[] args) {
// spring 开启容器的方式:ApplicationContext 应用上下文(可以配置并管理bean对象,等) 父类引用指向子类对象
// ClassPathXmlApplicationContext :根据ClassPath路径,指定一个xml文件(配置文件),
// 根据配置文件完成配置工作(如bean的实例化)
ApplicationContext context = new
ClassPathXmlApplicationContext("applications.xml");
new String();
// 通过Bean的名称获取bean对象,bean名称就是xml中的bean的id
String str = (String) context.getBean("Josvin");
String str2 = (String) context.getBean("java.lang.String#0");
System.out.println(str);
System.out.println(str2);
// 通过类型获取bean对象,如果该类型有多个对象就会报错,只支持一个该类型的对象
//context.getBean(String.class);
}
}
有参构造
定义一个自己的类
package Josvin;
/**
* @ClassName Duck
* @Description :TODO
* @Author Josvin
* @Date 2021/05/27/21:09
*/
public class Duck {
private String name;
private Integer age;
public Duck(String s) {
}
@Override
public String toString() {
return "Duck{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
}
初始化
使用
如何在实例化时注入其属性
有构造方法:constructor-arg(构造方法注入)
无构造方法(属性注入)