Spring1
来源:互联网 发布:淘宝服装定制店怎么开 编辑:程序博客网 时间:2024/06/08 02:27
一、什么是Spring
Spring框架是以简化J2EE应用程序开发为特定目标而建立的.
Spring是一个轻量级的IOC(DI)和AOP容器框架.(IOC:控制反转;DI:依赖注入;AOP:面向切面)
1、Spring模块
Application Context 上下文模块。
这个模块扩展了BeanFactory,添加了对I18N、应用生命周期事件以及验证的支持。另外,这个模块提供了很多企业级服务,如电子邮件、JNDI访问、EJB集成、远程调用以及定时服务,并且支持与模板框架(如Velocity和FreeMarker)的集成。
AOP模块
Spring对面向切面编程提供了丰富的支持。这个模块是为Spring应用开发切面的基础。与DI一样,AOP支持应用对象之间的松耦合。利用AOP,应用程序所关心的与其应用的对象关系不大。
JDBC抽象及DAO模块
Spring的JDBC和DAO模块把编写JDBC代码时的一些样板代码(获得连接、创建语句、处理结果、关闭连接)抽象出来,让数据库代码变得简单明了。
ORM映射集成模块
Spring MVC框架
Spring 的WEB模块
二、常见的注入方式
接口注入
构造方法注入
Setter方法注入
@Annotation反射注入
三、BeanFactory与ApplicationContext 的区别
BeanFactory
基础类型IoC容器,提供完整的IoC服务支持。
特点:默认采用延时初始化策略、启动速度快、资源占用少
ApplicationContext
ApplicationContext完全由BeanFactory扩展而来,增加了更多支持企业核心内容的功能。
特点:默认采用立即初始化策略、事件发布、国际化信息支持……
四、JDBC连接
applicationcontext.xml配置
<!-- 连接数据库 --> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/linsenhai?useUnicode=true&characterEncoding=utf8"></property> <property name="username" value="root"></property> <property name="password" value="*********"></property> </bean> <!-- jdbc模板,封装增删改查方法 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="datasource"></property> </bean> <bean id="userdao" class="com.dao.imple.UserDaoImple"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean>
增删改查:
public class UserDaoImple implements UserDao { private JdbcTemplate jdbcTemplate; class userMap implements RowMapper<User> { public User mapRow(ResultSet rs, int rowNums) throws SQLException { //System.out.println(rowNums); User user = new User(); user.setUserId(rs.getInt("user_id")); user.setUserName(rs.getString("user_name")); user.setUserPass(rs.getString("user_pass")); return user; } } public int addUser() { int result = jdbcTemplate.update( "insert into tb_user(user_name,user_pass) values(?,?)", "***", "123456"); System.out.println(result); return result; } public int delUser(int id){ int result = jdbcTemplate.update("delete from tb_user where user_id=?", id); System.out.println(result); return result; } public int updateUser(User user) { int result = jdbcTemplate.update( "update tb_user set user_name=? where user_id=?", user.getUserName(), user.getUserId()); return result; } public List<User> getAllUser() { List<User> users = jdbcTemplate.query("select * from tb_user", new userMap()); return users; } public User getUser(int uid) { User user = jdbcTemplate.queryForObject( "select * from tb_user where user_id=?", new Object[] { uid }, new userMap()); return user; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; }}
五、Spring整合struts
1、修改web.xml,在web启动的时候加载spring 容器,添加Spring 3.0 Web Libraries包。classpath的xml文件时是src下的Spring的XML配置文件,名字要相同
<!-- web启动的时候加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:springContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
2、导入struts-spring-2.x.jar支持包
3、注意action需要配置成为非单例
<!-- 这里的action都是单例的,必须设置为非单例 --><bean id="userAction" class="com.action.UserAction" scope="prototype"> <property name="userBiz" ref="userBiz"></property></bean>
4、配置action时不能直接New出来
//spring整合struts,className不能直接New出来, //而是要从spring容器中组件中取,和applicationContext.xml中的action的id一致 @Action(value = "listUsers", className = "userAction", results = { @Result(name = "success", location = "/listuser.jsp") }) public String listUsers() { users = userBiz.getUsers(); return SUCCESS; }
5、推荐面向接口编程
6、一般采用Spring框架的都是大型项目,需分三层:dao、biz(业务层)、action;其中action调用biz层,biz层调用dao层
六、Spring中的邮件发送
1、设置邮件发送配置文件
<!-- 邮件发送者配置 --> <bean name="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="username" value="********@qq.com"></property> <property name="password" value="*********"></property> <property name="port" value="465"></property> <property name="defaultEncoding" value="utf-8"></property> <property name="host" value="smtp.qq.com"></property> <property name="javaMailProperties"> <props> <prop key="mail.debug">true</prop> <prop key="mail.smtp.auth">true</prop> <!-- 采用ssl 加密方式 --> <prop key="mail.smtp.starttls.enable">true</prop> <prop key="mail.smtp.socketFactory.class"> javax.net.ssl.SSLSocketFactory </prop> </props> </property> </bean> <bean id="mailAction" class="com.action.EmailAction" scope="prototype"> <property name="javaMailSender" ref="mailSender"></property> </bean>
2、导包Spring 3.0 Misc Library、mail.jar、activation.jar,新建EmailAction
@Namespace("/")@ParentPackage("default")public class EmailAction extends ActionSupport{ private JavaMailSender javaMailSender; @Action(value="send",className="mailAction",results={@Result(name="success",location="/success.jsp")}) public String send(){ //简单邮件发送 SimpleMailMessage message = new SimpleMailMessage(); message.setTo("********@qq.com"); message.setFrom("********@qq.com"); message.setText("hello world"); message.setSubject("FirstEmail"); javaMailSender.send(message); return SUCCESS; } public void setJavaMailSender(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; }}
//支持HTML、内嵌图片、附件 MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("********@qq.com"); helper.setFrom("*******@qq.com"); helper.setText("<html><body><h1>hello world</h1> <img src='cid:s'/></body></html>",true); FileSystemResource res = new FileSystemResource(new File("f://s.jpg")); helper.addInline("s", res); FileSystemResource file = new FileSystemResource(new File("f://s.zip")); helper.addAttachment("sfile.zip", file); javaMailSender.send(message); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- Spring1
- spring1
- spring1
- Spring1
- Spring1
- spring1
- spring1
- Spring1
- Spring1
- spring1
- Spring1--浅谈
- 1001 spring1
- Spring1.基础知识
- spring1 selfApplicationContext used
- Spring1 Bean实例化
- spring1.5x单元测试
- spring1 配置方式注入
- Spring1.x声明式事务
- 解决Xcode引用VLCKit之后Undefined symbols for architecture 问题
- java接口
- LeetCode 190: Reverse Bits
- CSRF引用学习
- 实现lucene来进行全文索引查找
- Spring1
- VPN 双机热备 灾难备份
- linux 批量ssh认证
- dataguard故障ERROR 1017 ORA-16191
- https证书 wamp2.5 配置单双向SSL证书
- 欢迎使用CSDN-markdown编辑器
- 有向图
- Ubuntu14.04 安装PIL(Image) Package
- 解决:IE浏览器打开就自动最小化,无法打开