Spring框架学习--Spring环境搭建及helloword的实现

Spring:给软件行业带来春天........
理念:整合现有技术使现有技术更加实用,本身是大杂烩
优点:
1、轻量级框架
2、ioc面向切面编程
3、对事务的支持
4、对框架的支持

Spring环境搭建:

1、进入官网https://spring.io/projects

2、选择一个版本下载通常是选择最新

3、导入java工程

4、新建一个bean.xml文件

5、写入代码

扫描二维码关注公众号,回复: 9464508 查看本文章

新建一个hello.java类

package com.bean;

public class Hello {

	private String name;
	
	public Hello(String name) {
		super();
		this.name = name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void show(){
		System.out.println("hello"+name);
	}

}

新建一个test.java类

package com.test;

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

import com.bean.Hello;
import com.bean.User;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	   //解析beans.xml 
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
	    
		Hello hello = (Hello)context.getBean("hello"); 
		
	    hello.show();
	    
	    
	   
	}

}

在bean.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 java对象 由spring容器创建和管理 -->
	
			<!-- 无参创建对象 -->
	
	<bean name="hello" class="com.bean.Hello">
		<property name="name" value="Spring"></property>
	</bean>   
	
	
</beans>
发布了89 篇原创文章 · 获赞 58 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/sm16111/article/details/89413584
今日推荐