lm高会影响卵泡质量吗:spring的ioc实现和例子
来源:百度文库 编辑:偶看新闻 时间:2024/04/30 21:07:49
2.spring的ioc容器实现
使用spring的方式,创建一个由spring ioc容器控制的ioc的应用程序。
1)定义用于接口,打印“hello,world”消息
Java代码
2)实现这个接口
Java代码
3)setter injection
Java代码
4)配置依赖注入关系
在applicationContext.xml中配置:
Java代码
5)创建测试类
Java代码
运行测试类,控制台输出:Hello,World! ◆编写第一个Spring程序
HelloWorld接口:
Java代码
HelloWorldBean实现类:
Java代码
Spring XML配置文件ioc-config.xml:
Xml代码
测试类:
Java代码
步骤小结
①利用XmlBeanFactory读取xml配置文件并建立BeanFactory实例
②BeanFactory依据配置文件完成依赖注入
③通过getBean()方法指定Bean名称取得Bean实例
使用spring的方式,创建一个由spring ioc容器控制的ioc的应用程序。
1)定义用于接口,打印“hello,world”消息
Java代码
- public interface MessageProvider {
- public String getMessage();
- }
public interface MessageProvider {public String getMessage();}
2)实现这个接口
Java代码
- public class MessageHello implements MessageProvider {
- public String getMessage() {
- String message = "hello,world";
- return message;
- }
- }
public class MessageHello implements MessageProvider {public String getMessage() {String message = "hello,world";return message;}}
3)setter injection
Java代码
- public class HelloWorld {
- private MessageProvider provider;
- public void setProvider(MessageProvider provider) {
- this.provider = provider;
- }
- public void showMessage() {
- System.out.println(provider.getMessage());
- }
- }
public class HelloWorld {private MessageProvider provider;public void setProvider(MessageProvider provider) {this.provider = provider;}public void showMessage() {System.out.println(provider.getMessage());}}
4)配置依赖注入关系
在applicationContext.xml中配置:
Java代码
- "1.0" encoding="UTF-8"?>
- "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
"helloBean" class="test.spring.HelloWorld"> "provider"> - "helloProvider"/>
"helloProvider" class="test.spring.MessageHello">
5)创建测试类
Java代码
- public class ShowHello {
- public static void main(String[] args){
- try{
- XmlBeanFactory factory =new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));
- HelloWorld messageReader = (HelloWorld)factory.getBean("helloBean");
- messageReader.showMessage();
- }catch(Exception ex){
- System.out.println(ex.toString());
- }
- }
- }
public class ShowHello {public static void main(String[] args){try{XmlBeanFactory factory =new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));HelloWorld messageReader = (HelloWorld)factory.getBean("helloBean");messageReader.showMessage();}catch(Exception ex){System.out.println(ex.toString());}}}
运行测试类,控制台输出:Hello,World! ◆编写第一个Spring程序
HelloWorld接口:
Java代码
- /**
- *
- * @Copyright(C),2009-2010 SISE Java Team.
- * @Author:easinchu
- * @Email:easinchu@gmail.com
- * @Description:
- */
- public interface HelloWorld {
- public void sayHello();
- }
/**** @Copyright(C),2009-2010 SISE Java Team.* @Author:easinchu* @Email:easinchu@gmail.com* @Description:*/public interface HelloWorld {public void sayHello();}
HelloWorldBean实现类:
Java代码
- /**
- *
- * @Copyright(C),2009-2010 SISE Java Team.
- * @Author:easinchu
- * @Email:easinchu@gmail.com
- * @Description:
- */
- public class HelloWorldBean implements HelloWorld{
- private String helloWorld;
- public void setHelloWorld(String helloWorld) {
- this.helloWorld = helloWorld;
- }
- public void sayHello() {
- System.out.println(helloWorld);
- }
- }
/**** @Copyright(C),2009-2010 SISE Java Team.* @Author:easinchu* @Email:easinchu@gmail.com* @Description:*/public class HelloWorldBean implements HelloWorld{private String helloWorld;public void setHelloWorld(String helloWorld) {this.helloWorld = helloWorld;}public void sayHello() {System.out.println(helloWorld);}}
Spring XML配置文件ioc-config.xml:
Xml代码
id="helloWorldBean" class="cn.com.sise.firstapp.HelloWorldBean"> -
name="helloWorld"> -
Hello,Welcome To Spring World!
Hello,Welcome To Spring World!
测试类:
Java代码
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- /**
- *
- *@Copyright(C),2009-2010 SISE Java Team.
- *@Author:easinchu
- *@Email:easinchu@gmail.com
- *@Description:采用Spring的BeanFactory构造IoC容器.
- */
- public class FirstSpringDemo {
- public static void main(String []args) {
- //-----------BeanFactory IoC容器---------------------//
- //从classpath路径上装载XML的配置信息
- Resource resource = new ClassPathResource("ioc-config.xml");
- //实例化IOC容器,此时容器并未实例化beans-config.xml所定义的各个受管bean.
- BeanFactory factory = new XmlBeanFactory(resource);
- /
- //获取受管bean
- HelloWorld hello = (HelloWorld)factory.getBean("helloWorldBean");
- hello.sayHello();
- }
- }
import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;/****@Copyright(C),2009-2010 SISE Java Team.*@Author:easinchu*@Email:easinchu@gmail.com*@Description:采用Spring的BeanFactory构造IoC容器.*/public class FirstSpringDemo {public static void main(String []args) {//-----------BeanFactory IoC容器---------------------////从classpath路径上装载XML的配置信息Resource resource = new ClassPathResource("ioc-config.xml");//实例化IOC容器,此时容器并未实例化beans-config.xml所定义的各个受管bean.BeanFactory factory = new XmlBeanFactory(resource);///获取受管beanHelloWorld hello = (HelloWorld)factory.getBean("helloWorldBean");hello.sayHello();}}
步骤小结
①利用XmlBeanFactory读取xml配置文件并建立BeanFactory实例
②BeanFactory依据配置文件完成依赖注入
③通过getBean()方法指定Bean名称取得Bean实例
谁有struts+hibernate+spring的例子?
IOC的意思
IOC的含义是什么
spring 框架的介绍和好处?
使用spring框架的好处和不足?
Struts、Spring和Hibernate的学习该如何安排?
久石让spring的钢琴谱
Struts和spring分别是什么?
为了理想付出但理想未实现的例子
合作的名言和例子
网络适配器的定义和例子
网络适配器的定义和例子
有关坚持的正面例子和反面例子各三个
翻译:IOC
Thomas Nashe 的诗歌spring,the sweet spring 诗歌赏析
有关Spring的英语短文
spring outlook 的中文含意
关于 spring 的 小文章
那里能下载到off spring cowboy 和net king cole的歌?
用ASP和SQL2000做网站,如何实现站内搜索?我在说明中举了详细例子.
有关学习方法的例子和文章
生物链的相关知识和例子
关于取长补短的例子和名言
关于执着的例子和名句