政务公开网站建设管理,刚做的网站 为啥搜不到,如何建设好一个公司网站,deals网站建设前言
虽然之前用过Spring#xff0c;但是今天试着去搭建依然遇到了困难#xff0c;而且上网找教程#xff0c;很多写的是在web里使用Spring MVC的示例#xff0c;官方文档里的getting start一开始就讲原理去了#xff08;可能打开的方法不对#xff09;。没办法#xf…前言
虽然之前用过Spring但是今天试着去搭建依然遇到了困难而且上网找教程很多写的是在web里使用Spring MVC的示例官方文档里的getting start一开始就讲原理去了可能打开的方法不对。没办法好不容易实验成功了记下来免得自己以后麻烦。
添加依赖包
进入spring官网切换到projects下点击 spring framework.官网上写的是以maven依赖的形式写的所以可以新建一个maven项目然后将下面的依赖加入到pom.xml里
dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion4.2.0.RELEASE/version/dependency
/dependencies
或者也可以点击这个页面右上角的fork me on github,在github里下载依赖包然后加入到项目的build path中去。
注意 spring-context只是包含了spring最核心的功能如依赖注入切面等。
创建spring配置文件
新建一个名为bean.xml的配置文件放到代码目录里文件的内容如下
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/p xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdcontext:component-scan base-packagecom.lcl/context:component-scan
/beans
这个配置文件有几个地方需要说明一下
1、命名空间
beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/p xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
这个是xml的语法配置当前xml文件中的标签格式这里配置了context和p两个命名空间。例如配了context空间就可以使用/context:XXX这样的标签。 2、自动扫描组件
context:component-scan base-packagecom.lcl/context:component-scan
这个配置可以让spring框架自动扫描代码中用component注解了的类自动创建这些类的对象。
最后注意一下bean.xml要放在代码目录下其目的是为了将bean.xml添加到classpath中。
编写代码
首先写一个Person类作为bean类。所谓bean类简单来说就是所有成员变量都有getter和setter方法并且有无参构造方法的类。然后用了Component(“person”)注解将Person类标注为一个组件这样就可以使用Resource将Person的一个实例注入给其他对象的成员里或者使用Application类的getBean(Class)方法得到一个实例。
package com.lcl;import org.springframework.stereotype.Service;Component(person)
public class Person {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public void info(){System.out.println(一起来吃麻辣烫);System.out.println(name:getName() age:getAge());}
}
然后是A类A类有person成员变量引用了Person的实例我们是用spring的依赖注入来管理成员变量person的创建为了达到这个目的只需要将person变量用Resource注解标注即可。
package com.lcl;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;/*** author luchunlong** 2015年8月27日 上午10:20:41*/
Component
public class A {Resourceprivate Person person;public void info(){person.setName(abc);person.setAge(123);person.info();}public static void main(String[] args){ApplicationContext ctxnew ClassPathXmlApplicationContext(bean.xml);A actx.getBean(A.class);a.info();}}
总结
创建一个spring项目只要三步 - 加入依赖 - 编写bean类 - 编写bean.xml
其中编写bean类时用到了Component、Resource这两个注解