搭建Spring实例
持久层
import org.springframework.stereotype.Repository;
@Repository //将该类的对象创建为userDao实例
public class UserDao{
public String findUserByName(String userName) {
return "abc";
}
}
业务层
import com.baobaotao.dao.UserDao;
import com.sun.istack.internal.FinalArrayList;
@Service //通常作用在业务层,主要将业务层的类自动注册到spring容器
public class UserService{
@Autowired //自动将bean注册到该对象
private UserDao userDao;
private TransactionTemplate transactionTemplate;
public String login(final String name,final String password) {
return transactionTemplate.execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
Object result;
try {
result = userDao.findUserByName(name);
} catch (Exception e) {
status.setRollbackOnly();
result = false;
System.out.println("Transfer Error!");
}
return result;
}
});
}
}
控制层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import com.baobaotao.domain.User;
import com.baobaotao.service.UserService;
@Controller //将该类注册为一个控制层的Bean实例
public class UserController{
@Autowired //自动注入spring容器的bean实例
private UserService userService;
@RequestMapping(value="login") //
public String login(HttpServletRequest request,User user){
System.out.println("=========aaaaaaaa====");
String result = userService.login(user.getUserName(), user.getPassword());
return "success";
}
Spring容器配置文件
<?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" xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 扫描类包,将通过注解的类转换为Bean实例 --> <context:component-scan base-package="com.baobaotao.dao"></context:component-scan> <context:component-scan base-package="com.baobaotao.service"></context:component-scan> <context:component-scan base-package="com.baobaotao.controller"></context:component-scan> <context:component-scan base-package="testpackage"></context:component-scan> </beans>
SpringMVC配置文件
<?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">
<mvc:annotation-driven/>
<context:component-scan base-package="com.baobaotao.controller"/>
<!-- 配置视图解析器,将ModelAndView及字符串解析为具体页面 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>baobaotao</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 从类路径下面加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 加载配置文件的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringMVC的配置文件加载,这里定义了一个servlet,servlet名自己取后面要用到,class固定的,不可改变。
然后就是初始化init后要加载的xml文件,默认名为servlet名-servlet.xml,这个是要加载到webroot-webinf-classes下的 -->
<servlet>
<servlet-name>baobaotao</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:baobaotao-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>baobaotao</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注解详解
@Repository,标识Dao层的Bean实例。可以设置为@Repository(“XXX”)。
@Service(“XXX”),就相当于将业务层的类定义为一个 bean,其中,XXX 即为 bean 的名称。如果不设置bean的名字,会默认为类名作为实例名。
@Controller,用于标注控制层组件。可以设置为@Controller("XXX")。
@Component,泛指任何一层的bean实例,可以设置为@Component("XXX)。