参考Java Web学习系列——创建基于Maven的Web项目一文,创建一个名为LockMIS的Maven Web项目。

添加依赖Jar包

推荐在http://mvnrepository.com/http://search.maven.org/等网站上获取Jar包资源,当然也可以从对应Jar包的官网上获取。

修改后的pom.xml文件如下所示:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lock</groupId>
<artifactId>LockMIS</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>LockMIS Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org.springframework.version>4.0.7.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
</dependencies>
<build>
<finalName>LockMIS</finalName>
</build>
</project>

修改完pom.xml文件之后,需要更新该项目。

执行更新操作,会从Maven仓库下载所依赖的jar包。

使用XML配置文件实现Spring的IOC

以系统构建子系统为例,实现系统配置项各子项目的数据操作。

创建如下主代码结构:

新建系统配置项ConfigItem对象bean

package com.lock.base.bean;

/**
* (T_G_CONFIGITEM)
*
* @author hs
* @version 1.0.0 2016-08-09
*/
public class ConfigItem implements java.io.Serializable {
/** 版本号 */
private static final long serialVersionUID = -857268865287138078L; /** */
private Integer configitemid; /** */
private String idcode; /** */
private String label; /** */
private String itemvalue; /** */
private String note; /** */
private Integer datatypeid; /**
* 获取
*
* @return
*/
public Integer getConfigitemid() {
return this.configitemid;
} /**
* 设置
*
* @param configitemid
*
*/
public void setConfigitemid(Integer configitemid) {
this.configitemid = configitemid;
} /**
* 获取
*
* @return
*/
public String getIdcode() {
return this.idcode;
} /**
* 设置
*
* @param idcode
*
*/
public void setIdcode(String idcode) {
this.idcode = idcode;
} /**
* 获取
*
* @return
*/
public String getLabel() {
return this.label;
} /**
* 设置
*
* @param label
*
*/
public void setLabel(String label) {
this.label = label;
} /**
* 获取
*
* @return
*/
public String getItemvalue() {
return this.itemvalue;
} /**
* 设置
*
* @param itemvalue
*
*/
public void setItemvalue(String itemvalue) {
this.itemvalue = itemvalue;
} /**
* 获取
*
* @return
*/
public String getNote() {
return this.note;
} /**
* 设置
*
* @param note
*
*/
public void setNote(String note) {
this.note = note;
} /**
* 获取
*
* @return
*/
public Integer getDatatypeid() {
return this.datatypeid;
} /**
* 设置
*
* @param datatypeid
*
*/
public void setDatatypeid(Integer datatypeid) {
this.datatypeid = datatypeid;
}
}

操作ConfigItem的接口IConfigItemDao

package com.lock.base.daointerface;

import com.lock.base.bean.ConfigItem;

public interface IConfigItemDao {
public Boolean updateConfigItem(ConfigItem configItem);
}

实现IConfigItemDao接口的ConfigItemDaoImpl类

package com.lock.base.daoimpl;

import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; public class ConfigItemDaoImpl implements IConfigItemDao { @Override
public Boolean updateConfigItem(ConfigItem configItem) {
// TODO Auto-generated method stub
return null;
} }

Spring容器的配置文件ConfigItemMapper.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="configItemDao" class="com.lock.base.daoimpl.ConfigItemDaoImpl"></bean>
</beans>

配置项业务类ConfigItemService

package com.lock.base.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; public class ConfigItemService { IConfigItemDao configItemDao; public ConfigItemService() {
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
//从容器中获得id为configItemDao的bean
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao");
}
public Boolean updateConfigItem(ConfigItem configItem){
System.out.println("更新ConfigItem");
Boolean result = configItemDao.updateConfigItem(configItem);
System.out.println(result);
return result;
}
}

上述代码添加完成后,执行Maven编译命令;检查编译结果,如果错误,则对应处理直至编译成功。

在测试代码目录下新建BaseTest测试类,并在测试类中调用ConfigItemService的方法进行编码测试。

package com.lock;

import org.junit.Test;

import com.lock.base.bean.ConfigItem;
import com.lock.base.service.ConfigItemService; public class BaseTest {
@Test
public void test(){
System.out.println ("test");
ConfigItem configItem = new ConfigItem();
ConfigItemService configItemService = new ConfigItemService();
configItemService.updateConfigItem(configItem);
}
}

执行Maven编译命令,编译成功后,运行或者调试该测试代码

正确情况下,输出以下结果

使用Spring注解实现其IOC

Spring的注解是个很神奇的东西,对于各种云里雾里的注解,后续会详细剖析,这里只是拿来主义,讲解一下如何配置使用。

修改ConfigItemDaoImpl,为其添加@Component注解。Spring容器能够识别这种特定的注解,并且会自动把它转成容器管理的Bean。

package com.lock.base.daoimpl;

import org.springframework.stereotype.Component;

import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; @Component("configItemDao")
public class ConfigItemDaoImpl implements IConfigItemDao { @Override
public Boolean updateConfigItem(ConfigItem configItem) {
// TODO Auto-generated method stub
return null;
} }

修改ConfigItemService,同样添加@Component注解

package com.lock.base.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; @Component
public class ConfigItemService { IConfigItemDao configItemDao;
/*
public ConfigItemService() {
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
//从容器中获得id为configItemDao的bean
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao");
}*/ public Boolean updateConfigItem(ConfigItem configItem){
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao"); System.out.println("更新ConfigItem");
Boolean result = configItemDao.updateConfigItem(configItem);
System.out.println(result);
return result;
}
}

修改ConfigItemMapper.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.lock.base.*"></context:component-scan>
</beans>

修改BaseTest测试类,修改原方法或者新添加一个测试方法

package com.lock;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lock.base.bean.ConfigItem;
import com.lock.base.service.ConfigItemService; public class BaseTest {
/*@Test
public void test(){
System.out.println ("test");
ConfigItem configItem = new ConfigItem();
ConfigItemService configItemService = new ConfigItemService();
configItemService.updateConfigItem(configItem);
}*/ @Test
public void test2(){
System.out.println ("test");
ApplicationContext ctx = new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
ConfigItemService configItemService = ctx.getBean(ConfigItemService.class);
ConfigItem configItem = new ConfigItem();
configItemService.updateConfigItem(configItem);
}
}

执行Maven编译命令,编译成功后,运行或者调试该测试代码

可以看到,输出结果与上一种方式相同。

基于Spring的自动装配实现其IOC

在第一种方法中,有一处使用ApplicationContext初始化容器后获得需要的Bean;而在第二种方法中,有两处应用。实际上在Spring中,还可以通过自动装配来简化这一过程。

修改ConfigItemDaoImpl类的注解,用@Repository替换原来的注解

package com.lock.base.daoimpl;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository; import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; @Repository
public class ConfigItemDaoImpl implements IConfigItemDao { @Override
public Boolean updateConfigItem(ConfigItem configItem) {
// TODO Auto-generated method stub
return null;
} }

修改ConfigItemService类的注解为@Service,并且为configItemDao成员变量增加@Autowired注解。@Autowired默认是按照类型装配注入,Spring根据类型从容器中匹配一个Bean给configItemDao

package com.lock.base.service;

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; import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; @Service
public class ConfigItemService {
@Autowired
IConfigItemDao configItemDao;
/*
public ConfigItemService() {
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
//从容器中获得id为configItemDao的bean
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao");
}*/ public Boolean updateConfigItem(ConfigItem configItem){
/*
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao");
*/
System.out.println("更新ConfigItem");
Boolean result = configItemDao.updateConfigItem(configItem);
System.out.println(result);
return result;
}
}

ConfigItemMapper.xm配置文件和测试代码均维持不变,执行Maven编译,然后进行测试

无配置实现Spring的IOC

在上述三种方式中,Spring初始化容器时都必须使用xml配置文件。实际上,Spring还有一种零配置的方式来实现其IOC,也就是不需要配置文件。

修改ConfigItemService类中configItemDao成员变量的注解为@Resource,Resource注解默认是按照名称来装配注入

package com.lock.base.service;

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; import com.lock.base.bean.ConfigItem;
import com.lock.base.daointerface.IConfigItemDao; @Service
public class ConfigItemService {
@Resource
IConfigItemDao configItemDao;
/*
public ConfigItemService() {
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
//从容器中获得id为configItemDao的bean
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao");
}*/ public Boolean updateConfigItem(ConfigItem configItem){
/*
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
configItemDao=(IConfigItemDao)ctx.getBean("configItemDao");
*/
System.out.println("更新ConfigItem");
Boolean result = configItemDao.updateConfigItem(configItem);
System.out.println(result);
return result;
}
}

由于取消xml文件配置,因此需要新增一个类来实现原xml文件所起的作用。在java主目录下新建Package:com.lock.base.config,并新建AppConfig类

package com.lock.base.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import com.lock.base.bean.ConfigItem; @Configuration
@ComponentScan(basePackages="com.lock.base.*")
public class AppConfig {
@Bean
public ConfigItem getConfigItem(){
return new ConfigItem();
}
}

修改ConfigItem类,为该类添加注解@Component("configItem")

package com.lock.base.bean;

import org.springframework.stereotype.Component;

/**
* (T_G_CONFIGITEM)
*
* @author hs
* @version 1.0.0 2016-08-09
*/
@Component("configItem")
public class ConfigItem implements java.io.Serializable {
/** 版本号 */
private static final long serialVersionUID = -857268865287138078L; /** */
private Integer configitemid; /** */
private String idcode; /** */
private String label; /** */
private String itemvalue; /** */
private String note; /** */
private Integer datatypeid; /**
* 获取
*
* @return
*/
public Integer getConfigitemid() {
return this.configitemid;
} /**
* 设置
*
* @param configitemid
*
*/
public void setConfigitemid(Integer configitemid) {
this.configitemid = configitemid;
} /**
* 获取
*
* @return
*/
public String getIdcode() {
return this.idcode;
} /**
* 设置
*
* @param idcode
*
*/
public void setIdcode(String idcode) {
this.idcode = idcode;
} /**
* 获取
*
* @return
*/
public String getLabel() {
return this.label;
} /**
* 设置
*
* @param label
*
*/
public void setLabel(String label) {
this.label = label;
} /**
* 获取
*
* @return
*/
public String getItemvalue() {
return this.itemvalue;
} /**
* 设置
*
* @param itemvalue
*
*/
public void setItemvalue(String itemvalue) {
this.itemvalue = itemvalue;
} /**
* 获取
*
* @return
*/
public String getNote() {
return this.note;
} /**
* 设置
*
* @param note
*
*/
public void setNote(String note) {
this.note = note;
} /**
* 获取
*
* @return
*/
public Integer getDatatypeid() {
return this.datatypeid;
} /**
* 设置
*
* @param datatypeid
*
*/
public void setDatatypeid(Integer datatypeid) {
this.datatypeid = datatypeid;
}
}

修改BaseTest测试类,修改原方法或者新添加一个测试方法

package com.lock;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lock.base.bean.ConfigItem;
import com.lock.base.config.AppConfig;
import com.lock.base.service.ConfigItemService; public class BaseTest {
/*@Test
public void test(){
System.out.println ("test");
ConfigItem configItem = new ConfigItem();
ConfigItemService configItemService = new ConfigItemService();
configItemService.updateConfigItem(configItem);
} @Test
public void test2(){
System.out.println ("test");
ApplicationContext ctx = new ClassPathXmlApplicationContext("com/lock/base/daomapper/ConfigItemMapper.xml");
ConfigItemService configItemService = ctx.getBean(ConfigItemService.class);
ConfigItem configItem = new ConfigItem();
configItemService.updateConfigItem(configItem);
}*/ @Test
public void test3(){
System.out.println ("test");
ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);
ConfigItemService configItemService = ctx.getBean(ConfigItemService.class);
ConfigItem configItem1 = new ConfigItem();
configItemService.updateConfigItem(configItem1); ConfigItem configItem2 = ctx.getBean("configItem",ConfigItem.class);
configItemService.updateConfigItem(configItem2); ConfigItem configItem3 = ctx.getBean("getConfigItem",ConfigItem.class);
configItemService.updateConfigItem(configItem3);
}
}

在该测试方法中,Spring通过反射AppConfig.class来初始化容器。

执行Maven编译,然后进行测试

项目结构调整

为了使项目文件结构更加清晰明了,对文件结构做如下调整

调整代码结构后,需要修改代码中引用的包路径。编译后测试无误。

小结

本文在上一篇Java Web学习系列——创建基于Maven的Web项目基础之上,介绍了Maven Web项目添加Spring及其相关依赖JAR包的方法。并以完整实例的形式分别讲解了四种不同的实现Spring IOC的方式。通过比较来看,无配置实现Spring IOC的方式更显简洁易用,但这本身还是建立在Spring注解的基础之上。后续也会更详细的说明Spring各项注解的详细应用。

Java Web学习系列——Maven Web项目中集成使用Spring的更多相关文章

  1. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  2. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  3. nginx高性能WEB服务器系列之五--实战项目线上nginx多站点配置

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  4. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

  5. 使用Java web工程建立Maven Web Module工程

    1. 前言 之前有一篇关于搭建S2SH的文章中提到建立Maven Web Module工程,有人反馈说这个方面不会.那还是唠叨一下,写篇文章说明一下吧. 建立Maven Web Module的方式有多 ...

  6. ABP架构学习系列一 整体项目结构及目录

    本系列是基于aspnetboilerplate-0.8.4.0版本写的,其中原因是由于较高的版本太抽象难以理解和分析,对于还菜菜的我要花更多的时间去学习. abp的源码分析学习主要来源于 HK Zha ...

  7. Java基础扫盲系列(-)—— String中的format

    Java基础扫盲系列(-)-- String中的format 以前大学学习C语言时,有函数printf,能够按照格式打印输出的内容.但是工作后使用Java,也没有遇到过格式打印的需求,今天遇到项目代码 ...

  8. Java命令学习系列(7):Javap(转)

    原文出处: Hollis(@Hollis_Chuang) javap是jdk自带的一个工具,可以对代码反编译,也可以查看java编译器生成的字节码. 一般情况下,很少有人使用javap对class文件 ...

  9. 转:深入Java集合学习系列:HashSet的实现原理

    0.参考文献 深入Java集合学习系列:HashSet的实现原理 1.HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特 ...

随机推荐

  1. How to install ZeroMQ on Ubuntu14.04

    Prepare: sudo apt-get install libtool autoconf automake uuid-dev sudo apt-get install python-dev sud ...

  2. duilib进阶教程 -- Container控件的bug (14)

    在<duilib进阶教程 -- TreeView控件的bug (9)>里,Alberl发现了两个bug,并解决了其中一个,现在教程已经接近尾声啦,所以Alberl就解决了另外一个bug. ...

  3. D. Book of Evil

    D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Java插件开发-取插件下的某个文件

    //找到插件所在处 Bundle bundle = Activator.getDefault().getBundle(); //根据插件转义成URL路径 URL url = FileLocator.t ...

  5. shell日期的应用

    #!bin/bash del_table() { #月初的第一天 month_first_day=`date +%Y%m01` #要删除的日期 last_7day_ago=`date -d " ...

  6. Apache Solr查询语法(转)

    查询参数 常用: q - 查询字符串,必须的. fl - 指定返回那些字段内容,用逗号或空格分隔多个. start - 返回第一条记录在完整找到结果中的偏移位置,0开始,一般分页用. rows - 指 ...

  7. SSH和SFTP简介

    传统FTP 在传输机制和实现原理上是没有考虑安全机制的,因为它们在网络上用明文传送数据.用户帐号和用户口令,别有用心的人非常容易地就可以截获这些数据.用户帐 号和用户口令.而且,这些网络服务程序容易受 ...

  8. SSD在SQLServer中的应用

        一. 首先,回顾一下 SSD 的读写特性 (1)有限次数写:        (2)随机读性能最好:        (3)顺序读性能好:        (4)顺序写性能差:        (5) ...

  9. 用DOS批处理实现FTP自动上传、下载、清理文件

    用DOS批处理实现FTP自动上传.下载.清理文件 最近好像特别的忙,好久没来写点东西了,今天写了一个利用批处理程序完成FTP自动上传.下载.清理文件的程序.赶紧 记录下来,以备日后之用.功能介绍:自动 ...

  10. ZooKeeper快速搭建

    原文地址:http://nileader.blog.51cto.com/1381108/795230 下载PDF版本 本文是ZooKeeper的快速搭建,旨在帮助大家以最快的速度完成一个ZK集群的搭建 ...