SSH搭建spring,使用依赖注入的方法

配置文件

 <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.lgp</groupId>
<artifactId>maven_struts_spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven_ssh1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <!-- 添加struts2依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.31</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.31</version>
</dependency>
<!-- 添加Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>maven_ssh1</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.xml

网上有很多用junit教你搭的,虽然都能跑,但是如果要实际运行和前台交互,那样是不行的,还要导入我所导入的包。

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee">
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>

web.xml

则多了sprig的配置,留意<context-param>,自带的web.xml会报错说版本不对,只要把开头的那一坨玩意去掉即可。

其他就是在resource文件夹下的配置文件

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="userDao" class="com.dao.impl.UserDao"></bean>
<bean id="userService" class="com.service.impl.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>

applicationcontext.xml 使用的是依赖注入,这个方法在初学spring的时候,对spring有很好的理解,层层递进,关系明了,当然实际应用通常用注解注入,但是如果我那样写的话,就看不透它的运行轨迹了。

java代码

 package com.service.impl;

 import com.dao.IUserDao;
import com.service.IUserService; public class UserService implements IUserService {
private IUserDao userDao; public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
} public void add() {
System.out.println("UserService.add()");
userDao.add();
} }
 package com.service;

 public interface IUserService {
public void add();
}
 package com.dao.impl;

 import com.dao.IUserDao;

 public class UserDao implements IUserDao{

     public void add() {
System.out.println("UserDao.add()");
}
}
 package com.dao;

 public interface IUserDao {
public void add();
}
package com.action;

import com.service.IUserService;

public class UserAction {
private String name;
private String pwd;
private IUserService userService; public String add() {
System.out.println("name--" + this.getName());
System.out.println("pwd---" + this.getPwd());
userService.add();
return "success";
} public void setUserService(IUserService userService) {
this.userService = userService;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
}
}

留意是用接口来实现方法的。

上一篇:队列queue(2):链表实现队列


下一篇:iOS--NSTimer设置定时器的两种方法