一 Spring是什么
Spring是一个开源框架,为简化企业级开发而生。具体描述如下:
– 轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
– 依赖注入(DI — dependency injection、IOC)
– 面向切面编程(AOP — aspect oriented programming)
– 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
– 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
– 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)。
Spring- HelloWorld
需要将以下jar包导入到工程中classpath中:
Spring jar包下载地址:https://repo.spring.io/libs-release-local/org/springframework/spring/
1.创建java工程
public class HelloWorld {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void Hello() {
System.out.println("hello:"+"userName");
}
}
2、在classpath下创建spring的配置文件:applicationContext.xml。增加bean属性
<bean name="helloWorld" class="com.java.spring.helloworld.HelloWorld">
<property name="userName" value="World"></property>
</bean>
3、创建Main方法,调用Hello()方法
public class Main {
public static void main(String[] args) {
ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld =(HelloWorld) con.getBean("helloWorld");
helloWorld.Hello();
}
}
输出效果: