日期:2017/12/9
spring是目前最流行的java开源框架。很容易可以知道,它整合了7大组件(spring mvc + spring web + spring aop + spring orm + spring core + spring dao + spring content)如何进行开发呢?下载了 spring-framework-4.1.3.BUILD-SNAPSHOT-dist.zip 后解压,可以查看官方文档:Spring Framework Reference Documentation。
学习spring,不能一脑瓜子的直接copy他人的配置文件,否则会给各个配置文件搞得神魂颠倒,出了问题还一头雾水。通过阅读官方文档,对整个spring框架的主体有了个清晰的把握,开发起来更加得心应手。
1.常规注入(DI): (1)需要在类里面 声明一个成员变量,并且写了一个它的setter方法。这样框架底层才能读取成员变量,并且在配置文件中注入。
(2)利用了类名的反射方式,创建类。class必须具体到包下的类(全类名);
(3)由于使用了反射机制,因此,凡是利用bean配置的类都要设置一个无参构造器;
2.<beans></beans>包含多个<bean></bean>;
3.<bean id=“ ” class=“ ”></bean>,里面可以加入<propertities></propertities>元素。
例子:
三个.java 文件:
(1)Address.java
package com.spring.beans.autowire;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
public Address(String province, String city) {
super();
this.province = province;
this.city = city;
}
public Address() {
super();
// TODO 自动生成的构造函数存根
}
}
(2)Car.java
package com.spring.beans.autowire;
public class Car {
private String brand;
private String number;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", number=" + number + "]";
}
public Car(String brand, String number) {
super();
this.brand = brand;
this.number = number;
}
public Car() {
super();
// TODO 自动生成的构造函数存根
}
}
(3)Person .java
package com.spring.beans.autowire;
public class Person {
private String name;
private Car car;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", car=" + car + ", address=" + address + "]";
}
public Person(String name, Car car, Address address) {
super();
this.name = name;
this.car = car;
this.address = address;
}
public Person() {
super();
// TODO 自动生成的构造函数存根
}
}
(4)spring_autowire.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="car" class="com.spring.beans.autowire.Car"></bean>
<bean id="address" class="com.spring.beans.autowire.Address"></bean>
<bean id="person" class="com.spring.beans.autowire.Person">
<property name="address" ref="address"></property>
<property name="car" ref="car"></property>
<property name="name" value="mmb"></property>
</bean>
</beans>