Spring ioc,Spring-IOC

 2023-10-08 阅读 29 评论 0

摘要:結構: 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;p

結構:

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;}}

Spring ioc,SpringHelloWorld.java:

package com.spring.test.helloWorld.impl;import com.spring.test.helloWorld.HelloWorld;public class SpringHelloWorld implements HelloWorld {@Overridepublic 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 {@Overridepublic 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/beanshttp://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();}
}

Spring詳解。運行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/beanshttp://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:

@SpringBootApplication、?

轉載于:https://www.cnblogs.com/Alwaysbecoding/p/6942145.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/131312.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息