通过代码入门Spring①何为IOC
2016-06-04 12:41
621 查看
HelloWord.java
Student.java
App.java
项目下载http://download.csdn.net/detail/bojie5744/9541005
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
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- 详解Java的MyBatis框架和Spring框架的整合运用
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解