实验五:Spring入门程序
一、实验目的
1.熟悉Spring框架的下载及目录结构
2.掌握Spring框架入门程序的编写
二、实验内容
1.完成Spring的入门程序
2.实现一个简单的登录验证
三、实验步骤
1.完成Spring的入门程序
(1)在pom.xml引入spring的相关包
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sun</groupId>
<artifactId>sy_ch5</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
(2)创建HelloSpring类
在src\main\java文件夹中创建com\sun文件夹,在文件夹中创建HelloSpring.java
package com.sun;
public class HelloSpring {
public void setUserName(String userName) {
this.userName = userName;
}
private String userName;
public void show(){
System.out.println(userName+"欢迎来到Spring");
}
}
(3)创建HelloSpring类的配置文件
在src\main\resources文件夹中创建applicationContext.xml作为HelloSpring类的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloSpring" class="com.sun.HelloSpring">
<property name="userName" value="孙昊楠"></property>
</bean>
</beans>
(4)创建测试类
在src\main\java\com\sun文件夹中创建TestHelloSpring.java
package com.sun;
导入相关包...
public class TestHelloSpring {
public static void main(String[] args){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
HelloSpring helloSpring=
(HelloSpring)applicationContext.getBean("helloSpring");
helloSpring.show();
}
}
运行结果
2.实现一个简单的登录验证
(1)编写Dao层:
在src\main\java\com\sun中创建dao文件夹并创建UserDao.java
public interface UserDao {
public boolean login(String name,String password);
}
(2)创建接口的实现类
在src\main\java\com\sun\dao中创建impl文件夹并创建UserDaoImpl.java
public class UserDapImpl implements UserDao {
public boolean login(String name, String password) {
if (name.equals("孙昊楠")&&password.equals("1234")){
return true;
}
return false;
}
}
(3)编写Service层:
在src\main\java\com\sun中创建service文件夹并创建UserService.java
public interface UserService {
public boolean login(String name,String password);
}
(4)创建UserService接口
在src\main\java\com\sun\service中创建impl文件夹并创建UserServiceImpl.java
public class UserServiceImpl implements UserService {
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao=userDao;
}
@Override
public boolean login(String name,String password){
return userDao.login(name, password);
}
}
(5)修改配置文件
在配置文件applicationContext.xml中添加<bean>元素
<bean id="userDao" class="com.sun.dao.impl.UserDapImpl"></bean>
<bean id="userService" class="com.sun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
(6)编写测试类:
在src\main\java\com\sun文件夹中创建TestSpring.java
package com.sun;
导入相关包...
public class TestSpring {
public static void main(String[] args){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)
applicationContext.getBean("userService");
boolean flag =userService.login("孙昊楠","1234");
if (flag){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
}
运行结果