Spring-IOC

结构:

HelloWorld.java:

package com.spring.test.helloWorld;

public interface HelloWorld {
    
    public void sayHello();
     
}

HelloWorldService.java:

package com.spring.test.helloWorld;

public class HelloWorldService {
      
    private HelloWorld helloWorld;
  
    public HelloWorldService() {
  
    }
  
    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }
  
    public HelloWorld getHelloWorld() {
        return this.helloWorld;
    }
  
}

SpringHelloWorld.java:

package com.spring.test.helloWorld.impl;

import com.spring.test.helloWorld.HelloWorld;

public class SpringHelloWorld implements HelloWorld {
      
    @Override
    public void sayHello() {
        System.out.println("Spring Say Hello!!");
    }
  
}

StrutsHelloWorld.java:

package com.spring.test.helloWorld.impl;

import com.spring.test.helloWorld.HelloWorld;

public class StrutsHelloWorld implements HelloWorld {
      
    @Override
    public void sayHello() {
        System.out.println("Struts Say Hello!!");
    }
  
}

beans.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="springHelloWorld"
        class="com.spring.test.helloWorld.impl.SpringHelloWorld"></bean>
    <bean id="strutsHelloWorld"
        class="com.spring.test.helloWorld.impl.StrutsHelloWorld"></bean>
  
    <bean id="helloWorldService"
        class="com.spring.test.helloWorld.HelloWorldService">
        <property name="helloWorld" ref="springHelloWorld"/>
    </bean>
  
</beans>

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring</groupId>
  <artifactId>Spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
 
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
  </dependencies>
</project>

HelloProgram.java:

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.helloWorld.HelloWorld;
import com.spring.test.helloWorld.HelloWorldService;
public class HelloProgram {
    public static void main(String[] args) {
        
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");//读取beans.xml 文件来创建一个应用程序上下文对象
         
        HelloWorldService service =
             (HelloWorldService) context.getBean("helloWorldService");
          
        HelloWorld hw= service.getHelloWorld();
         
        hw.sayHello();
    }
}

运行HelloProgram.java

修改beans.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="springHelloWorld"
        class="com.spring.test.helloWorld.impl.SpringHelloWorld"></bean>
    <bean id="strutsHelloWorld"
        class="com.spring.test.helloWorld.impl.StrutsHelloWorld"></bean>
  
    <bean id="helloWorldService"
        class="com.spring.test.helloWorld.HelloWorldService">
        <property name="helloWorld" ref="strutsHelloWorld"/><!-- IoC容器创建strutsHelloWorld对象并通过setter方法注入依赖 -->
    </bean>
  
</beans>

运行HelloProgram.java:

 

转载于:https://www.cnblogs.com/Alwaysbecoding/p/6942145.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值