标签:spring
Struts,是MVC框架中控制层的解决方案,可以进行请求数据自动封装、类型转换、文件上传、效验…
Hibernate, 是持久层的解决方案;可以做到“把对象保存到数据库”和“从数据库中取出的是对象”。
基于mvc的项目框架结构:entity / dao / service / action
// 1. 实体类 class User{ } //2. dao class UserDao{ .. 访问db } //3. service class UserService{ UserDao userDao = new UserDao(); } //4. action class UserAction{ UserService userService = new UserService(); .. 拿到数据或结果 }
用户访问:/user.action ----> Tomcat (服务器创建Action、Service、dao)
1. 对象创建创建能否写死?(HardCode)
2. 对象创建细节
对象数量
-->action 多个 【维护成员变量】
-->service 一个 【不需要维护公共变量】
-->dao 一个 【不需要维护公共变量】
创建时间
-->action 访问时候创建
-->service 启动时候创建
-->dao 启动时候创建
3. 对象的依赖关系
-->action 依赖 service
-->service依赖 dao
总结:spring就是解决上面的问题的,简单来说,就是处理对象的创建的、以及对象的依赖关系!
Spring框架,可以解决“对象创建”以及“对象之间依赖关系”的一种框架,且可以和其他框架一起使用(Spring与Struts, Spring与hibernate),起到整合(粘合)作用的一个框架。
组件/框架设计
侵入式设计,引入了框架,对现有的类的结构有影响;即需要实现或继承某些特定类。例如: Struts框架。
非侵入式设计,引入了框架,对现有的类结构没有影响。例如:Hibernate框架 / Spring框架。
控制反转
Inversion on Control , 控制反转 IOC。对象的创建交给外部容器完成,这个叫做“控制反转”.
依赖注入
dependency injection,处理对象的依赖关系
控制反转和依赖注入的区别
控制反转, 解决对象创建的问题 【对象创建交给别人】
依赖注入,在创建完对象后, 对象关系的处理就是依赖注入 【通过set方法依赖注入】
AOP
面向切面编程。切面,简单来说来可以理解为一个类,由很多重复代码形成的类。
切面举例:事务、日志、权限;
Spring提供了一站式解决方案:
1) Spring Core spring的核心功能: IOC容器, 解决对象创建及依赖关系
2) Spring Web Spring对web模块的支持。
-->可以与struts整合,让struts的action创建交给spring
-->spring mvc模式
3) Spring DAO Spring 对jdbc操作的支持 【JdbcTemplate模板工具类】
4) Spring ORM spring对orm的支持
-->既可以与hibernate整合,
-->也可以使用spring的对hibernate操作的封装
5)Spring AOP 切面编程
6)SpringEE spring 对javaEE其他模块的支持
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <bean id="user" class="com.rk.spring.a_helloworld.User" scope="prototype" lazy-init="false" init-method="init_user" destroy-method="destroy_user"></bean> </beans>
User.java
package com.rk.spring.a_helloworld; public class User { private int id; private String name; public User() { System.out.println("---创建User对象---"); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void init_user() { System.out.println("初始化用户"); } public void destroy_user() { System.out.println("销毁用户"); } }
App_Create.java
package com.rk.spring.a_helloworld; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App_Create { /** * 1) 对象创建: 单例/多例 * scope="singleton", 默认值, 即 默认是单例 【service/dao/工具类】 * scope="prototype", 多例; 【Action对象】 * * 2) 什么时候创建? * scope="singleton" 在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个。 * scope="prototype" 在用到对象的时候,才创建对象。 * * 3)是否延迟创建(只对单例有效) * lazy-init="false" 默认为false, 不延迟创建,即在启动时候就创建对象 * lazy-init="true" 延迟初始化, 在用到对象的时候才创建对象 * * 4) 创建对象之后,初始化/销毁(经我测试,只对单例有效) * init-method="init_user" 【对应对象的init_user方法,在对象创建之后执行 】 * destroy-method="destroy_user" 【在调用容器对象的destroy方法时候执行,(容器用实现类)】 */ @Test public void testIOC() { System.out.println("-----1-----"); // 得到IOC容器对象 【用实现类,因为要调用销毁的方法】 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/rk/spring/a_helloworld/applicationContext.xml"); System.out.println("-----2-----"); // 从容器中获取bean User user1 = (User) ac.getBean("user"); User user2 = (User) ac.getBean("user"); System.out.println("-----3-----"); System.out.println("user1 == user2 : " + (user1 == user2)); System.out.println(user1); System.out.println(user2); // 销毁容器对象 ac.destroy(); } }
SpringIOC容器,是spring核心内容。
作用: 创建对象 & 处理对象的依赖关系
IOC容器创建对象,有几种方式:
1) 调用无参数构造器
2) 带参数构造器
3) 工厂创建对象
工厂类,静态方法创建对象
工厂类,非静态方法创建对象
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- IOC容器的配置: 要创建的所有的对象都配置在这里--> <!-- ###############对象创建############### --> <!-- 1. 默认无参数构造器 --> <bean id="user1" class="com.rk.spring.b_ioc_container.User"></bean> <!-- 2. 带参数构造器 --> <bean id="user2" class="com.rk.spring.b_ioc_container.User"> <constructor-arg index="0" type="int" value="101"></constructor-arg> <constructor-arg index="1" type="java.lang.String" value="Tom"></constructor-arg> </bean> <!-- 定义一个字符串,值是"Jack" ; String s = new String("jack")--> <bean id="str" class="java.lang.String"> <constructor-arg index="0" type="java.lang.String" value="jack"></constructor-arg> </bean> <bean id="user3" class="com.rk.spring.b_ioc_container.User"> <constructor-arg index="0" type="int" value="102"></constructor-arg> <constructor-arg index="1" type="java.lang.String" ref="str"></constructor-arg> </bean> <!-- 3. 工厂类创建对象 --> <!-- # 3.1 工厂类,实例方法 --> <!-- 先创建工厂 --> <bean id="factory" class="com.rk.spring.b_ioc_container.ObjectFactory"></bean> <!-- 在创建user对象,用factory方的实例方法 --> <bean id="user4" factory-bean="factory" factory-method="getInstance"></bean> <!-- # 3.2 工厂类: 静态方法 --> <!-- class 指定的就是工厂类型 factory-method 一定是工厂里面的“静态方法” --> <bean id="user" class="com.rk.spring.b_ioc_container.ObjectFactory" factory-method="getStaticInstance"></bean> <!-- 对象写法 --> <!-- 问题:spring配置文件中,bean节点的id与name属性的区别? id 不能有特殊符号, 且唯一,且不能以数字开始 name 可以有特殊符号 --> <bean id="test" name="1test" class="com.rk.spring.b_ioc_container.User"></bean> </beans>
User.java
package com.rk.spring.b_ioc_container; public class User { private int id; private String name; public User() { } public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
ObjectFactory.java
package com.rk.spring.b_ioc_container; import java.util.Random; //工厂,创建对象 public class ObjectFactory { private static User user = new User(200, "小明"); public User getInstance() { Random rand = new Random(); int nextInt = rand.nextInt(200); return new User(nextInt,"Lucy"+nextInt); } // 静态方法创建对象 public static User getStaticInstance() { return user; } }
App.java
package com.rk.spring.b_ioc_container; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { ApplicationContext ac = new ClassPathXmlApplicationContext("com/rk/spring/b_ioc_container/applicationContext.xml"); // 测试:对象创建 @Test public void testIOC() { User user = (User) ac.getBean("user"); System.out.println(user); } // 测试: 容器中对象的写法 @Test public void test() { User user = (User) ac.getBean("1test"); System.out.println(user); } }
Spring中,如何给对象的属性赋值? 【DI, 依赖注入】
1) 通过构造函数
2) 通过setter方法给属性注入值
3) p名称空间
4) 自动装配(了解)
5) 注解
1) 通过构造函数
<bean id="user1" class="com.rk.spring.d_di_property.User"> <constructor-arg index="0" type="int" value="101"></constructor-arg> <constructor-arg index="1" type="java.lang.String" value="Tomcat"></constructor-arg> </bean>
2) 通过setter方法给属性注入值(常用)
<bean id="user" class="com.rk.spring.d_di_property.User"> <property name="id" value="102"></property> <property name="name" value="JBoss"></property> </bean>
案例
<!-- dao instance --> <bean id="userDao" class="com.rk.spring.d_di_property.UserDao"></bean> <!-- service instance --> <bean id="userService" class="com.rk.spring.d_di_property.UserService"> <property name="userDao" ref="userDao"></property> </bean> <!-- action instance --> <bean id="userAction" class="com.rk.spring.d_di_property.UserAction"> <property name="userService" ref="userService"></property> </bean>
也可以使用内部bean的方式
<bean id="userAction" class="com.rk.spring.d_di_property.UserAction"> <property name="userService"> <bean class="com.rk.spring.d_di_property.UserService"> <property name="userDao"> <bean class="com.rk.spring.d_di_property.UserDao"></bean> </property> </bean> </property> </bean>
3) p名称空间(优化)
<!-- 给对象属性注入值: # p 名称空间给对象的属性注入值 (spring3.0以上版本才支持) --> <bean id="userDao" class="com.rk.spring.d_di_property.UserDao"></bean> <bean id="userService" class="com.rk.spring.d_di_property.UserService" p:userDao-ref="userDao"></bean> <bean id="userAction" class="com.rk.spring.d_di_property.UserAction" p:userService-ref="userService" ></bean> <!-- 传统的注入: <bean id="user" class="cn.itcast.c_property.User" > <property name="name" value="xxx"></property> </bean> --> <!-- p名称空间优化后 --> <bean id="user" class="com.rk.spring.d_di_property.User" p:id="103" p:name="Lily"></bean>
完整的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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- ###############对象属性赋值############### --> <!-- 1) 通过构造函数 --> <bean id="user1" class="com.rk.spring.d_di_property.User"> <constructor-arg index="0" type="int" value="101"></constructor-arg> <constructor-arg index="1" type="java.lang.String" value="Tomcat"></constructor-arg> </bean> <!-- 2) 通过setter方法给属性注入值 --> <!-- <bean id="user" class="com.rk.spring.d_di_property.User"> <property name="id" value="102"></property> <property name="name" value="JBoss"></property> </bean> --> <!-- 案例: action/service/dao --> <!-- dao instance --> <!-- <bean id="userDao" class="com.rk.spring.d_di_property.UserDao"></bean> --> <!-- service instance --> <!-- <bean id="userService" class="com.rk.spring.d_di_property.UserService"> <property name="userDao" ref="userDao"></property> </bean> --> <!-- action instance --> <!-- <bean id="userAction" class="com.rk.spring.d_di_property.UserAction"> <property name="userService" ref="userService"></property> </bean> --> <!-- ##############内部bean############## --> <!-- <bean id="userAction" class="com.rk.spring.d_di_property.UserAction"> <property name="userService"> <bean class="com.rk.spring.d_di_property.UserService"> <property name="userDao"> <bean class="com.rk.spring.d_di_property.UserDao"></bean> </property> </bean> </property> </bean> --> <!-- 给对象属性注入值: # p 名称空间给对象的属性注入值 (spring3.0以上版本才支持) --> <bean id="userDao" class="com.rk.spring.d_di_property.UserDao"></bean> <bean id="userService" class="com.rk.spring.d_di_property.UserService" p:userDao-ref="userDao"></bean> <bean id="userAction" class="com.rk.spring.d_di_property.UserAction" p:userService-ref="userService" ></bean> <!-- 传统的注入: <bean id="user" class="cn.itcast.c_property.User" > <property name="name" value="xxx"></property> </bean> --> <!-- p名称空间优化后 --> <bean id="user" class="com.rk.spring.d_di_property.User" p:id="103" p:name="Lily"></bean> </beans>
User.java
package com.rk.spring.d_di_property; public class User { private int id; private String name; public User() { } public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
UserDao.java
package com.rk.spring.d_di_property; public class UserDao { public void save(User user) { System.out.println("保存用户成功:" + user); } }
UserService.java
package com.rk.spring.d_di_property; public class UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(User user) { userDao.save(user); } }
UserAction.java
package com.rk.spring.d_di_property; public class UserAction { private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public void save(User user) { userService.save(user); } }
App.java
package com.rk.spring.d_di_property; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/spring/d_di_property/applicationContext.xml"); @Test public void testIOC() { User user = (User) ac.getBean("user"); System.out.println(user); } @Test public void test() { User user = (User) ac.getBean("user"); UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.save(user); } }
4) 自动装配
根据名称自动装配:autowire="byName"
自动去IOC容器中找与属性名同名的引用的对象,并自动注入
<bean id="userDao" class="com.rk.spring.e_autowire.UserDao" autowire="byName"></bean> <bean id="userService" class="com.rk.spring.e_autowire.UserService" autowire="byName"></bean> <bean id="userAction" class="com.rk.spring.e_autowire.UserAction" autowire="byName"></bean>
根据类型自动装配:autowire="byType"
必须确保改类型在IOC容器中只有一个对象;否则报错。
<bean id="userDao" class="com.rk.spring.e_autowire.UserDao" autowire="byType"></bean> <bean id="userService" class="com.rk.spring.e_autowire.UserService" autowire="byType"></bean> <bean id="userAction" class="com.rk.spring.e_autowire.UserAction" autowire="byType"></bean>
也可以定义到全局, 这样就不用每个bean节点都去写autowire=”byName”
<?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" 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" default-autowire="byType"><!-- 全局配置 --> <!-- ###############自动装配############### --> <bean id="userDao" class="com.rk.spring.e_autowire.UserDao"></bean> <bean id="userService" class="com.rk.spring.e_autowire.UserService"></bean> <bean id="userAction" class="com.rk.spring.e_autowire.UserAction"></bean> </beans>
完整的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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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" default-autowire="byType"> <!-- ###############自动装配############### --> <!-- <bean id="userDao" class="com.rk.spring.e_autowire.UserDao" autowire="byName"></bean> <bean id="userService" class="com.rk.spring.e_autowire.UserService" autowire="byName"></bean> <bean id="userAction" class="com.rk.spring.e_autowire.UserAction" autowire="byName"></bean> --> <!-- 如果根据类型自动装配: 必须确保IOC容器中只有一个该类型的对象 --> <!-- <bean id="userDao" class="com.rk.spring.e_autowire.UserDao" autowire="byType"></bean> <bean id="userService" class="com.rk.spring.e_autowire.UserService" autowire="byType"></bean> <bean id="userAction" class="com.rk.spring.e_autowire.UserAction" autowire="byType"></bean> --> <bean id="userDao" class="com.rk.spring.e_autowire.UserDao"></bean> <bean id="userService" class="com.rk.spring.e_autowire.UserService"></bean> <bean id="userAction" class="com.rk.spring.e_autowire.UserAction"></bean> <!-- 报错: 因为上面已经有一个该类型的对象,且使用了根据类型自动装配 --> <!-- <bean id="userService2" class="com.rk.spring.e_autowire.UserService"></bean> --> </beans>
UserDao.java
package com.rk.spring.e_autowire; public class UserDao { public void save() { System.out.println("保存用户成功"); } }
UserService.java
package com.rk.spring.e_autowire; public class UserService { private UserDao user_dao; public UserDao getUserDao() { return user_dao; } public void setUserDao(UserDao user_dao) { this.user_dao = user_dao; } public void save() { user_dao.save(); } }
UserAction.java
package com.rk.spring.e_autowire; public class UserAction { private UserService user_service; public UserService getUserService() { return user_service; } public void setUserService(UserService user_service) { this.user_service = user_service; } public void save() { user_service.save(); } }
App.java
package com.rk.spring.e_autowire; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/spring/e_autowire/applicationContext.xml"); @Test public void testIOC() { UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.save(); } }
总结:Spring提供的自动装配主要是为了简化配置; 但是不利于后期的维护。(一般不推荐使用)
5) 注解
注解方式可以简化spring的IOC容器的配置!
使用注解步骤:
1)先引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"
2)开启注解扫描
<context:component-scan base-package="com.rk.spring.f_annotation"></context:component-scan>
3)使用注解
通过注解的方式,把对象加入ioc容器。
创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
方案一:
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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" default-autowire="byType"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.rk.spring.f_annotation"></context:component-scan> </beans>
UserDao.java
package com.rk.spring.f_annotation; import org.springframework.stereotype.Component; @Component("userDao") public class UserDao { public void save() { System.out.println("保存用户成功"); } }
UserService.java
package com.rk.spring.f_annotation; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component("userService") public class UserService { @Resource(name = "userDao") private UserDao user_dao; public UserDao getUserDao() { return user_dao; } public void setUserDao(UserDao user_dao) { this.user_dao = user_dao; } public void save() { user_dao.save(); } }
UserAction.java
package com.rk.spring.f_annotation; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component("userAction") public class UserAction { @Resource(name="userService") private UserService user_service; public UserService getUserService() { return user_service; } public void setUserService(UserService user_service) { this.user_service = user_service; } public void save() { user_service.save(); } }
App.java
package com.rk.spring.f_annotation; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/spring/f_annotation/applicationContext.xml"); @Test public void testIOC() { UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.save(); } }
方案二:
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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" default-autowire="byType"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.rk.spring.g_annotation2"></context:component-scan> </beans>
UserDao.java
package com.rk.spring.g_annotation2; import org.springframework.stereotype.Repository; //把当前对象加入ioc容器 //@Component("userDao") // 相当于bean.xml 【<bean id=userDao class=".." />】 //@Component // 加入ioc容器的UserDao对象的引用名称, 默认与类名一样, 且第一个字母小写 @Repository // 在持久层可以选择用这个注解 public class UserDao { public void save() { System.out.println("保存用户成功"); } }
UserService.java
package com.rk.spring.g_annotation2; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Service // 表示业务逻辑层的组件 public class UserService { //@Resource(name = "userDao") // 根据名称查找 @Resource // 根据类型查找 【在容器中要确保该类型只有一个变量】 private UserDao user_dao; // 去容器中找UserDao类型的变量,找到后就赋值 public UserDao getUserDao() { return user_dao; } public void setUserDao(UserDao user_dao) { this.user_dao = user_dao; } public void save() { user_dao.save(); } }
UserAction.java
package com.rk.spring.g_annotation2; import javax.annotation.Resource; import org.springframework.stereotype.Controller; @Controller // 控制层的组件 public class UserAction { @Resource private UserService user_service; public UserService getUserService() { return user_service; } public void setUserService(UserService user_service) { this.user_service = user_service; } public void save() { user_service.save(); } }
App.java
package com.rk.spring.g_annotation2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/spring/g_annotation2/applicationContext.xml"); @Test public void testIOC() { UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.save(); } }
总结:
1) 使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI)
2) 注解可以和XML配置一起使用。
Spring,负责对象对象创建
Struts, 用Action处理请求
Spring与Struts框架整合,关键点:让struts框架action对象的创建,交给spring完成!
SSH整合:
Spring与Struts整合的关键点: action交给spring创建!
Spring与Hibernate整合的关键点:sessionFactory对象交给spring创建!
Spring与Struts框架整合的步骤:
引入jar文件:
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源码】
配置:
4)配置XML
struts.xml 【struts路径与action映射配置】
beans.xml 【spring ioc容器配置】
web.xml 【核心过滤器: 引入struts功能】【初始化spring的ioc容器】
5) 代码以及配置
整合项目的结构
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"> <display-name></display-name> <!-- 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/beans-*.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> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="false" /> <package name="my_spring_struts_package" namespace="/" extends="struts-default"> <action name="saveUser" class="userAction" method="save"> <result name="success" type="dispatcher">/success.jsp</result> </action> </package> </struts>
beans-entity.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" 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"> <bean id="user" class="com.rk.entity.User"></bean> </beans>
beans-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" 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"> <bean id="userDao" class="com.rk.dao.UserDao"></bean> </beans>
beans-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" 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"> <bean id="userService" class="com.rk.service.UserService"> <property name="user_dao" ref="userDao"></property> </bean> </beans>
beans-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" 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"> <bean id="userAction" class="com.rk.action.UserAction" scope="prototype"> <property name="user_service" ref="userService"></property> <property name="user" ref="user"></property> </bean> </beans>
User.java
package com.rk.entity; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
UserDao.java
package com.rk.dao; import com.rk.entity.User; public class UserDao { public void save(User user) { System.out.println("用户保存成功:" + user); } }
UserService.java
package com.rk.service; import com.rk.dao.UserDao; import com.rk.entity.User; public class UserService { private UserDao user_dao; public UserDao getUser_dao() { return user_dao; } public void setUser_dao(UserDao user_dao) { this.user_dao = user_dao; } public void save(User user) { user_dao.save(user); } }
UserAction.java
package com.rk.action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.rk.entity.User; import com.rk.service.UserService; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user; private UserService user_service; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUser_service() { return user_service; } public void setUser_service(UserService user_service) { this.user_service = user_service; } @Override public User getModel() { return user; } public String save() { System.out.println(user); user_service.save(user); return Action.SUCCESS; } }
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>保存用户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form action="${pageContext.request.contextPath }/saveUser.action" method="post"> <label for="input_id">用户id:</label> <input id="input_id" name="user.id"/> <br/> <label for="input_name">用户名:</label> <input id="input_name" name="user.name"/> <br/> <input type="submit" value="提交"/> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>保存成功</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> 用户id:<s:property value="id"/> <br/> 用户名称:<s:property value="name"/> <br/> <s:debug></s:debug> </body> </html>
标签:spring
原文地址:http://lsieun.blog.51cto.com/9210464/1828542