在上一篇Spring框架提升(一)常用注解中,我们主要介绍了以下几种常用的注解:
@Configuration | 表示该类是一个配置类 |
@Bean | 告诉Spring这个方法将会返回一个对象并注入到容器中 |
@Component | 告诉Spring这个类会作为组件类并注入到容器中 |
@Controller | 在控制层使用,表示将类注入到容器中 |
@Service | 在业务层使用,表示将类注入到容器中 |
@Repository | 在持久层使用,表示将类注入到容器中 |
@ComponentScan | 指定扫描路径,并将路径下标明@Controller等注解的类注入到容器中 |
@Scope | 指定对象的作用域 |
@Lazy | 延迟加载,当对象作用域为单例时有效,在使用时才会创建对象 |
上篇介绍了好几种注入bean的注解,此外还有条件注入@Conditional。当需要满足某些条件才将该类注入到容器中时可以使用该注解。使用步骤:
1) 自定义条件类,实现Condition接口,并重写该接口的matcher方法。在matcher方法中判断条件,返回Boolean值
2)在需要根据条件注入的类上使用@Conditional(自定义条件类.class)即可
Spring,负责对象对象创建
Struts, 用Action处理请求
Spring与Struts框架整合,
关键点:让struts框架action对象的创建,交给spring完成!
整合步骤:
1.导包(这里有下载好的:https://download.csdn.net/download/quge_name_harder/10847255)
1)引入struts .jar相关文件
2)spring-core 相关jar文件
3)spring-web 支持jar包
spring-web-3.2.5.RELEASE.jar 【Spring源码】
struts2-spring-plugin-2.3.4.1.jar 【Struts源码】
2.配置三个XML
web.xml 核心的XML文件
主要功能:一、引入struts功能 二、初始化spring的ioc容器
struts.xml
主要功能:struts路径与action映射配置
bean.xml (Application.xml)
主要功能: spring ioc容器配置,即创建对象,以及对象依赖关系
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 1. struts配置 -->
<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>
<!-- 2. spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user" extends="struts-default">
<!--
<action name="user" class="包.类" method="execute">
整合之后,不用写包.类了,直接到IOC容器中找该对象
-->
<action name="user" class="userAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
配置bean.xml (解决对象的创建以及对象之间的依赖关系)
最好将dao,service,action的bean分开配置,下面是bean-dao.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="userDao" class="cn.itcast.dao.UserDao" scope="singleton" lazy-init="false"></bean>
</beans>
bean-service.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="userService" class="cn.itcast.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
bean-action.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 指定action多例 -->
<bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>
整个项目链接:https://download.csdn.net/download/quge_name_harder/10847282