什么是Spring
- Spring是一个开源框架。
- Spring是为简化企业级开发应用而生的。使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能。
- Spring是一个IOC(DI)和AOP容器框架。
Spring的特点
-
轻量级:Spring是非侵入性的。基于Spring开发的应用中的对象可以不依赖于Spring的API。
-
依赖注入:DI(Dependency Injection)
-
面向切面编程:AOP(Aspect-oriented Programming)
-
容器: Spring是一个容器,因为它包含并管理应用对象的生命周期。
-
站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库。
Spring模块
Spring Hello World
创建HelloWorld类
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void Hello(){
System.out.println("Hello " + name);
}
}
编写Spring配置文件
<?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 id="helloWorld" class="com.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
</beans>
主类中调用HelloWorld方法
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) applicationContext.getBean("helloWorld");
hello.Hello();
}
}
网友评论