首页 > 编程语言 > 详细

Hello spring

时间:2018-08-19 16:08:32      阅读:158      评论:0      收藏:0      [点我收藏+]

步骤:导入相关jar包(记得选中jar包,右键build path)   

      commons-logging-1.2.jar
      spring-aop-4.1.6.RELEASE.jar
      spring-aspects-4.1.6.RELEASE.jar
      spring-beans-4.1.6.RELEASE.jar
      spring-context-4.1.6.RELEASE.jar
      spring-context-support-4.1.6.RELEASE.jar
      spring-core-4.1.6.RELEASE.jar
      spring-expression-4.1.6.RELEASE.jar
      spring-instrument-4.1.6.RELEASE.jar
      spring-jdbc-4.1.6.RELEASE.jar
      spring-orm-4.1.6.RELEASE.jar
      spring-test-4.1.6.RELEASE.jar
      spring-tx-4.1.6.RELEASE.jar
      spring-web-4.1.6.RELEASE.jar
      spring-webmvc-4.1.6.RELEASE.jar

         编写spring配置文件(名称可以自定义)

(废话少说-----:)

Hello.java

package com.yikuan.cn;

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

beans.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.yikuan.cn.Hello">
<property name="name" value="张三"/>
</bean>
</beans>

Test1.java

package com.yikuan.cn.test;

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

import com.yikuan.cn.Hello;

public class Test1 {
public static void main(String[] args) {
//解析beans.xml文件,生成管理相应的bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
}

Hello spring

原文:https://www.cnblogs.com/yikuan-919/p/9501469.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!