SPRING环境搭建
来源:互联网 发布:ubuntu 16.04 百度云 编辑:程序博客网 时间:2024/06/03 06:03
Spring 是什么?
•Spring 是一个开源框架.
•Spring 为简化企业级应用开发而生.使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能.
•Spring 是一个IOC(DI)和AOP容器框架.
•具体描述 Spring:
–轻量级:Spring是非侵入性的-基于Spring开发的应用中的对象可以不依赖于Spring的API
–依赖注入(DI--- dependencyinjection、IOC)
–面向切面编程(AOP--- aspect oriented programming)
–容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
–框架:Spring实现了使用简单的组件配置组合成一个复杂的应用.在Spring中可以使用XML和Java注解组合这些对象
–一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的SpringMVC和持久层的Spring JDBC)
搭建 Spring开发环境
•把以下 jar包加入到工程的classpath下:
•Spring 的配置文件:一个典型的Spring项目需要创建一个或多个Bean配置文件,这些配置文件用于在SpringIOC容器里配置Bean.Bean的配置文件可以放在classpath下,也可以放在其它目录下.
由于我的eclipse安装了spring插件,因此自动生成的如上图所示。
接下来创建bean,特别注意,bean必须要有一个无参的构造函数
com.pplsunny.HelloWorld.java文件
package com.pplsunny;public class HelloWorld {private String user;public HelloWorld() {System.out.println("HelloWorld's constructor...");}public void setUser(String user) {System.out.println("setUser:" + user);this.user = user;}public HelloWorld(String user) {this.user = user;}public void hello(){System.out.println("Hello: " + user);}}
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.pplsunny.HelloWorld"><!-- 为属性赋值 --><property name="user" value="Jerry"></property></bean></beans>
Main.java
package com.pplsunny;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {//1. 创建 Spring 的 IOC 容器ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2. 从 IOC 容器中获取 bean 的实例,"helloWorld"是applicationContext.xml里的id="helloWorld"HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");//根据类型来获取 bean 的实例: 要求在 IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常. //一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个. // HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);//3. 使用 beanhelloWorld.hello();}}
============
1 0
- spring环境搭建
- Spring环境搭建
- Spring Web环境搭建
- Spring MVC环境搭建
- spring环境搭建
- Spring环境搭建
- 搭建Spring开发环境
- Spring+Struts2环境搭建
- Spring+Hibernate环境搭建
- Spring 环境搭建
- maven 搭建spring环境
- spring mvc环境搭建
- spring环境搭建
- Eclipse+Spring 环境搭建
- Spring MVC 环境搭建
- spring MVC环境搭建
- 搭建spring-framework环境
- Spring环境搭建错误
- Brothers【NOIP2016提高A组模拟9.9】
- 236_自定义抽签器二
- 将一个字符串数组中的字母按照以下规则排列
- 237_自定义抽签器三
- 什么是Zero-Copy?
- SPRING环境搭建
- JQ中的选择器使用
- 面向对象的思考
- hdu5880Family View(AC自动机)
- c++学习框架
- JS-事件模型
- 山丘(李宗盛)
- poj 1012 Joseph
- 238_自定义抽签器四