Spring框架学习(1)——HelloWorld
来源:互联网 发布:做淘宝客服工资怎么样 编辑:程序博客网 时间:2024/06/05 02:53
1.创建一个名为spring-01-hello的Maven项目,结构如下
2.引入依赖jar包,代码如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yc</groupId> <artifactId>spring-01-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-01-hello</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 引入spring框架的依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> <scope>runtime</scope> </dependency> </dependencies></project>
3.创建实体类Hello.java和Person.java
package com.yc.spring;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component("hello") //创建一个Hello这个类的对象,交给spring容器管理,对象名为hello@Scope("prototype")//原型对象,默认为单例对象public class Hello { private String name="无名氏"; @Autowired //自动注入对象 private Person person; public Hello() { System.out.println("我是Hello的无参构造方法...."); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public void sayHello(){ System.out.println(String.format("%s 对 %s说 你好帅!!", name,person.getName())); } @Override public String toString() { return "Hello [name=" + name + "]"; }}
package com.yc.spring;import org.springframework.stereotype.Component;@Component("person")public class Person { private String name="帅哥"; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [name=" + name + "]"; }}
4.创建Spring配置文件spring.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描bean包 就是由spring容器创建好的对象--> <!-- 制定可以作为spring容器管理的对象的包 --> <context:component-scan base-package="com.yc.spring" /></beans>
5.创建测试类HelloTest.java,代码如下
package com.yc.spring;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloTest { @Test public void testIOC() { Hello h=new Hello(); System.out.println(h); } @Test public void testIOC02() { ApplicationContext cxt=new ClassPathXmlApplicationContext("spring.xml"); System.out.println("======================"); Hello h01=(Hello) cxt.getBean("hello"); System.out.println(h01); h01.sayHello(); System.out.println("======================"); Hello h=(Hello) cxt.getBean("hello"); System.out.println(h); h.sayHello(); if (h01==h) { System.out.println("同一对象"); }else { System.out.println("不同对象"); } }}
阅读全文
0 0
- Spring框架学习(1)——HelloWorld
- Spring框架学习——HelloWorld
- Spring框架学习(一)——HelloWorld
- Spring框架学习(1):环境配置以及HelloWorld
- Spring框架学习笔记--HelloWorld(二)
- SpringMVC框架学习(1)——HelloWorld
- Spring Data 学习(1)—— HelloWorld
- spring学习笔记(1)——创建spring项目之HelloWorld
- 1. 笔记JAVA框架学习——Spring入门环境搭建及helloworld
- spring笔记(1)——Spring介绍和helloWorld
- Spring —— HelloWorld
- spring框架创建helloworld
- Spring框架HelloWorld程序
- Spring学习一:基本框架图介绍与HelloWorld程序
- Spring学习之路——简单入门HelloWorld
- 3、SSM框架-Spring第一个Spring HelloWorld(3)
- 学习框架——Spring
- Spring+Maven——HelloWorld
- 代理模式
- Ubuntu下安装Express框架
- 四种解析XML文件的方法
- 理解c++STL regex_research()
- maven实战(六)使用Nexus创建私服
- Spring框架学习(1)——HelloWorld
- HDU 3697(H) ——Selecting courses(暴力枚举,贪心)
- Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
- Android AOP实现模块间通信
- HDU1873 看病要排队【模拟+优先队列】
- jquery给input赋值val(val)
- 实现二叉树的前序/中序/后序递归、非递归遍历
- Ubuntu使用apt-file解决库或者文件缺失依赖
- 设置线程池的大小