通过代码入门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
原创粉丝点击