java Spring框架IOC(控制反转)

Spring是java web开发中应用很广的java EE框架之一,它的主要思想是IOC(控制反转)和AOP(面向切面编程),它经常和struts、hibernate一起使用,合称ssh

IOC控制反转

IOC也叫做DI(Dependency Injection,依赖注入)。传统程序中,如果要使用一个类,就要new一个对象Person p1 = new Person("Lily");而在Spring中是一种完全不同的方法,下面是个简单的例子(使用maven来管理jar包):

IDao.java:

package com.yeetrack.mavenSpring;
public interface IDao
{
    public String sayHello(String name);
}

实现IDao接口,DaoImpl.java:

package com.yeetrack.mavenSpring;

public class DaoImpl implements IDao
{
    public String sayHello(String name)
    {
        // TODO Auto-generated method stub
        return "Hello, "+name;
    }
}  

新建业务接口,IService.java:

package com.yeetrack.mavenSpring;

public interface IService
{
    public void sayHello(String name);
}

实现IService接口:

package com.yeetrack.mavenSpring;

public class ServiceImpl implements IService
{
    private IDao dao; //定义一个IDao对象

    public void sayHello(String name) //实现IService接口中的方法
    {
        // TODO Auto-generated method stub
        System.out.println(dao.sayHello(name));
    }

    public IDao getDao()
    {
        return dao;
    }

    public void setDao(IDao dao)
    {
        this.dao = dao;
    }
}

下面在applicationContext.xml中配置ServiceImpl和DaoImpl:

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="     
      http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
      http://www.springframework.org/schema/context     
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      http://www.springframework.org/schema/aop     
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<!-- DAO对象 -->
<bean id="daoImpl" class="com.yeetrack.mavenSpring.DaoImpl"></bean>

<!-- Service对象 -->
<bean id="service" class="com.yeetrack.mavenSpring.ServiceImpl">
    <!-- 设置dao属性 -->
    <property name="dao" ref="daoImpl"></property>
</bean>

OK,现在配置完成,我们可以通过applicationContext来获取ServiceImpl实例了。在贴出自己的pom.xml文件内容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://   maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yeetrack</groupId>
    <artifactId>mavenSpring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mavenSpring Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>mavenSpring</finalName>
    </build>
</project>  

测试我们配置的applicationContext文件,新建SpringTest.java文件:

package com.yeetrack.mavenSpring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest
{

    public static void main(String[] args)
    {
        //获取ApplicationContext容器,会默认寻找classpath路径,如果没找到xml文件,也可以手动指定applicationContext.xml文件的路径
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IService hello = (IService) context.getBean("service");
        hello.sayHello("Good Day!");

    }
}

OK,运行即可。在代码运行时,spring会根据applicationContext.xml文件的配置来加载相应的java bean,并通过反射机制调用getter和seter方法设置bean之间的依赖。

java Spring框架IOC(控制反转)
java Spring框架AOP(面向切面编程)

版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=14