哪个网站做logo好,福州专业网站搭建排名,不用写代码做网站,查看网站开发平台今天来学习Spring ioc .一、spring jar 包导入 在 spring 官网下载开发包 spring-framework-4.2.4.RELEASE,然后导入需要的 jar 包到项目 /lib/ 目录下。 #xfffc; 二、代码开发 新建一个 src/cn/sxt/bean/Hello.java文件 package cn.sxt.bean;/*** Created by kaiyiwang o… 今天来学习Spring ioc . 一、spring jar 包导入 在 spring 官网下载开发包 spring-framework-4.2.4.RELEASE,然后导入需要的 jar 包到项目 /lib/ 目录下。 二、代码开发 新建一个 src/cn/sxt/bean/Hello.java文件 package cn.sxt.bean;/*** Created by kaiyiwang on 18/5/22.*/
public class Hello {private String name;private void setName(String name){this.name name;}public void show(){System.out.println(hello, name);}
}新建 beans.xml 文件 ?xml version1.0 encodingutf-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd!-- bean就是Java对象 由Spring来创建和管理 --bean namehello classcn.sxt.bean.Helloproperty namename value张三/bean
/beans 3、新建测试文件 Test.java src/cn/sxt/test/Test.java package cn.sxt.test;import cn.sxt.bean.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by kaiyiwang on 18/5/27.*/
public class Test {public static void main(String[] args){// 解析beans.xml文件,生成管理响应的bean对象ApplicationContext context new ClassPathXmlApplicationContext(beans.xml);Hello hello (Hello)context.getBean(hello);hello.show();}
}右键 Run Test.main() 执行编译该文件会报出如下错误: Exception in thread main java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactoryat org.springframework.context.support.AbstractApplicationContext.init(AbstractApplicationContext.java:159) 抛出这样的错误是因为没有引入commons/logging 包所以先到官网 http://struts.apache.org/down... 下载 struts-2.3.34 包 然后引入 commons-logging-1.1.3.jar包到项目src下 引入该 jar包后需要添加为项目库路径否则在写代码引入方法时不能自动提示相关的方法 点击 OK 即可添加成功。