Spring 框架介绍和使用

Spring框架主要是运用于一个Web项目里的service层。

先贴出一个项目的结构。这里项目运用的技术框架有Struts2、Mybatis、Spring.这里只讲在Struts2、Mybatis基础上Spring的运用。

新建一个maven工程项目,

第一步:导包

在porm.xml里面配置下面的代码,就会自动下载jar包。

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>5.0.5.RELEASE</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-expression</artifactId>
		<version>5.0.5.RELEASE</version>
	</dependency>


	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>5.0.5.RELEASE</version>
	</dependency>


	<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>1.8.13</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>5.0.5.RELEASE</version>
	</dependency>

第二步:配置applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
		
<bean name="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:mgr.properties"></property>
<property name="fileEncoding" value="UTF-8" />
</bean>	
		
	<context:component-scan base-package="org.zhuao.service.impl"></context:component-scan>	
			
</beans>

这里要根据自己的包名来写,就是事务的实现类包。

这里还要写一个mgr.properties。

第三步:写一个工具类SpringUtils.java

public class SpringUtils {	
	public static  Object getBean(String beanName) {		
		String s="applicationContext.xml";
		ApplicationContext conf=new ClassPathXmlApplicationContext(s);
		Object bean =conf.getBean(beanName);
		return bean;		
	}
}

第四步:写service层接口类,这里举个例子:

 

public interface LoginService {
	User login(String name,String password);
}

第五步:写service.impl事务实现类

@Service("loginServiceImpl")
public class LoginServiceImpl implements LoginService {
     
	@Value("${wzj}")
	private  String names;

	@Override
	public User login(String name, String password) {
		System.out.println("name是: "+names);	
		SqlSession session =MyBatisUtils.getSession();
		UserMapper mapper=session.getMapper(UserMapper.class);	
		User u=mapper.login(name, password);
	    System.out.println(u.toString());
	    session.close();
		return u;
	}
	public String getNames() {
		return names;
	}
	public void setNames(String names) {
		this.names = names;
	}
}

这里要注意几点,一个是

Service标签里面的值是这个LoginServcieImpl的标识,Action类中要靠这个标识来获取这个LoginServiceImpl事务实现类。

一个是

这里对应的是mgr.properties里的值。

mgr.properties的代码如下:

wzj=kfc

 

也就是说names的值是kfc.

第六步:Action类调用service层事务实现类

这里以一个登陆的请求业务为例:

private String username;
	private  String password;
	private  String code;
	private JSONObject jsonPerson;
	
	public String doLogin() {		
		HttpServletResponse response=ServletActionContext.getResponse();
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session =request.getSession();	
		//先判断是否验证码正确
		String oldKeyCode=(String) session.getAttribute("keycode");		
		if(!"".equals(username) && !"".equals(password) && oldKeyCode.equals(code.toUpperCase())) {
			
			LoginService ls=(LoginService) SpringUtils.getBean("loginServiceImpl");
			User u =ls.login(username, password);		
			if(u!=null) {
				session.setAttribute("loginUser",u);	
				return "suc";
			}else {
				return "Err";
			}		
	}else {
		return "Err";
	}
	}

主要看的是

这句话,按照这个模式,想要调用其他service的话,也是如此调用。

到這裡的话,其实Spring框架的部分已经完结了。

其他的部分例如jsp界面,dao层、model层的代码就不一一贴出了。

上传一个总的框架集合,仅供参考的小demo工程。https://download.csdn.net/download/weixin_42120561/11456840

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值