1.关于Spring Framework 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:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.littlepage"></context:component-scan> </beans>
context:component-scan 表示扫描package,扫描com.littlepage包下的package
2.关于目录结构
3.测试层开始,测试层模拟入口,也就是类似的jsp提交
package com.littlepage.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.littlepage.controller.LoginController; public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext cpx=new ClassPathXmlApplicationContext("applicationContext.xml"); LoginController loginController = cpx.getBean("loginController",LoginController.class); boolean isLogin=loginController.testLogin("root", "root"); if(isLogin) { System.out.println("Login Success"); }else { System.out.println("Login Failed"); } cpx.close(); } }
4.与jsp相连的层是controller层,从controller层开始接入service层,这边是controller层的内容
package com.littlepage.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.littlepage.service.LoginService; @Controller public class LoginController { @Autowired private LoginService loginService; public boolean testLogin(String name,String password) { return loginService.testLogin(name,password); } }
5.关于service层的验证
package com.littlepage.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.littlepage.dao.GetNameAndPassword; import com.littlepage.pojo.User; @Service public class LoginService { @Autowired GetNameAndPassword getNameAndPassword; public boolean testLogin(String name, String password) { User user=getNameAndPassword.getUser(); if(user.getName().equals(name)&&user.getPassword().equals(password)) return true; return false; } }
6.service层与dao层连,dao有一个orm映射,与pojo层相连
package com.littlepage.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.littlepage.pojo.User; @Repository public class GetNameAndPassword { @Autowired private User user; public User getUser() { return user; } }
7.pojo层注意,是一个prototype的scope,属性用@Value进行赋值
package com.littlepage.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") public class User { @Value("root") private String name; @Value("root") private String password; public User() { } public User(String name, String password) { super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
8.运行,测试结果无误