目录
创建一个maven项目Spring配置解析
创建一个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>spring-HelloWorld
</artifactId>
<version>1.0-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-webmvc
</artifactId>
<version>5.1.10.RELEASE
</version>
</dependency>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>4.12
</version>
<scope>compile
</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java
</directory>
<includes>
<include>**/*.properties
</include>
<include>**/*.xml
</include>
</includes>
<filtering>false
</filtering>
</resource>
<resource>
<directory>src/main/resources
</directory>
<includes>
<include>**/*.properties
</include>
<include>**/*.xml
</include>
</includes>
<filtering>false
</filtering>
</resource>
</resources>
<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>
编写一个Hello实体类
package com
.luming
.beans
;
public class Hello {
private String name
;
private int age
;
public Hello(String name
, int age
) {
this.name
= name
;
this.age
= age
;
}
public Hello() {
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
@Override
public String
toString() {
return "Hello{" +
"name='" + name
+ '\'' +
", age=" + age
+
'}';
}
public void show() {
System
.out
.println(name
);
}
}
创建 Spring 配置文件
<?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="hello" class="com.luming.beans.Hello">
<property name="name" value="HelloWorld"></property>
</bean>
</beans>
测试
public class MyTest {
@Test
public void test() {
ApplicationContext context
=new ClassPathXmlApplicationContext("/bean.xml");
Hello bean
= (Hello
) context
.getBean("hello");
bean
.show();
}
}
hello对象是由Spring创建的hello对象的属性是由Spring容器设置的所谓的控制反转就是: 控制:谁来控制对象的创建,传统应用程序的对象是由程序本身创建的,使用Spring后对象是由Spring创建的 反转:程序本身不创建对象,而是被动接收对象依赖注入:本质是使用set方法注入的。IOC是一种编程思想,有主动的编程变成被动的接收
Spring配置解析
别名 alias 设置别名 , 为bean设置别名 , 可以设置多个别名
<!--设置别名:在获取Bean的时候可以使用别名获取
-->
<alias name
="userT" alias
="userNew"/>
Bean的配置
bean是java对象,由Spring创建和管理
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
id是bean的标识符,要唯一,如果没有配置id,name就是默认标识符 如果配置了id,有配置了name,那么name是别名name可以设置多个别名,可以用逗号,分号,空格分开如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;class是bean的全限定名(即包名+类名)