通过代码入门Spring①何为IOC
来源:互联网 发布:淘宝免费推广方式 编辑:程序博客网 时间:2024/06/01 09:54
HelloWord.java
package cn.podger.spring.demo1;public class HelloWorld {private String strSomeThingSay = "helloWorld!";public String getSomeThingSay(){ System.out.println(strSomeThingSay); return strSomeThingSay;}public void saySomeThing(String str){ strSomeThingSay = str; System.out.println(strSomeThingSay);}}
Student.java
package cn.podger.spring.demo1;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component@Scope("prototype")public class Student {private String name;private String id;private int age = 100;public String getName() { return name;}public void setName(String name) { this.name = name;}public String getId() { return id;}public void setId(String id) { this.id = id;}public int getAge() { return age;}public void setAge(int age) { this.age = age;}}
App.java
package cn.podger.spring.demo1;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;/** * Spring IOC 功能演示 程序中的之间的关系,不用代码控制,而完全是由容器来控制 * */public class App { public static void main( String[] args ) { /** * FileSystemResource 搜索的是项目工作目录下的地址 本案例为 /spring-demo1文件夹下 */ XmlBeanFactory xbf = new XmlBeanFactory(new FileSystemResource("applicationContext.xml")); HelloWorld hw1 = (HelloWorld)xbf.getBean("helloWorld"); hw1.getSomeThingSay(); hw1.saySomeThing("Spring 你好!"); HelloWorld hw2 = (HelloWorld)xbf.getBean("helloWorld"); hw2.getSomeThingSay(); /** * 输出结果为 * helloWorld! * Spring 你好! * Spring 你好! * 以下关于bean的解析说明了,默认是单例模式,所以以上结果也就能理解了. * The scope of this bean: typically "singleton" (one shared instance, which will be returned by all calls to * getBean with the given id), or "prototype" (independent instance resulting from each call to getBean). * Default is "singleton". Singletons are most commonly used, and are ideal for multi-threaded service * objects. Further scopes, such as "request" or "session", might be supported by extended bean * factories (e.g. in a web environment). Note: This attribute will not be inherited by child bean * definitions. Hence, it needs to be specified per concrete bean definition. Inner bean definitions inherit * the singleton status of their containing bean definition, unless explicitly specified: The inner bean will * be a singleton if the containing bean is a singleton, and a prototype if the containing bean has any * other scope. */ /** * 在applicationContext 修改bean为以下 * <bean id="helloWorld" scope="prototype" class="cn.podger.spring.demo1.HelloWorld"></bean> * 也就是添加了个scope="prototype"属性 意思为每次getBean 为new 对象 * 修改后的结果为 * helloWorld! * Spring 你好! * helloWorld! * 根据以上的解析这输出毋容置疑. */ /** * 另外还有种通过spring注解方法加载的方法 * 在配置文件中添加以下内容 * <context:component-scan base-package="cn.podger.spring.demo1"></context:component-scan> * 意思是扫描@component加载bean * 同<bean id="" class=""> </bean> 的意思 * 代码如下 */ /** *Student student = (Student)xbf.getBean("student"); *System.out.println(student.getAge()); */ }}
项目下载http://download.csdn.net/detail/bojie5744/9541005
0 0
- 通过代码入门Spring①何为IOC
- 通过代码入门Spring②何为AOP
- Spring IOC之通过JAVA代码配置Bean
- 【spring】IOC入门
- Spring入门之IOC
- Spring入门案例 IoC
- Spring IoC入门
- Spring入门 IOC
- 通过Java代码装配bean【Spring 入门】
- 逆向入门--何为OEP
- spring ioc原理(模拟ioc原理代码)
- Spring IOC容器---入门 一
- Spring IoC容器--入门 二
- Spring入门(IOC&AOP)
- Spring中Ioc入门实例
- Spring入门篇之IOC
- spring入门之IOC容器
- Spring入门之IOC,AOP
- Win32 GDI+ 学习笔记(一)之win32显示图片
- Codeforces AIM Tech Round (Div. 2) A. Save Luke
- java集合框架详解
- Android自定义控件之滑动开关
- ffmpeg学习---vs2008下ffmpeg配置
- 通过代码入门Spring①何为IOC
- 《深入理解mybatis原理》 MyBatis缓存机制的设计与实现
- 浅谈html/CSS
- SQL Server Management Studio (SSMS) Studio June 2016
- 裁剪图片(自定义裁剪范围)
- Android 开发那些走不完的配置套路一
- java中的随机数Random
- 【Java】excelSheetColumnNumber
- 《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项