spring入门(一)
emmm,这两天才拿到offer。公司不推荐我去实习,因为补贴很低。所以我现在的局面是大量的空白时间。当然我现在需要找点事情做才行。在当时面试的时候,面试官问了一些spring的东西。当然问得很简单,但是这不由得我重新整理一下spring,当时只是简单的过了一遍没有通过文章整理(虽然用OneNote记了一次笔记,写了一些代码,但是收益不是很大)。正如之前复习javase一样,必须把基础弄夯实才能修建高楼大厦。现在我重新系统地整理一遍笔记。
spring介绍
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式)轻量级开源框架。
特点
优点
- 方便解耦,简化开发
spring就是一个大工程,可以将所有对象创建和依赖关系维护,交给spring管理 - AOP编程的支持
spring提供了面向切面编程,可以方便实现对程序进行权限拦截,运行监控等功能 - 方便程序的测试
spring对Junit4支持,可以通过注解方便测试spring程序 - 框架的直接支持
spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀的框架的直接支持 - 降低javaee API的使用难度
spring 对javaee开发中非常难用的一些API(JDBC,JavaMail,远程调用)。都提供了封装,使这些API应用难度降低
个人理解:
将对象创建交给spring之后,自己就不用显式调用new创建了。这样代码里面就没有许多"多余"的new。真的逊爆了!
面向切面的好处在于对于一个方法就真的可以只做自己的事请了。就不用准备各种奇怪的东西,比如日志记录,权限检查。勇士就只应该拿上剑,去拯救公主。记录英勇事迹这种事应该交给游吟诗人!各其所职。
在我学习spring的时候,我觉得最帅的事情就是即写即所得。使用了Junit之后,代码变得十分容易测试。然后使用测试驱动开发的思想结合起来。bug胎死腹中。bug修复成本就会降到最低。
spring方便和其他秀框架集成。比如mybatis,可以直接通过一个配置就将mybatis和spring完美的整合在了一起(不要太容易好吗)。
曾经写javaee项目的时候,你最痛苦的是什么?不用多说绝对是dao层的编写。就如同下图一样的代码,真的太多太多了!!!!!!有一次我写了三十多个这种的代码,朋友不要太窒息!!!!
public List<Admin> select(String condition) {
connection = MysqlBaseDao.getAdminConnection();// 连接数据库
List<Admin> admins = new ArrayList<>();
String sql = "SELECT * FROM `pesystem`.`tb_admin` where" + condition;
System.out.println(sql);
Admin admin = null;
try {
Statement stmt = connection.createStatement();
resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
admin = new Admin();
admin.setUserName(resultSet.getString("adminName"));
admin.setUserEmail(resultSet.getString("adminEmail"));
admin.setUserId(resultSet.getString("adminId"));
admin.setUserTel(resultSet.getString("adminTel"));
admin.setUserPwd(resultSet.getString("adminPwd"));
admins.add(admin);
}
Iterator<Admin> iterator = admins.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
MysqlBaseDao.close(resultSet, preparedStatement, connection);
}
return admins;
}
如果使用spring的jdbcTemplate呢?
这一部分代码来源于网络
package com.spring.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import com.spring.domain.User;
public class UserDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void addUser(User user) {
String sql = "insert into user (username, password) values (?, ?)";
jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
}
}
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: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">
<!-- IOC和DI的注解扫描 -->
<context:component-scan base-package="com.spring" ></context:component-scan>![20180713202033735.jpg](https://img.haomeiwen.com/i13539521/6aaf011053c651c1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<!-- 打开AOP的注解 -->
<!-- 这里用的是中间的横线而不是下划线 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_03"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="com.spring.dao.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="userService" class="com.spring.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
这样只需要改sql语句和一点入参什么的。模板代码就不用了!当然整合了mybatis之后,你又会惊讶~~
缺点
配置,真的令人窒息好吗?当然后面可以用springboot来改善。好的框架意味着有一个精美的牢笼,你将失去自由。对于核心设计模式和思想都会被隐藏和限定,让你窒息。
spring结构
20180713202033735.jpg四个核心:beans,core,context,expression
测试:Junit
面向切面:aspect4j
数据访问:jdbcTemplate
web:springmvc
HelloWorld
项目目录
我使用的idea创建的spring项目并没有用maven来引入包,项目结构如下:
image.png
创建UserService接口
public interface UserService {
void addUser();
}
创建UserServiceImpl
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("UserServiceImpl.addUser");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.spring.helloworld.UserServiceImpl"/>
</beans>
测试
public class UserServiceImplTest {
@Test
public void addUserBefore(){
UserService userService = new UserServiceImpl();
userService.addUser();
}
@Test
public void addUserByIOC() {
String applicationContextPath = "com/spring/helloworld/applicationContext.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(applicationContextPath);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.addUser();
}
}
结果
addUserBeforece测试结果
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\ideaIU-2018.1.1.win\lib\idea_rt.jar=52096:D:\ideaIU-2018.1.1.win\bin -Dfile.encoding=UTF-8 -classpath "D:\ideaIU-2018.1.1.win\lib\idea_rt.jar;D:\ideaIU-2018.1.1.win\plugins\junit\lib\junit-rt.jar;D:\ideaIU-2018.1.1.win\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;E:\workspace\springStudy\out\production\springStudy;E:\workspace\springStudy\lib\spring-instrument-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-instrument-tomcat-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-aspects-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-context-support-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-expression-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-aop-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-jms-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-oxm-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-jdbc-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-messaging-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-tx-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-orm-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\commons-logging-1.2.jar;E:\workspace\springStudy\lib\aopalliance-1.0.jar;E:\workspace\springStudy\lib\spring-beans-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-test-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-core-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-context-4.3.18.RELEASE.jar;D:\ideaIU-2018.1.1.win\lib\junit-4.12.jar;D:\ideaIU-2018.1.1.win\lib\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.spring.helloworld.UserServiceImplTest,addUserBeFore
UserServiceImpl.addUser
Process finished with exit code 0
addUSerByIOC测试结果
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\ideaIU-2018.1.1.win\lib\idea_rt.jar=51709:D:\ideaIU-2018.1.1.win\bin -Dfile.encoding=UTF-8 -classpath "D:\ideaIU-2018.1.1.win\lib\idea_rt.jar;D:\ideaIU-2018.1.1.win\plugins\junit\lib\junit-rt.jar;D:\ideaIU-2018.1.1.win\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;E:\workspace\springStudy\out\production\springStudy;E:\workspace\springStudy\lib\spring-instrument-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-instrument-tomcat-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-aspects-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-context-support-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-expression-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-aop-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-jms-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-oxm-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-jdbc-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-messaging-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-tx-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-orm-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\commons-logging-1.2.jar;E:\workspace\springStudy\lib\aopalliance-1.0.jar;E:\workspace\springStudy\lib\spring-beans-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-test-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-core-4.3.18.RELEASE.jar;E:\workspace\springStudy\lib\spring-context-4.3.18.RELEASE.jar;D:\ideaIU-2018.1.1.win\lib\junit-4.12.jar;D:\ideaIU-2018.1.1.win\lib\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.spring.helloworld.UserServiceImplTest,addUserByIOC
十一月 01, 2018 9:39:42 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@64cee07: startup date [Thu Nov 01 09:39:42 CST 2018]; root of context hierarchy
十一月 01, 2018 9:39:42 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/spring/helloworld/applicationContext.xml]
UserServiceImpl.addUser
Process finished with exit code 0
看结果可以看出使用spring的有更多的输出,当然都是配置什么的东西。