星空网 > 软件开发 > Java

Spring基础——一个简单的例子

一、学习版本 spring-framework-4.0.0

二、导入 jar 包:

Spring基础——一个简单的例子

三、在类路径下创建 Spring Config 文件:ApplicationContext.

<??><beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

四、创建一个非侵入的 Bean

/** * @author solverpeng * @create 2016-07-15-10:09 */public class HelloWorld {  private String userName;  public void setUserName(String userName) {    this.userName = userName;  }  public void hello() {    System.out.println("hello:" + userName);  }}

五、在 Spring Config 文件中配置该 Bean

<??><beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  <bean id="helloWorld" class="com.nucsoft.spring.helloworld.HelloWorld">    <property name="userName" value="spring"/>  </bean></beans>

六、通过 IOC 容器对象来获取在 Spring Config 文件中配置的 Bean,并调用其方法

public static void main(String[] args) {  ApplicationContext ctx = new ClassPath);  HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");  helloWorld.hello(); // 输出 hello:spring}

七、总结

两个重点:

1.在 Spring Config 中配置 Bean

2.通过 IOC 容器中获取 Sring Config 配置的 Bean 对象

 




原标题:Spring基础——一个简单的例子

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

独立站卖家:二十万美金的侵权惨痛教训!:https://www.ikjzd.com/articles/110906
超级干货!详解外贸询盘分析与跟进技巧!:https://www.ikjzd.com/articles/110907
为什么要优化listing?优化listing能给亚马逊带来什么?:https://www.ikjzd.com/articles/110908
你有多重视产品,你就有多少市场份额!:https://www.ikjzd.com/articles/110909
怎样玩转亚马逊listing的SEO?这里有小窍门:https://www.ikjzd.com/articles/110910
旺季干货:如何备战lazada双12?:https://www.ikjzd.com/articles/110911
亚马逊测评风控严吗?应该怎么做?:https://www.xlkjsw.com/news/88171.html
6个技巧帮助你的谷歌搜索广告投放快速出单:https://www.kjdsnews.com/a/1840804.html
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流