本文通过极客学院中Spring的学习,总结视频中的代码实例,自己写的一个例子。
Spring框架的两个核心机制为控制反转IOC和面向切面编程AOP,控制反转的意思是用户创建的类以实体bean的方式注入IOC容器,注入方式有三种:
- setter&getter方式注入
- 构造注入
- 接口注入
IOC容器在控制反转的过程中获得实体bean资源的过程叫做依赖注入。而控制反转本身是程序调用者不直接使用类来创建自己的需求,而且需要实体类的时候通过请求IOC容器,让IOC容器来分配所需要的实体类。这种方式就程序本身并不需要关心实体类的实现细节,只需要通过.xml文件来配置各个类的属性和类之间的联系方式。
实例功能:有一个接口HelloWorldInterface,有两个类实现了这个接口,这两个类分别为MyHelloWorld和YourHelloWorld。还有一个Person类,在Person里面有一个私有的HelloWorldInterface,没有被实例化。现在想在测试函数中通过IOC容器创建一个Person对象,实例化Person类中的HelloWorldInterface,并输出实现类的信息。
代码编写:
- 创建一个Spring工程,创建lib文件夹,加入spring的jar包,然后将jar加入工程,通过build path.具体细节不多做叙述。
- 在src文件夹下创建自己的jar包,比如我的是com.yangwan.beans。代码实现HelloWorldInterface接口、MyHelloWorld类、YourHelloWord类以及Person类。具体代码如下
HelloWorldInterface接口代码
public interface HelloWolrdInterface { public void sayHelloWorld(); }
MyHelloWorld类代码public class MyHelloWorld implements HelloWolrdInterface { @Override public void sayHelloWorld() { System.out.println("This is My HelloWorld"); } }
YourHelloWorld类代码public class YourHelloWorld implements HelloWolrdInterface { @Override public void sayHelloWorld() { System.out.println("This is My HelloWorld"); } }
Person类代码
注解:这里Person类,有一个尚未用实体类实现的接口变量,下面会使用setter getter方式实现实体类注入IOC容器,所以这里必须实现helloMessage的get和set方法。右键点击resource中的generate getter and setter..功能既可以直接快捷实现。public class Person { private HelloWolrdInterface helloMessage; public HelloWolrdInterface getHelloMessage() { return helloMessage; } public void setHelloMessage(HelloWolrdInterface helloMessage) { this.helloMessage = helloMessage; } public void sayHello(){ helloMessage.sayHelloWorld(); } }
3. 在工程目录文件下创建application-config.xml配置文件,通过配置将类以实体bean的方式注入IOC容器。具体代码
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="myhelloword" class="com.yangwan.beans.MyHelloWorld"></bean> <bean id="yourhelloworld" class="com.yangwan.beans.YourHelloWorld"></bean> <bean id="person" class="com.yangwan.beans.Person"> <property name="helloMessage" ref="yourhelloworld"></property> </bean> </beans>
注解:bean标签中的id属性是BeanFactory找到该实体beans的关键属性
property标签设置了person中helloMessage跟两个实现类MyHelloWorld和YourHelloWord的依赖关系。其中name必须和Person类中helloWorld变量表示法一直,否则出现错误。ref的值可以选择上面两个beans的id,如果选择myhelloword,则IOC容器会创建myHelloWorld来实现helloWorld。选择yourhelloworld则创建yourHelloWorld来实现helloWorld。
4.创建Main类,实现测试函数
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import com.yangwan.beans.Person; public class Main { public static void main(String[] args) { Resource resource = new FileSystemResource("application-config.xml"); BeanFactory beanFactory = new XmlBeanFactory(resource); Person personTemp = (Person)beanFactory.getBean("person"); personTemp.sayHello(); } }
5.实例结果因为在配置文件中,property中ref=yourhelloworld。所以IOC容器会通过yourhelloworld的id创建一个YourHelloWorld对象实例化helloWorld。所以控制台输出This is YourHelloWorld.