本文通过 3 个示例让大家对 spring 有一个直观的感受。
我们可以到 maven 仓库 查看spring的最新版本。
项目基于 Gradle 构建。
示例1
项目结构
├── build.gradle
└── src
└── main
├── java
│ └── demo
│ ├── Main.java
│ └── SayHello.java
└── resources
└── beans.xml
build.gradle
group 'com.example'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
implementation group: 'org.springframework', name: 'spring-context', version: '5.0.6.RELEASE'
}
SayHello.java
package demo;
public class SayHello {
public String sayHello(String name) {
return "Hello, " + name;
}
}
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-2.5.xsd">
<bean id="sayHello" class="demo.SayHello">
</bean>
</beans>
Main.java:
package demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
SayHello sayHello = (SayHello) ctx.getBean("sayHello");
System.out.println(sayHello.sayHello("world"));
}
}
执行结果
运行 Main 类,会输出:
Hello, world
除此之外,也会输出一些 Spring 的信息,例如:
三月 04, 2019 8:00:51 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c6: startup date [Mon Mar 04 08:00:51 CST 2019]; root of context hierarchy
三月 04, 2019 8:00:51 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
示例2
项目结构
├── build.gradle
└── src
└── main
├── java
│ └── demo
│ ├── Main.java
│ └── bean
│ ├── Address.java
│ └── Person.java
└── resources
└── beans.xml
Address.java
package demo.bean;
public class Address {
private String province;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
'}';
}
}
Person.java
package demo.bean;
public class Person {
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"address=" + address +
'}';
}
}
beans.xml
beans.xml
中配置bean,通过property向bean中注入数据:
<?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-2.5.xsd">
<bean id="ltt" class="demo.bean.Person">
<property name="address" ref="address" />
</bean>
<bean id="address" class="demo.bean.Address" scope="singleton">
<property name="province" value="上海"/>
</bean>
</beans>
Main.java
Main 类内容:
package demo;
import demo.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person ltt = (Person) ctx.getBean("ltt");
System.out.println(ltt);
}
}
执行结果
Main 类运行后输出:
Person{address=Address{province='上海'}}
示例3
这里对示例2中代码稍作改动,通过引入@Autowired
注解减少beans.xml
中的配置。autowired 意思是自动装配
。
首先将 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-2.5.xsd">
<!-- 注意这个新引入的 Spring 类 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="ltt" class="demo.bean.Person">
<!-- 这里不再注入其他bean -->
</bean>
<bean id="address" class="demo.bean.Address" scope="singleton">
<property name="province" value="上海"/>
</bean>
</beans>
将 Person 类改成:
package demo.bean;
import org.springframework.beans.factory.annotation.Autowired;
public class Person {
@Autowired
private Address address; // 变量名,不叫 address 也行,这说明默认是基于类型进行注入
@Override
public String toString() {
return "Person{" +
"address=" + address +
'}';
}
}
Main 类内容:
package demo;
import demo.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person ltt = (Person) ctx.getBean("ltt");
System.out.println(ltt);
}
}
运行后,输出:
Person{address=Address{province='上海'}}