Spring入门之HelloSpring

news/2024/7/6 6:28:11

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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架构图:

helloSpring的目录结构:

 

HelloSpring代码:

 1 package com.yl;
 2 
 3 public class HelloSpring {
 4 
 5     private String name;
 6     
 7     public HelloSpring(){
 8         System.out.println("HelloSpring's 构造方法");
 9     }
10     
11     public void setName(String name) {
12         System.out.println("setName:" + name);
13         this.name = name;
14     }
15     
16     public void hello() {
17         System.out.println("Hello : " + name);
18     }
19 
20 }

Main代码:

 1 package com.yl;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         
 9         /*//创建HelloSpring对象
10         HelloSpring helloSpring = new HelloSpring();
11         //为name属性赋值
12         helloSpring.setName("Spring");*/
13         
14         //1.创建Spring的IOC容器--调用bean的构造方法和setName方法
15         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
16         //2.从IOC容器中获取Bean实例
17         HelloSpring helloSpring = (HelloSpring)ctx.getBean("helloSpring");
18         //3.调用hello方法
19         helloSpring.hello();
20     }
21 }

 

applicationContext.xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <!-- 配置bean -->
 7     <bean id="helloSpring" class="com.yl.HelloSpring">
 8         <property name="name" value="Spring"></property>
 9     </bean>
10     
11 </beans>

 

转载于:https://my.oschina.net/yulei1943/blog/1490675


http://www.niftyadmin.cn/n/4596944.html

相关文章

关于koa的起服务

关于koa的起服务 第一步&#xff1a;全局安装 koa-generator npm install koa-generator -g 第二步&#xff1a;使用 koa-generator 生成 koa 项目 # 项目名字为&#xff1a;myproject koa2 myproject # 进入 myproject 项目 cd myproject # 安装 npm install 第四步&#xff1…

ES6学习笔记八:类与继承

一&#xff1a;Class ES6 提供了更接近传统语言的写法&#xff0c;引入了 Class&#xff08;类&#xff09;这个概念&#xff0c;作为对象的模板。通过class关键字&#xff0c;可以定义类。 定义“类”的方法的时候&#xff0c;前面不需要加上function这个关键字&#xff0c;直…

默默前行的livego--基于go语言的rtmp直播服务器

为什么go语言&#xff0c;原因太多了&#xff01; 轻量级协程&#xff0c;随时goroutine方便的channel方便的interface强大的服务器性能简单点GO语言---为服务器而生&#xff01;我们来看看: livego--基于go语言的rtmp直播服务器github地址: https://github.com/runner365/liv…

Vue 的自我模拟面试

vue的响应系统 vue的mvvm框架&#xff0c;当数据模型data变化时&#xff0c;页面视图会响应更新 原理对data的getter/setter方法进行拦截&#xff0c; 利用发布订阅的设计模式。 在getter中进行订阅&#xff0c;在setter方法中发布通知&#xff0c;让所有订阅者 完成响应。在响…

WIN32_FIND_DATA 详细结构(附循环读取文件代码)

//去除路径最后多余的斜杠和反斜杠 std::string TrimPath(std::string path) {//string test3("内容"); 使用引用字符数组作为参数传给构造函数std::string illegal(" \t\f\v\n\r\\/");//string 赋值size_t pos path.find_last_not_of(illegal);//从后往…

不公平才是世界的真相----摘自罗辑思维

不公平才是世界的真相如果让你来模拟上帝。给世界上全部人分配財富。你必须依照什么原则分配&#xff0c;才会得到一个与真实世界差点儿相同的结果呢&#xff1f;首先你不可能均匀分配&#xff0c;否则世界上就不会有穷人和富人的差别。你可能会考虑随心情分配&#xff0c;今天…

vue通讯e

在这里插入代码片 父级向子级通讯(利用props)父级&#xff1a;data(){return{**tit:加油加油 &#xff01;**}}子级&#xff1a;props:[tit] 接收<val1 :tit"tit"></val1>子级向父级通讯(利用回调函数)子级&#xff1a;this.$emit(solgan,this.val)父级&…

项目上线相关配置配置 https服务

项目上线相关配置 使用pm2管理应用 1在服务器安装 pm2 :npm i pm2 -g 启动项目 pm2 start 脚本 --name 自定义名称 查看运行项目 pm2ls 重启项目 pm2 restart 自定义名称 停止项目 pm2stop自定义名称 删除项目pm2delete自定义名称 ***## 在这里插入代码片*** 配置 https服务 ht…