Spring核心
IoC:控制反转Inversion of Control
AOP:Aspect Oriented programming
依赖注入配置文件介绍
1、 配置Bean的配置文件(文件名字叫什么都可以)
2、 在程序中利用context获取实例对象
如何实现依赖注入
使用IoC容器实现依赖注入DI。
DI依赖注入几种方法:
1. 构造注入(调用构造器)
2. set方法注入(常用)
3. 接口注入
即:在相应的组件里面提供set方法,spring自动调用set方法。
实际操作
如果想使用spring,首先导入jar;spring是一个非全有全无开源框架。spring可以用在普通的java程序,web程序,ejb程序。
1、 编写实体类
2、 在源包下创建文件夹并创建配置文件如:bean.xml,并做配置实体类。
3、 利用ClassPathXmlApplicationContext(“配置文件位置”)获取实体对象。
注意:配置文件中配置的bean,可以选择配置 scope=”prototype/singleton”
prototype:每次获取对象都是创建的新对象。
singleton:每次获取对象都是同一个对象。
bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="address" class="com.justcrazy.www.model.Address">
<property name="city" value="北京" />
<property name="street" value="五棵松路26号" />
</bean>
<bean id="company" class="com.justcrazy.www.model.Company" scope="prototype">
<property name="name" value="嘿嘿嘿" />
<property name="age" value="1" />
<property name="address" ref="address" />
<property name="employees">
<list>
<value>帅哥</value>
<value>美女</value>
<value>女流氓</value>
</list>
</property>
<property name="department">
<set>
<value>耍流氓</value>
<value>服务</value>
</set>
</property>
</bean>
</beans>
Address 类
public class Address {
private String city;
private String street;
public Address() {
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address{" + "city=" + city + ", street=" + street + '}';
}
}
Company类:
public class Company {
private String name;
private int age;
private Address address;
private List employees;
private Set department;
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;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List getEmployees() {
return employees;
}
public void setEmployees(List employees) {
this.employees = employees;
}
public Set getDepartment() {
return department;
}
public void setDepartment(Set department) {
this.department = department;
}
@Override
public String toString() {
return "Company{" + "name=" + name + ", age=" + age + ", address=" + address + ", employees=" + employees + ", department=" + department + '}';
}
}
测试类
public class Test {
public static void main(String[] args) {
//配置文件所在位置为:src目录下config包内
ApplicationContext context = new ClassPathXmlApplicationContext("/config/beans.xml");
Company company = (Company)context.getBean("company");
System.out.println(company);
}
}
打印结果
Company{name=嘿嘿嘿, age=1, address=Address{city=北京, street=五棵松路26号}, employees=[帅哥, 美女, 女流氓], department=[耍流氓, 服务]}