01 hello world

早前跟着马士兵老师的视频学习了一下spring,动手比较少,没什么深刻的理解。今天开始从头下手对spring了解一下。

代码结构如下:


image.png


public class HelloWorld {
    private String name;
    public void setname(String name) {
        System.out.println("setname:"+name);
        this.name=name;
    }
    public void hello(){
        System.out.println("hello "+name);
    }
    public HelloWorld(){
        System.out.println("hello world construct");
    }

}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //HelloWorld hellow= new HelloWorld();
        //hellow.setname("Spring test");
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        //HelloWorld hellow = (HelloWorld)ctx.getBean("helloWorld");
        //hellow.hello();
    }

}

bean.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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="helloWorld" class="com.lyc.spring.HelloWorld">
        <property name="name" value="Spring_HelloWorld"></property>
    </bean>

</beans>    

输出结果看spring完成的class的实例化,构造函数。接下来就能直接调用了。

这样就引入了控制反转IOC:
传统都是new一个对象出来调用。现在交给了Spring容器创建需要的对象。通俗来讲有个司机开车需要自己动手,控制反转就是自己不动手开车,而是设置程序自动驾驶

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Go语言的众多特性,是我很长时间以来一直寻找的集合体。一直想在服务器编程方面有一个更舒适的语言。想舒适的实现跨平台...
    厚土火焱阅读 4,088评论 2 5
  • React Native就像React,但它使用本地组件而不是Web组件作为构建块。因此,要理解React Nat...
    wanqijian阅读 2,338评论 0 0
  • 01 今天和朋友c君通了电话,他说家里又逼着他去相亲了。 c君在上学时可谓校园里的风流人物,人高且帅,还喜欢参加各...
    大爱龙猫阅读 2,915评论 0 2
  • 瘦人碗子阅读 1,841评论 1 2
  • 回忆是那盏模糊的蜡烛,忽明忽暗。不经意间在光晕中看清了自己的内心,还是在滴落地烛泪中泪湿襟衫? 大三的时候一次...
    Fri阅读 2,555评论 0 3

友情链接更多精彩内容