當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring之HelloWorld
生活随笔
收集整理的這篇文章主要介紹了
Spring之HelloWorld
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
搭建 Spring 開發環境
把以下 jar 包加入到下:
工程的 classpath?
Spring 的配置文件: 一個典型的 Spring 項目需要創建一個或多個 Bean 配置文件, 這些配置文件用于在 Spring IOC 容器里配置 Bean. Bean 的配置文件可以放在 classpath 下, 也可以放在其它目錄下.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- id:bean的唯一標識class:指定全類名 反射的方式創建對象: Class cls = Class.forName("com.learn.spring.beans.HelloWorld")Object obj = cls.newInstance(); // 需要提供默認的構造器.property: 通過set方法給指定的屬性賦值--><bean id="helloWorld" class="com.learn.spring.beans.HelloWorld"><property name="name1" value="Jerry"></property></bean> </beans> package com.learn.spring.beans;public class HelloWorld {private String name ;public HelloWorld() {System.out.println("invoke HelloWorld Constructor.....");}public String getName() {return name;}public void setName1(String name) {System.out.println("invoke HelleWorld setName......");this.name = name;} public void sayHello(){System.out.println("my name 's " + name );}} package com.learn.spring.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {/*//1.new 對象HelloWorld helloWorld = new HelloWorld();//2.給name屬性賦值helloWorld.setName("Tom");*///1.獲取IOC容器ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2.從IOC容器中獲取對象.//HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld"); //3.調用方法helloWorld.sayHello(); } }?
總結
以上是生活随笔為你收集整理的Spring之HelloWorld的全部內容,希望文章能夠幫你解決所遇到的問題。