spring(一)helloworld

来源:互联网 发布:js截取字符串后四位 编辑:程序博客网 时间:2024/05/17 01:58

Spring概述:

1.轻量级:非侵入性的。对象可以不依赖于Spring的API

2.依赖注入:

3.AOP面向切面编程

4.容器:包含并且管理应用对象的生命周期

5.框架:

6.一站式:可以整合其他框架


Spring模块

HelloWorld:

1.导入jar包:



2.HelloWorld类:

public class HelloWorld {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public void Say(){System.out.println("helloworld:"+name);}}

3.applicationContext.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.xsd"><!-- 配置bean --><bean id="HelloWorld" class="com.milan.beans.HelloWorld"><property name="name" value="milan"></property></bean></beans>

4.Main方法中调用HelloWorld的say()方法

public class Main {public static void main(String[] args) {//1.创建IOC容器对象ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2.从IOC容器中获取bean实例HelloWorld helloWorld = (HelloWorld) ctx.getBean("HelloWorld");//注:未使用spring的方法//HelloWorld helloWorld = new HelloWorld();//helloWorld.setName("milan");//3.调用hello方法helloWorld.Say();}}






0 0