一、前期准备
- 开发工具:eclipse
- Spring框架包(基本只需四个包,其他的在之后需要用到的时候自行添加)
- 第三方依赖包
准备好后,将上面的五个包复制到lib或者Referenced Libraries文件夹内。
二、Spring入门程序(通过它来理解)
1.src下
2.Custom.java
#创建一个接口Custom,然后再接口中定义一个say()函数
package com.example.custom;
public interface Custom {
public void say();
}
3.CustomImplement.java
#创建类CustomImplment,实现接口Custom
package com.example.implement;
import com.example.custom.Custom;;
public class CustomImplement implements Custom{
public void say() {
System.out.println("hello customImplement!");
}
}
4.applicationContext.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">
<!- 关键是在applicationContext.xml中加入一个id为custom的bean ->
<bean id="custom" class="com.example.implement.CustomImplement"/>
</beans>
5.mainpage.java
在mainpage中的main()函数里测试前面的是否成功。
package com.example.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.custom.Custom;
public class mainpage {
public static void main(String[] args) {
#初始化spring容器,加载配置装置
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
# 通过容器获取custom实例
Custom custom=(Custom) applicationContext.getBean("custom");
# 调用实例中say()方法
custom.say();
}
}
6.最后结果(测试成功)
三、实现依赖注入
大致框架如下:com.example.implement下是实现com.example.interfaces下的接口函数。
1.company.java
package com.example.interfaces;
public interface Company {
public void say();
}
2.companyImplement.java
package com.example.implement;
import com.example.interfaces.Company;
import com.example.interfaces.Custom;
public class CompanyImplement implements Company{
# 声明Custom属性
private Custom custom;
# 添加Custom属性的setter方法,用于实现依赖注入
public void setCustom(Custom custom) {
this.custom=custom;
}
# 实现Company中的接口函数
public void say(){
custom.say();
System.out.println("hello company!");
}
}
3.applicationContext.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="custom" class="com.example.implement.CustomImplement"/>
<!- 将id为custom,注入到id为company的bean中去 ->
<bean id="company" class="com.example.implement.CompanyImplement">
<property name="custom" ref="custom"/>
</bean>
</beans>
4.mainpage.java
package com.example.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.interfaces.Company;
public class mainpage {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Company company=(Company) applicationContext.getBean("company");
company.say();
}
}
5.结果显示